Understand the idea
Inertia decreases as more centres are added, so its minimum alone does not select k. Silhouette describes separation relative to cohesion. Neither proves a single natural grouping.
Python skill: Reads the fitted sum of squared distances to assigned centroids; it tends to decrease as k grows.
Meet the syntax
model.inertia_
silhouette_score(scaled, labels)model.inertia_- Reads the fitted sum of squared distances to assigned centroids; it tends to decrease as k grows.
silhouette_score(scaled, labels)- Summarises separation relative to within-group distances for this scaled geometry.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
model=KMeans(n_clusters=3,n_init=20,random_state=42).fit(scaled)
answer=model.inertia_
This practice: Read and run the Python. Next: Change · Choosing k requires evidence and judgement.
Given data · CLUSTER36
36 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.
Reference labels are omitted from this preview and must remain outside fitting.
| length_mm | width_cm |
|---|---|
| 1106.65 | 6.36006 |
| 1262.66 | 13.292 |
| 317.138 | 5.44237 |
| 1044.74 | 8.89315 |
| 994.12 | 7.01435 |
| 1307.79 | 12.7223 |
| 1023.11 | 13.9453 |
| 1163.63 | 6.99248 |
Column meanings and units
Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.
Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.
| Column | Stored type |
|---|---|
| length_mm | float64 |
| width_cm | float64 |
Supplied setup · available if you need to inspect it
This code runs before your editor on every Run. These are the objects your exercise uses.
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
X=df.copy()
scaler=StandardScaler()
scaled=scaler.fit_transform(X)
Your task · Follow
Fit k=3 and report inertia.
Hint 1 — Think
Inertia is a within-cluster squared-distance objective.
Hint 2 — Tools
The fitted KMeans.inertia_ attribute.
Hint 3 — Approach
Fit the declared k on the supplied scaled population and read its objective value.
Explained solution
model=KMeans(n_clusters=3,n_init=20,random_state=42).fit(scaled)
answer=model.inertia_
The value summarises compactness for this representation and population; it is not supervised accuracy.
Helpful prior knowledge: K-Means assigns points to centroids These links are guidance, not locks.
Sources and API context
Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.