Menu
CoddyTech

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:

A 3 by 3 table of counts, clusters down and coffee kinds across: arabica, liberica, robusta. Cluster 0 holds 49 robusta beans; cluster 1 holds 48 arabica, 3 liberica and 1 robusta; cluster 2 holds 47 liberica and 2 arabica

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 icon

Challenge

Medium

Do k-means clusters find the iris species? The starter scales the flowers, keeps their names in species, reads k and clusters them.

  1. Crosstab the clusters against the species.
  2. For each species, print its largest count in one cluster, out of its total.
  3. 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 3

Try 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
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