Menu
CoddyTech

Name the Clusters

Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 46 of 51.

Clusters come with numbers, not names. Now you may use the labels: a crosstab counts which digits landed in each cluster, like these 1s, 7s and 9s in 3 clusters:

A 3 by 3 table of counts, clusters down and digits 1, 7 and 9 across. Each row's biggest count is boxed: cluster 0 holds 166 nines, cluster 1 176 sevens, cluster 2 154 ones

Name each cluster after its most common digit. Its scans of that digit count as named right:

ct = pd.crosstab(km.labels_, y)
names = ct.idxmax(axis=1)      # each row's biggest column
right = ct.max(axis=1).sum()
print(names.tolist(), right)

This prints [9, 7, 1] 496: 496 of 541 scans named right, by groups found without a single label.

challenge icon

Challenge

Medium

Name your 10 clusters. The starter carries km, fitted without labels; now you may use the digits in y.

  1. Crosstab the clusters against y and name each cluster after its most common digit.
  2. In order of the names, print each cluster's scans named right, its size and its share right, as a whole percent.
  3. Print the scans named right overall, the percent to 1 decimal.

For 5 clusters:

Cluster named 0: 175 of 177 scans (99%)
Cluster named 2: 160 of 367 scans (44%)
Cluster named 3: 168 of 531 scans (32%)
Cluster named 6: 179 of 359 scans (50%)
Cluster named 7: 176 of 363 scans (48%)
Named right without labels: 858 of 1797 scans (47.7%)

Which of your clusters is weakest, and what hides in it?

Try it yourself

import pandas as pd
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans

# From Look at the Digits: 1,797 scans of 64 pixels, ink from 0 to 16
digits = load_digits()
X = digits.data
y = digits.target

# From Shrink with PCA: 80% of the variance in 13 numbers per scan
pca = PCA(n_components=0.8)
X_small = pca.fit_transform(X)

# From Cluster the Digits: 10 clusters, found without a label
km = KMeans(n_clusters=10, n_init=10, random_state=0).fit(X_small)

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler