Een clustering evalueren

Unsupervised Learning in Python

Benjamin Wilson

Director of Research at lateral.io

Een clustering evalueren

  • Je kunt overeenkomsten checken met bv. irissoorten
  • ... maar wat als er geen soorten zijn om mee te vergelijken?
  • Meet de kwaliteit van een clustering
  • Helpt bij de keuze van het aantal clusters
Unsupervised Learning in Python

Iris: clusters vs. soorten

  • k-means vond 3 clusters in de iris-samples
  • Komen de clusters overeen met de soorten?
species  setosa  versicolor  virginica
labels
0             0           2         36
1            50           0          0
2             0          48         14
Unsupervised Learning in Python

Kruistabel met pandas

  • Clusters vs. soorten is een "kruistabel"
  • Gebruik de pandas-bibliotheek
  • Gegeven de soort van elke sample als lijst species
print(species)
['setosa', 'setosa', 'versicolor', 'virginica', ... ]
Unsupervised Learning in Python

Labels en soorten uitlijnen

import pandas as pd
df = pd.DataFrame({'labels': labels, 'species': species})
print(df)
     labels     species
0         1      setosa
1         1      setosa
2         2  versicolor
3         2   virginica
4         1      setosa
...
Unsupervised Learning in Python

Kruistabel van labels en soorten

ct = pd.crosstab(df['labels'], df['species'])
print(ct)
species  setosa  versicolor  virginica
labels
0             0           2         36
1            50           0          0
2             0          48         14

Hoe evalueer je een clustering als er geen soortinformatie is?

Unsupervised Learning in Python

Clusteringkwaliteit meten

  • Alleen met samples en hun clusterlabels

  • Een goede clustering heeft compacte clusters

  • Samples binnen elk cluster dicht bij elkaar

Unsupervised Learning in Python

Inertia meet clusteringkwaliteit

  • Meet hoe verspreid de clusters zijn (lager is beter)
  • Afstand van elke sample tot het centroid van z’n cluster
  • Na fit() beschikbaar als attribuut inertia_
  • k-means minimaliseert de inertia bij het kiezen van clusters
from sklearn.cluster import KMeans

model = KMeans(n_clusters=3)
model.fit(samples)
print(model.inertia_)
78.9408414261
Unsupervised Learning in Python

Het aantal clusters

  • Clusterings van de iris-dataset met verschillende aantallen clusters
  • Meer clusters betekent lagere inertia
  • Wat is het beste aantal clusters?

Lijndiagram: aantal clusters vs. inertia

Unsupervised Learning in Python

Hoeveel clusters kies je?

  • Een goede clustering heeft compacte clusters (dus lage inertia)
  • ... maar niet té veel clusters!
  • Kies een "elleboog" in de inertia-plot
  • Waar de inertia trager daalt
  • Voor iris is 3 een goede keuze

Lijndiagram: aantal clusters vs. inertia

Unsupervised Learning in Python

Laten we oefenen!

Unsupervised Learning in Python

Preparing Video For Download...