Groups and dense areas
Ordito’s head of marketing would like to write different messages to different customers, but she does not know what types of customers she has. There is no target to predict: the task is to find groups of people who resemble each other. This is clustering, and the two most widely used algorithms reason in opposite ways.
Centres or dense areas
K-means represents each group with a centre. It alternates two steps: each point goes to the nearest centre, each centre moves to the mean of its points. The inertia, the sum of the squared distances from the centres, can only go down, until the groups stop changing. The number of groups k is chosen by whoever uses the algorithm.
DBSCAN instead looks for dense areas: a point is a core point if it has at least min_samples neighbours within the distance eps, and neighbouring core points form a group. Whoever is in an empty area stays out: it is noise. The number of groups comes from the data.
In the lab you try the two algorithms on three sets of points.
Three experiments
The worked example. On the six customers of the chapter K-means deliberately starts from two awkward centres, A and C. On the first round the inertia is 129, on the second C changes group, on the third nobody moves: {A, B, C} and {D, E, F}, with inertia 13.33.
The half-moons. K-means cuts them with a straight line, because each point goes to the nearest centre. DBSCAN with eps = 0.2 recovers the two exact moons, 150 points each, without knowing that there should be two.
The cloud of customers. On Ordito’s customers DBSCAN finds a single large group or many fragments, because there are no gaps between the types of customers. K-means divides them anyway, and the five groups in the book, sale hunters, loyalists, young digitals, premium classics and quiet regulars, are a useful choice for marketing, not a truth written in the data.
The same experiment in Python
import numpy as np
from sklearn.cluster import DBSCAN, KMeans
from sklearn.datasets import make_moons
punti = np.array([[1, 8], [2, 9], [4, 6], [8, 2], [9, 3], [10, 1]], dtype=float) # the six customers
km = KMeans(n_clusters=2, init=punti[[0, 2]], n_init=1).fit(punti) # we start from A and C
print(km.labels_, round(km.inertia_, 2), km.n_iter_)
lune, _ = make_moons(n_samples=400, noise=0.07, random_state=0)
print(np.bincount(KMeans(n_clusters=2, n_init=10, random_state=0).fit_predict(lune)))
print(np.unique(DBSCAN(eps=0.2, min_samples=5).fit_predict(lune), return_counts=True))
In the book
This lab accompanies chapter 12, “Clustering”: K-means from scratch and with scikit-learn, k-means++, the silhouette and the elbow, reading the groups and their stability, the comparison with the true segments, hierarchical clustering with the dendrogram of the stores, and DBSCAN.
The “Inside Ordito” series
- Chapter 1 The line of the five stores
- Chapter 2 A simulated store
- Chapter 3 Where the leak hides
- Chapter 4 The valley of the five stores
- Chapter 5 The threshold
- Chapter 6 Why was it returned?
- Chapter 7 Build a tree
- Chapter 8 Boosting step by step
- Chapter 9 The margin
- Workshop A Price is place
- Chapter 10 Shapley by hand
- Chapter 11 Rotate the cloud
- Chapter 12 Groups and dense areas
- Chapter 13 Isolating a point
- Chapter 14 Naive forecasts
- Workshop B A year in production
- Chapter 15 The ink stain
- Chapter 16 Backpropagation