Hierarchical Clustering Dendrogram

Machine LearningMachine LearningBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to plot the corresponding dendrogram of a hierarchical clustering using AgglomerativeClustering and the dendrogram method available in scipy.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("`Sklearn`")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["`Core Models and Algorithms`"]) sklearn(("`Sklearn`")) -.-> sklearn/UtilitiesandDatasetsGroup(["`Utilities and Datasets`"]) ml(("`Machine Learning`")) -.-> ml/FrameworkandSoftwareGroup(["`Framework and Software`"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/cluster("`Clustering`") sklearn/UtilitiesandDatasetsGroup -.-> sklearn/datasets("`Datasets`") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("`scikit-learn`") subgraph Lab Skills sklearn/cluster -.-> lab-49063{{"`Hierarchical Clustering Dendrogram`"}} sklearn/datasets -.-> lab-49063{{"`Hierarchical Clustering Dendrogram`"}} ml/sklearn -.-> lab-49063{{"`Hierarchical Clustering Dendrogram`"}} end

Import the necessary libraries

We will start by importing the necessary libraries for this lab.

import numpy as np
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram
from sklearn.datasets import load_iris
from sklearn.cluster import AgglomerativeClustering

Load the dataset

We will use the load_iris() function from the sklearn.datasets module to load the iris dataset.

iris = load_iris()
X = iris.data

Create the model

Next, we will create the agglomerative clustering model using the AgglomerativeClustering() function from the sklearn.cluster module.

model = AgglomerativeClustering(distance_threshold=0, n_clusters=None)

Fit the model

We will fit the agglomerative clustering model using the fit() method of the model object.

model = model.fit(X)

Plot the dendrogram

We will plot the dendrogram using the dendrogram() function from the scipy.cluster.hierarchy module and the plot_dendrogram() function defined in the original code.

plt.title("Hierarchical Clustering Dendrogram")
plot_dendrogram(model, truncate_mode="level", p=3)
plt.xlabel("Number of points in node (or index of point if no parenthesis).")
plt.show()

Summary

In this lab, we learned how to plot the corresponding dendrogram of a hierarchical clustering using AgglomerativeClustering and the dendrogram method available in scipy. We loaded the iris dataset, created an agglomerative clustering model, and fit the model. Finally, we plotted the dendrogram using the dendrogram() function from the scipy.cluster.hierarchy module and the plot_dendrogram() function defined in the original code.

Other Machine Learning Tutorials you may like