Understand the idea
Different starting centres can find different local solutions. Multiple starts help, but do not remove the preference for compact centroid-based groups or resolve the meaning of k.
Illustrative data
| Column | Values |
|---|---|
x | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 |
y | -0.1, 0.25, 0.5, 1.45, 2.3, 3.85, 5.3, 7.45, 9.5, 12.249999999999998, 14.9, 18.25 |
Click the highlighted code to change the result.
scaled = StandardScaler().fit_transform(example[["x", "y"]])
example_model = KMeans(n_clusters=4, n_init=, random_state=)
example_model.fit(scaled)
inertia = example_model.inertia_Seed 2 · four groups, one start · inertia 1.538. Crosses mark centroids; group IDs are not known classes.
What the syntax does
n_init=1- Uses a single initialisation so seed sensitivity is visible in this exercise.
n_init:"auto",10,20
model.inertia_- Records each fit’s compactness so instability can be compared.
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 |
|---|---|
| 5820.15 | 45.332 |
| 2911.98 | 36.4235 |
| 3023.77 | 37.199 |
| 3616.25 | 38.4231 |
| 4037.74 | 41.4146 |
| 4313.07 | 43.8337 |
| 5331.02 | 48.9987 |
| 3962.73 | 38.9743 |
- On CLUSTER45_ANISO’s unequal-size elongated clouds
- Fit four-cluster K-Means with one initialisation for each
seed1, 2 and 3. - Store each inertia in
answerinseedorder.
answerHints
Think: Different initial centres can lead to different local solutions.
Tools: KMeans with n_init=1, random_state and inertia_.
Approach: Keep k and data fixed while fitting each declared seed, then compare the objectives.
Explained solution
answer=[]
for seed in [1,2,3]:
model=KMeans(n_clusters=4,n_init=1,random_state=seed).fit(scaled)
answer.append(model.inertia_)
One start exposes initialisation sensitivity that a multi-start run is designed to reduce.