k-means
scikit-learn
clustering
inertia
machine learning

How to get inertia value for each k-means cluster using scikit-learn?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

K-Means clustering is one of the most popular unsupervised machine learning algorithms used for partitioning data into clusters. The algorithm assigns each data point to one of `k` clusters based on distance to the cluster centroids, which are recalculated iteratively to minimize the within-cluster sum of squares. One of the key metrics used to evaluate the performance of the K-Means clustering is "inertia."

In inertia, you measure the sum of the squared distances between each data point and its assigned cluster centroid. Lower inertia indicates better compactness of clusters. In this article, we will discuss how to compute the inertia value for each K-Means cluster using the popular Python library `scikit-learn`.

What is Inertia?

Inertia is the measure of how well a dataset has been clustered by K-Means. It is also known as within-cluster sum of squares (WCSS). Inertia is calculated as:

Inertia=_i=1k_xC_ixμ_i2\text{Inertia} = \sum\_{i=1}^{k} \sum\_{x \in C\_i} |x - \mu\_i|^2

Where: • kk is the total number of clusters. • CiC_i represents the iith cluster. • xx is a data point in cluster CiC_i. • μi\mu_i is the centroid of cluster CiC_i. • 2\|\cdot\|^2 denotes the squared Euclidean distance.

Implementing K-Means and Computing Inertia in Scikit-learn

`Scikit-learn` is a powerful Python library which provides a simple interface to implement K-Means clustering. Here's a step-by-step guide to compute the inertia value using `scikit-learn`.

Installation

If you haven't already installed `scikit-learn`, you can do so using pip:

Elbow Method: Plot the inertia against various values of `k` and look for an "elbow" point where the rate of decrease sharply changes. • Silhouette Score: Measures how similar a point is to its cluster compared to other clusters. • Market Segmentation: Grouping customers based on buying habits. • Image Compression: Reducing the number of colors. • Anomaly Detection: Identifying unusual patterns. • Sensitivity to Initialization: Random initialization can lead to different results. • Non-linear Boundaries: K-Means assumes clusters are convex and isotropic. • Scaling: Features should be standardized or normalized.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.