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.
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:
Where: • is the total number of clusters. • represents the th cluster. • is a data point in cluster . • is the centroid of cluster . • 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
- How to get labels ids in Keras when training on multiple classes?
- How to get mini-batches in pytorch in a clean and efficient way?
- How to get most informative features for scikit-learn classifiers?
- How to get N numbers of data points which are nearest from a cluster's center?
- How to get output of hidden layer given an input, weights and biases of the hidden layer in keras?
- How to get PI in tensorflow?
- How to get reproducible result when running Keras with Tensorflow backend
- How to Get Reproducible Results Keras, Tensorflow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.