Week 07 · Clustering with centroids and density
See why a centroid objective can split a curved cluster.
← Week 6 · Subject contents · Week 8 →
See why a centroid objective can split a curved cluster.
Work through the problem
K-means minimizes the sum of squared distances from each point to its assigned centroid. The boundary between two centroids is a line in two dimensions. Two crescents can therefore be split by position even when each crescent is connected.
DBSCAN identifies core points with at least min_samples neighbours inside radius ε, counting the point itself. It connects nearby core points and can mark isolated points as noise. A single radius is difficult when groups have different densities, and both methods depend on feature scale.
Two crescents and a centroid split.
Run the Python lab
from sklearn.cluster import KMeans, DBSCAN
from sklearn.datasets import make_moons
from sklearn.metrics import adjusted_rand_score
from sklearn.preprocessing import StandardScaler
X,y = make_moons(n_samples=320,noise=.045,random_state=7)
for name, coords in [("Original",X),("Scaled first axis",X*[12,1]),("Standardized",StandardScaler().fit_transform(X*[12,1]))]:
preds = [KMeans(n_clusters=2,n_init=10,random_state=2).fit_predict(coords),DBSCAN(eps=.20,min_samples=5).fit_predict(coords)]
print(name,[round(adjusted_rand_score(y,p),3) for p in preds])
if name=="Original":
assert adjusted_rand_score(y,preds[1]) > adjusted_rand_score(y,preds[0])
Output from the lab
Original [0.235, 1.0]
Scaled first axis [0.107, 0.021]
Standardized [0.454, 1.0]
Practice
- If there are no known labels, can you still report adjusted Rand score against truth?
Check your reasoning
- No. Inspect cluster stability and fitness for the actual decision, and report what remains unverified.