cat /en/groups-and-dense-areas/
MDN-120:40
SERIES Inside Ordito · Chapter 12

Groups and dense areas

· Artificial Intelligence · 4 min read

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.

DEMO Groups and dense areas · about this demo · full screen ↗

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.

COVER COMING SOON
mltp · 2026

Machine Learning. Teoria, matematica e pratica degli algoritmi

Michele di Nuzzo · in Italian

Every algorithm in two steps: first derive it, with the mathematics it needs and an example worked by hand, then write it from scratch in NumPy. Only at the end use it with scikit-learn.

[the book’s page] [shop links coming soon]

The “Inside Ordito” series