Labels
- Machine Learning course
- Intermediate
- Lesson 51/112
- 5 min
KMeans assigns a cluster label to each example, stored in labels_.
Interpreting the labels
Cluster labels are numbers, not names; rerunning on the same data can swap 0 and 2. That's why clusters are defined by their contents (centre, size, example features), not by their numbers.
for k in range(km.n_clusters):
uyeler = X[km.labels_ == k]
print(k, len(uyeler), uyeler.mean(axis=0).round(1))If the true labels are known (for testing), the comparison must use a criterion independent of the numbers.
Adjusted Rand index
adjusted_rand_score measures how well two labellings agree, independently of the numbers: 1 is full agreement, 0 the random level. It is used to compare clustering methods on synthetic data with a known structure:
from sklearn.metrics import adjusted_rand_score
print(adjusted_rand_score([0, 0, 1, 1], [1, 1, 0, 0])) # 1.0
print(adjusted_rand_score(y_gercek, km.labels_))Real projects have no labels; then internal criteria like the silhouette and domain knowledge are used.
Practice Validate the clustering
Train a KMeans(n_clusters=4, n_init=10, random_state=0) on the synthetic data. Print the ARI against the true labels with round(..., 3). Then print the plain accuracy_score(y_gercek, km.labels_) with round(..., 3) and see why the two differ.
Starter code
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)
from sklearn.metrics import adjusted_rand_score, accuracy_score
# buraya yazSample solution
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)
from sklearn.metrics import adjusted_rand_score, accuracy_score
km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(X)
print(round(adjusted_rand_score(y_gercek, km.labels_), 3))
print(round(accuracy_score(y_gercek, km.labels_), 3))Expected output
0.946 0.497
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 If the true labels are [0, 0, 1, 1] and the cluster labels [1, 1, 0, 0], how successful is the clustering?
- It can't be evaluated
- Perfect; only the numbers are swapped Correct answer
- Completely wrong
- Half right
Explanation: The groups are the same, only the names differ; ARI = 1.
2 Measure the agreement of two labellings.
ari = ____(y_gercek, km.labels_)
ari = adjusted_rand_score(y_gercek, km.labels_)
Explanation: ARI isn't affected by swapped numbers.
3 Match the criteria.
| Term | Meaning |
|---|---|
ARI = 1 |
Full agreement |
ARI ≈ 0 |
Random level |
accuracy_score |
Misleading for clustering |
silhouette |
Label-free internal criterion |