Clusters vs Known Labels
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 13 of 51.
When labels exist but k-means never saw them, check what it found: pd.crosstab counts each pair of cluster and label.
labels = KMeans(n_clusters=3, n_init=10, random_state=0).fit_predict(X)
print(pd.crosstab(labels, kind))For 150 coffee beans of 3 kinds, clustered on length and caffeine: one row per cluster, one column per kind:
Each cluster is mostly one kind; only 6 beans strayed. And k-means never saw a label.
Clusters are not classes, though. A close match means the classes differ in the data. A poor match is a finding, not a bug.
Challenge
MediumDo k-means clusters find the iris species? The starter scales the flowers, keeps their names in species, reads k and clusters them.
- Crosstab the clusters against the species.
- For each species, print its largest count in one cluster, out of its total.
- Count the clusters that hold a single species.
For another garden and k = 3:
setosa: 48 of 50 share a cluster
versicolor: 41 of 50 share a cluster
virginica: 33 of 50 share a cluster
Clusters with one species: 0 of 3Try it yourself
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
# 150 iris flowers, scaled, and the species name of each, hidden from k-means
iris = load_iris()
X = StandardScaler().fit_transform(iris.data)
species = iris.target_names[iris.target]
k = int(input())
labels = KMeans(n_clusters=k, n_init=10, random_state=0).fit_predict(X)
# 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 Ones3Reading Clusters
Profiling ClustersCenters in Real UnitsClusters vs Known LabelsWhen K-Means FailsRecap: Tea TastingPractice on your own: Online Python compiler