Basics of hierarchical
clustering
CLUS TERIN G METH ODS W ITH S CIP Y
Shaumik Daityari
Business Analyst
Creating a distance matrix using linkage
[Link](observations,
method='single',
metric='euclidean',
optimal_ordering=False
)
method : how to calculate the proximity of clusters
metric : distance metric
optimal_ordering : order data points
CLUSTERING METHODS WITH SCIPY
Which method should use?
single: based on two closest objects
complete: based on two farthest objects
average: based on the arithmetic mean of all objects
centroid: based on the geometric mean of all objects
median: based on the median of all objects
ward: based on the sum of squares
CLUSTERING METHODS WITH SCIPY
Create cluster labels with fcluster
[Link](distance_matrix,
num_clusters,
criterion
)
distance_matrix : output of linkage() method
num_clusters : number of clusters
criterion : how to decide thresholds to form clusters
CLUSTERING METHODS WITH SCIPY
Hierarchical clustering with ward method
CLUSTERING METHODS WITH SCIPY
Hierarchical clustering with single method
CLUSTERING METHODS WITH SCIPY
Hierarchical clustering with complete method
CLUSTERING METHODS WITH SCIPY
Final thoughts on selecting a method
No one right method for all
Need to carefully understand the distribution of data
CLUSTERING METHODS WITH SCIPY
Let's try some
exercises
CLUS TERIN G METH ODS W ITH S CIP Y
Visualize clusters
CLUS TERIN G METH ODS W ITH S CIP Y
Shaumik Daityari
Business Analyst
Why visualize clusters?
Try to make sense of the clusters formed
An additional step in validation of clusters
Spot trends in data
CLUSTERING METHODS WITH SCIPY
An introduction to seaborn
seaborn : a Python data visualization library based on matplotlib
Has better, easily modi able aesthetics than matplotlib!
Contains functions that make data visualization tasks easy in the context of data analytics
Use case for clustering: hue parameter for plots
CLUSTERING METHODS WITH SCIPY
Visualize clusters with matplotlib
from matplotlib import pyplot as plt
df = [Link]({'x': [2, 3, 5, 6, 2],
'y': [1, 1, 5, 5, 2],
'labels': ['A', 'A', 'B', 'B', 'A']})
colors = {'A':'red', 'B':'blue'}
[Link](x='x',
y='y',
c=df['labels'].apply(lambda x: colors[x]))
[Link]()
CLUSTERING METHODS WITH SCIPY
Visualize clusters with seaborn
from matplotlib import pyplot as plt
import seaborn as sns
df = [Link]({'x': [2, 3, 5, 6, 2],
'y': [1, 1, 5, 5, 2],
'labels': ['A', 'A', 'B', 'B', 'A']})
[Link](x='x',
y='y',
hue='labels',
data=df)
[Link]()
CLUSTERING METHODS WITH SCIPY
Comparison of both methods of visualization
MATPLOTLIB PLOT SEABORN PLOT
CLUSTERING METHODS WITH SCIPY
Next up: Try some
visualizations
CLUS TERIN G METH ODS W ITH S CIP Y
How many clusters?
CLUS TERIN G METH ODS W ITH S CIP Y
Shaumik Daityari
Business Analyst
Introduction to dendrograms
Strategy till now - decide clusters on visual
inspection
Dendrograms help in showing progressions as
clusters are merged
A dendrogram is a branching diagram that
demonstrates how each cluster is composed by
branching out into its child nodes
CLUSTERING METHODS WITH SCIPY
Create a dendrogram in SciPy
from [Link] import dendrogram
Z = linkage(df[['x_whiten', 'y_whiten']],
method='ward',
metric='euclidean')
dn = dendrogram(Z)
[Link]()
CLUSTERING METHODS WITH SCIPY
CLUSTERING METHODS WITH SCIPY
CLUSTERING METHODS WITH SCIPY
CLUSTERING METHODS WITH SCIPY
CLUSTERING METHODS WITH SCIPY
CLUSTERING METHODS WITH SCIPY
Next up - try some
exercises
CLUS TERIN G METH ODS W ITH S CIP Y
Limitations of
hierarchical
clustering
CLUS TERIN G METH ODS W ITH S CIP Y
Shaumik Daityari
Business Analyst
Measuring speed in hierarchical clustering
timeit module
Measure the speed of .linkage() method
Use randomly generated points
Run various iterations to extrapolate
CLUSTERING METHODS WITH SCIPY
Use of timeit module
from [Link] import linkage
import pandas as pd
import random, timeit
points = 100
df = [Link]({'x': [Link](range(0, points), points),
'y': [Link](range(0, points), points)})
%timeit linkage(df[['x', 'y']], method = 'ward', metric = 'euclidean')
1.02 ms ± 133 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
CLUSTERING METHODS WITH SCIPY
Comparison of runtime of linkage method
Increasing runtime with data points
Quadratic increase of runtime
Not feasible for large datasets
CLUSTERING METHODS WITH SCIPY
Next up - exercises
CLUS TERIN G METH ODS W ITH S CIP Y