Clustering is unsupervised: with no labels, it groups similar points together. K-means is the classic algorithm — you choose the number of clusters k, and it finds k centres that minimise within-cluster distance.
K-means
from sklearn.cluster import KMeanskm = KMeans(n_clusters=3, random_state=42, n_init="auto")labels = km.fit_predict(X) # each point's cluster idkm.cluster_centers_ # the centre of each cluster
Choosing k
Key takeaways
- 01Clustering groups unlabelled data by similarity (unsupervised).
- 02K-means finds k cluster centres minimising within-cluster distance; you choose k.
- 03Use the elbow method or silhouette score to pick k.
- 04Scale features first; k-means assumes spherical clusters (try DBSCAN for others).
