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:
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
MediumName your 10 clusters. The starter carries km, fitted without labels; now you may use the digits in y.
- Crosstab the clusters against
yand name each cluster after its most common digit. - In order of the names, print each cluster's scans named right, its size and its share right, as a whole percent.
- 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unsupervised Learning
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd OnesPractice on your own: Online Python compiler