How to Build a Hierarchical Cluster Model in Sklearn

03.17.2021

Intro

Another algorithm that is an alternative to the generic k-means cluster model is the Hierarchical Cluster Model. This algorithm will group data based on hierarchy. In this article, we will learn how to build a Hierarchical Cluster Model in Sklearn.

Building a Hierarchical Cluster Model

To build a Hierarchical Cluster Model, we will use the AgglomerativeClustering class from the cluster. Then, as usual, we will create an instance of the AgglomerativeClustering class and pass our features and target to the fit method to train our model.

from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import AgglomerativeClustering

iris = datasets.load_iris()
features = iris.data

scaler = StandardScaler()
features_std = scaler.fit_transform(features)

agglom = AgglomerativeClustering()

model = agglom.fit(features_std)