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.
What the syntax does
model.inertia_- Reads the fitted sum of squared distances to assigned centroids; it tends to decrease as k grows.
Supplied for this practice
These objects are prepared afresh before your editor runs.
X- Feature dataframe for the declared population, preserving row indices.
X = df.copy() scaler- Already fitted coordinate system learned from
X; transform reuses its statistics without fitting again. Fitted onX; reuse without refitting. scaled- Standardised features in the same row/column order as
X.scaled = scaler.fit_transform(X)
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
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 |
- Fit k=3 and report inertia.
answerHints
Think: Inertia is a within-cluster squared-distance objective.
Tools: The fitted KMeans.inertia_ attribute.
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.