Hierarchical Clustering
Learn all about Hierarchical Clustering in this comprehensive tutorial.
- •Hierarchical clustering is an unsupervised learning method for clustering data points.
- •We will use Agglomerative Clustering, a type of hierarchical clustering that follows a bottom up approach.
- •Import the modules you need.
Hierarchical Clustering
Hierarchical clustering is an unsupervised learning method for clustering data points. The algorithm builds clusters by measuring the dissimilarities between data. Unsupervised learning means that a model does not have to be trained, and we do not need a "target" variable. This method can be used on any data to visualize and interpret the relationship between individual data points.
Here we will use hierarchical clustering to group data points and visualize the clusters using both a dendrogram and scatter plot.
How does it work?
We will use Agglomerative Clustering, a type of hierarchical clustering that follows a bottom up approach. We begin by treating each data point as its own cluster. Then, we join clusters together that have the shortest distance between them to create larger clusters. This step is repeated until one large cluster is formed containing all of the data points.
Hierarchical clustering requires us to decide on both a distance and linkage method. We will use euclidean distance and the Ward linkage method, which attempts to minimize the variance between clusters.
Now we compute the ward linkage using euclidean distance, and visualize it using a dendrogram:
Example Explained
Import the modules you need.
You can learn about the Matplotlib module in our "Matplotlib Tutorial.
You can learn about the SciPy module in our SciPy Tutorial.
NumPy is a library for working with arrays and matricies in Python, you can learn about the NumPy module in our NumPy Tutorial.
scikit-learn is a popular library for machine learning.
Create arrays that resemble two variables in a dataset. Note that while we only use two variables here, this method will work with any number of variables:
Turn the data into a set of points:
Result:
Compute the linkage between all of the different points. Here we use a simple euclidean distance measure and Ward's linkage, which seeks to minimize the variance between clusters.
Finally, plot the results in a dendrogram. This plot will show us the hierarchy of clusters from the bottom (individual points) to the top (a single cluster consisting of all data points).
plt.show() lets us visualize the dendrogram instead of just the raw linkage data.
Result:
The scikit-learn library allows us to use hierarchichal clustering in a different manner. First, we initialize the AgglomerativeClustering class with 2 clusters and the Ward linkage.
The .fit_predict method can be called on our data to compute the clusters using the defined parameters across our chosen number of clusters.
Result:
Finally, if we plot the same data and color the points using the labels assigned to each index by the hierarchical clustering method, we can see the cluster each point was assigned to:
Result:
Module quiz
2 questionsWhich of the following is true about Hierarchical Clustering?
What is the most common pitfall when working with Hierarchical Clustering?
Answer all questions to submit.