Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Clustering and Discovery lessons
K-Means · ML-U06 · 12–18 MIN

When K-Means geometry misleads

Recognise initialisation, scale and shape limitations.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeCompute and inspect the evidence
  3. TransferCompute and inspect the evidence

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

Illustrative data
ColumnValues
x0, 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; n_init=1; four fitted groups; inertia 1.538. Each start fits the same observations; the lowest-inertia fit is kept. Crosses mark fitted centroids; group IDs identify this fit, not known classes.-2-20022Standardised xScaled yg0g1g2g3

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 on X; reuse without refitting.
scaled
Standardised features in the same row/column order as X.
scaled = scaler.fit_transform(X)

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
CLUSTER45_ANISO · first 8 rows
length_mmwidth_cm
5820.1545.332
2911.9836.4235
3023.7737.199
3616.2538.4231
4037.7441.4146
4313.0743.8337
5331.0248.9987
3962.7338.9743
Your task
  1. On CLUSTER45_ANISO’s unequal-size elongated clouds
  2. Fit four-cluster K-Means with one initialisation for each seed 1, 2 and 3.
  3. Store each inertia in answer in seed order.
answer
Hints

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.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.