Summary - Clustering
- Machine Learning course
- Intermediate
- Lesson 52/112
- 6 min
In this group you learned clustering, KMeans, and the elbow method.
Summary: clustering
In this group:
- Clustering groups without labels; the numbers are arbitrary.
- KMeans: assign → move to the mean; assumes round, similar clusters; scaling is a must.
- Centroids are the clusters' typical examples;
transformgives distances to the centres. - Choosing k: elbow (
inertia_) and the silhouette score. - ARI for comparing with a known structure; plain accuracy misleads.
In the next groups you'll see how KMeans is initialised and hierarchical clustering.
Practice KMeans and DBSCAN
Run KMeans(n_clusters=2, n_init=10, random_state=0) and DBSCAN(eps=0.3, min_samples=5) on the moon data. Print both ARI values against the true labels with round(..., 3) on separate lines.
Starter code
from sklearn.datasets import make_moons
from sklearn.cluster import KMeans, DBSCAN
from sklearn.metrics import adjusted_rand_score
X, y = make_moons(n_samples=300, noise=0.05, random_state=0)
# buraya yazSample solution
from sklearn.datasets import make_moons
from sklearn.cluster import KMeans, DBSCAN
from sklearn.metrics import adjusted_rand_score
X, y = make_moons(n_samples=300, noise=0.05, random_state=0)
km = KMeans(n_clusters=2, n_init=10, random_state=0).fit_predict(X)
db = DBSCAN(eps=0.3, min_samples=5).fit_predict(X)
print(round(adjusted_rand_score(y, km), 3))
print(round(adjusted_rand_score(y, db), 3))Expected output
0.241 1.0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is more suitable than KMeans for moon-shaped clusters?
- DBSCAN Correct answer
- KMeans with a larger k
- PCA
- Logistic regression
Explanation: Density-based DBSCAN finds clusters regardless of shape.
2 Set up density-based clustering.
db = ____(eps=0.3, min_samples=5)
db = DBSCAN(eps=0.3, min_samples=5)
Explanation: DBSCAN grows clusters from points with min_samples neighbours within radius eps.
3 Match the tools.
| Term | Meaning |
|---|---|
KMeans |
Centroid-based |
DBSCAN |
Density-based |
silhouette_score |
Choosing k |
adjusted_rand_score |
Comparison with labels |