How to get the inertia of each k-means cluster in scikit-learn
Codemia Knowledge Hub ·
Inertia is the sum of the squared distances between each data point and the centroid of its assigned cluster. Lower inertia means more compact clusters. K-means itself works by assigning each point to one of k clusters by distance to the centroids, then recalculating the centroids iteratively to minimize this within-cluster sum of squares.
The inertia formula
Inertia is also known as the within-cluster sum of squares (WCSS):
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.
The per-cluster value is the inner sum for a single cluster .
Computing inertia with scikit-learn
scikit-learn provides a simple interface for K-Means clustering.
Installation
If you have not installed scikit-learn yet, you can do so with pip:
Total and per-cluster inertia
After fitting, KMeans stores the total inertia in inertia_. There is no attribute for each cluster's share, but you can compute it from labels_ and cluster_centers_:
The per-cluster values add up to inertia_.
Choosing k
• 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.
Where K-Means is used
• Market Segmentation: grouping customers based on buying habits. • Image Compression: reducing the number of colors. • Anomaly Detection: identifying unusual patterns.
Limitations of K-Means
• 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
System design interview problems on this topic
- Design an E-commerce Recommendation System: solution and walkthrough
- Design an LLM Inference Service: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough
- Design ChatGPT: solution and walkthrough
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.