Menu
CoddyTech

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:

A map of a city's delivery stops, east in km across and north in meters up, split into three zones of 45, 40 and 25 stops, each with a black star at its middle

First, a score. Inertia adds up every point's squared distance to its own center: small when points hug their centers.

36 of the apples in three groups, each joined to its group's black star center by a thin gray spoke. Inertia squares every spoke's length and adds them all up

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 196

More centers, shorter spokes: inertia always shrinks as k grows. With one center per point it is 0, so smallest is not best.

challenge icon

Challenge

Easy

A 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.

  1. Try k = 1, 2, 3 and so on: fit KMeans on vases and print each k's inertia, rounded.
  2. 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: 3

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler