How to do Agglomerative Clustering in Python?

This recipe helps you do Agglomerative Clustering in Python

Recipe Objective

Have you ever tried to do Agglomerative Clustering in python? Clustering can give us an idea that how the data set is in groups.

So this is the recipe on how we can do Agglomerative Clustering in Python.

Hands-On Guide to the Art of Tuning Locality Sensitive Hashing in Python

Step 1 - Import the library

from sklearn import datasets from sklearn.preprocessing import StandardScaler from sklearn.cluster import AgglomerativeClustering import pandas as pd import seaborn as sns import matplotlib.pyplot as plt

We have imported datasets, StandardScaler, AgglomerativeClustering, pandas, and seaborn which will be needed for the dataset.

Step 2 - Setting up the Data

We have imported inbuilt iris dataset and stored data in x. We have plotted a heatmap for corelation of features. iris = datasets.load_iris() X = iris.data; data = pd.DataFrame(X) cor = data.corr() sns.heatmap(cor, square = True); plt.show()

Step 3 - Training model and Predicting Clusters

Here we we are first standarizing the data by standardscaler. scaler = StandardScaler() X_std = scaler.fit_transform(X) Now we are using AffinityPropagation for clustering with features:

  • linkage: It determines which distance to use between sets of observation. The algorithm will merge the pairs of cluster that minimize this criterion.
  • n_clusters: It is the number of clusters we want to have
  • affinity: In this we have to choose between euclidean, l1, l2 etc.

clt = AgglomerativeClustering(linkage="complete", affinity="euclidean", n_clusters=5) We are training the data by using clt.fit and printing the number of clusters. model = clt.fit(X_std) Finally we are predicting the clusters. clusters = pd.DataFrame(model.fit_predict(X_std)) data["Cluster"] = clusters

Step 4 - Visualizing the output

fig = plt.figure(); ax = fig.add_subplot(111) scatter = ax.scatter(data[0],data[1], c=data["Cluster"],s=50) ax.set_title("Agglomerative Clustering") ax.set_xlabel("X0"); ax.set_ylabel("X1") plt.colorbar(scatter); plt.show()

We have plot a sactter plot which will show the clusters of data in different colour.

 

Download Materials

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification