Inertia
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 6 of 51.
This chapter is about choosing k. By its end, you will choose it for a bakery's deliveries and map the zones:
First, a score. Inertia adds up every point's squared distance to its own center: small when points hug their centers.
KMeans keeps it in inertia_. For the 180 apples:
for k in [1, 2, 3, 4]:
km = KMeans(n_clusters=k, n_init=10, random_state=0)
km.fit(X)
print(k, round(km.inertia_))This prints:
1 1718
2 752
3 230
4 196More centers, shorter spokes: inertia always shrinks as k grows. With one center per point it is 0, so smallest is not best.
Challenge
EasyA pottery packs vases into boxes made in a few sizes. Each vase takes the nearest size; inertia measures how badly they fit. The program reads budget, the most misfit the pottery accepts.
- Try k = 1, 2, 3 and so on: fit KMeans on
vasesand print each k's inertia, rounded. - Stop at the first k whose inertia is within budget, and print it.
For another pottery:
k = 1, inertia 15120
k = 2, inertia 4811
k = 3, inertia 1290
Box sizes needed: 3Try it yourself
import numpy as np
from sklearn.cluster import KMeans
# Don't change the lines below
rng = np.random.default_rng(8)
# Four kinds of vase: typical height and width in cm, and how many
kinds = [(15, 8, 25), (38, 13, 25), (22, 22, 20), (58, 26, 10)]
parts = []
for h, w, n in kinds:
parts.append(np.column_stack([rng.normal(h, 2.5, n), rng.normal(w, 2, n)]))
# 80 vases, one row each: height and width in cm
vases = np.round(np.concatenate(parts), 1)
# The most misfit, as inertia, the pottery accepts
budget = int(input())
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unsupervised Learning
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd OnesPractice on your own: Online Python compiler