Machine Learning
Labels
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.
Exercise 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.
Show example 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))
Test yourself
-
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
- Completely wrong
- Half right
Show answer
Perfect; only the numbers are swapped — The groups are the same, only the names differ; ARI = 1.
-
Measure the agreement of two labellings.
ari = ____(y_gercek, km.labels_)
Show answer
ari = adjusted_rand_score(y_gercek, km.labels_)
-
Match the criteria.
- ARI = 1
- Full agreement
- ARI ≈ 0
- Random level
- accuracy_score
- Misleading for clustering
- silhouette
- Label-free internal criterion
Next lesson: Summary - Clustering