What is clustering?
- Machine Learning course
- Intermediate
- Lesson 47/112
- 5 min
Clustering is an unsupervised method that groups unlabeled data by similarity.
What is clustering?
Clustering is unsupervised learning that splits similar examples into groups without labels. Uses: customer segmentation, document grouping, anomaly detection, region separation in images.
from sklearn.cluster import KMeans
km = KMeans(n_clusters=3, n_init=10, random_state=0).fit(X)
print(km.labels_[:10]) # cluster number of each exampleCluster numbers are arbitrary: cluster 0 isn't the "first" or the "best", just a name.
Similarity and scale
Most clustering algorithms rely on distance, so scaling is a must. Also, the definition of a "good cluster" changes with the algorithm:
- KMeans: round clusters of similar size.
- DBSCAN: density-based; any shape, separates noise.
- Hierarchical: a tree of nested clusters.
from sklearn.preprocessing import StandardScaler
Xs = StandardScaler().fit_transform(X)
km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(Xs)Practice First clustering
Train a KMeans(n_clusters=4, n_init=10, random_state=0) on the synthetic 4-centre data. Print how many examples each cluster contains with np.bincount(km.labels_).
Starter code
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
X, y_gercek = make_blobs(n_samples=300, centers=4, cluster_std=0.8, random_state=0)
# buraya yazSample solution
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
X, y_gercek = make_blobs(n_samples=300, centers=4, cluster_std=0.8, random_state=0)
km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(X)
print(np.bincount(km.labels_))Expected output
[81 74 73 72]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How does clustering differ from classification?
- There is no difference
- It uses no labels in training Correct answer
- It is more accurate
- It finds only two groups
Explanation: Groups are derived from the data's own structure; no correct answer is given.
2 Read the cluster label of every example.
etiket = km.____
etiket = km.labels_
Explanation: labels_ gives the cluster of every example in the training data.
3 Match the algorithms.
| Term | Meaning |
|---|---|
KMeans |
Round clusters |
DBSCAN |
Density, noise |
Hierarchical |
Cluster tree |
StandardScaler |
Required before distance |