Assessing Hierarchical Clustering Models with Scikit-learn Metrics

Introduction

Welcome to today's discussion on Hierarchical Clustering. We will be studying its effectiveness using the Silhouette Score, the Davies-Bouldin Index, and Cross-Tabulation Analysis. We will utilize Python's powerful libraries, scikit-learn and pandas, to equip you with practical and useful skills for evaluating clustering models.

Hierarchical Clustering and Scikit-learn Introduction

Scikit-learn is a widely used Python library for machine learning. In this lesson, we will be using its powerful built-in methods, including the silhouette_score and davies_bouldin_score. Additionally, we will implement Hierarchical Clustering from scikit-learn on some data:

from sklearn.cluster import AgglomerativeClustering

data = [(1.5, 1.7), (1.9, 2.4), (2.0, 1.9), (3.2, 3.2), (3.5, 3.9), (6.0, 6.5)]

clustering = AgglomerativeClustering().fit(data)

This function applies Hierarchical Clustering to our dataset. The formed cluster labels can be accessed via clustering.labels_.

Silhouette Score

The Silhouette Score offers a measure to evaluate the effectiveness of our clustering. This score gauges how similar a point is to its own cluster compared to other clusters. Higher scores indicate better clustering.

We will implement the silhouette_score function from the sklearn library on our data:

from sklearn.metrics import silhouette_score

s_score = silhouette_score(data, clustering.labels_)
print(f"Silhouette Score is: {s_score}")  # higher the better

The output provides a single score showing the effectiveness of our clustering.

Davies-Bouldin Index

The Davies-Bouldin index evaluates the average similarity between clusters. It bears an inverse relationship to model performance, meaning that a lower index value indicates a better model.

We will use the davies_bouldin_score function in sklearn as follows:

from sklearn.metrics import davies_bouldin_score

db_index = davies_bouldin_score(data, clustering.labels_)
print(f"Davies-Bouldin index is: {db_index}")

The Davies-Bouldin Index thus obtained serves as another measure of our clustering effectiveness.

Visualizing and Assessing Clustered Data

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal