Clustering

../_images/cluster_warmup.png

How many clusters do you see in the picture?

Key Concepts

concept

description

k-Means clustering

algorithm assigning spherical clusters

hierarchical clustering

tree-building (agglomerative) clustering algorithms

DBSCAN

clustering algorithm that takes outliers into account

Curse of dimensionality

euclidean distance does not work well for higher dimensions

Silhouette Score

metric assessing the spherical shape of clusters

ARI

metric comparing a clustering to a “gold standard”

What is clustering used for?

  • Inventory categorization

  • Behavioral segmentation (e.g. by purchase history, activities on application, website, or platform)

  • Define personas based on interests

  • Create profiles based on activity monitoring

  • Detect activity types in motion sensors

  • Group images

  • Separate audio

  • Identify groups in health monitoring

  • Detecting bots

  • Anomaly or outlier detection

Clustering in scikit-learn

Compare the K-Means and DBScan algorithms on the moons dataset using the script below.

  1. Run the script

  2. Exchange moons for blobs

  3. Adjust the parameters n_clusters and eps to improve the clustering


from sklearn import cluster, datasets
from sklearn.neighbors import kneighbors_graph
import pylab as plt
import numpy as np

colors = np.array(['red', 'blue', 'green', 'orange', 'black', 'yellow']*100)

# generate data
moons = datasets.make_moons(n_samples=1000, noise=.05)[0]
blobs = datasets.make_blobs(n_samples=1000, random_state=8)[0]

# algorithm 1: KMeans
kmeans = cluster.MiniBatchKMeans(n_clusters=2)
kmeans.fit(moons)
clusters_k = kmeans.predict(moons)

plt.scatter(moons[:, 0], moons[:, 1], s=10, 
            color=colors[clusters_k])
plt.show()

# algorithm 2: DBSCAN
dbscan = cluster.DBSCAN(eps=0.3)
dbscan.fit(moons)
clusters_db = dbscan.labels_.astype(np.int8)

plt.scatter(moons[:, 0], moons[:, 1], s=10, 
            color=colors[clusters_db])
plt.show()

Recap Questions

  • What are the strengths and weaknesses of K-Means, hierarchical clustering and DBScan?

  • How can you evaluate a clustering?

  • What is the curse of dimensionality?