Editor

Predict the Inertia Pattern

Advanced · Predict the output · from K-Means Clustering: Find Patterns in Data Without Labels

From the lesson: K-Means Clustering: Find Patterns in Data Without Labels

A streaming service groups users into "action fans," "comedy lovers," and "documentary watchers" - without anyone filling out a survey. A retailer segments customers into "big spenders," "bargain hunters," and "window shoppers" based on purchase behavior. Nobody labeled these groups. The algorithm discovered them.

Read the lesson

This code compares inertia for K=1 vs K=3. Will K=3 have higher or lower inertia than K=1? What will the comparison print?

from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
X, _ = make_blobs(n_samples=150, centers=3,
cluster_std=1.0, random_state=42)
km1 = KMeans(n_clusters=1, random_state=42, n_init=10).fit(X)
km3 = KMeans(n_clusters=3, random_state=42, n_init=10).fit(X)
print(km1.inertia_ > km3.inertia_)

More K-Means Clustering exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz