Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Clustering and Discovery lessons
K-Means · ML-U03 · 25–40 MIN

K-Means assigns points to centroids

Understand centroids, distances and assignments.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferAdapt the Python
  4. ApplyBuild a guided full workflow

Understand the idea

K-Means alternates assignments and centroid updates to reduce within-cluster squared distances. Production uses multiple initialisations and a reproducible seed. k=3 is a starting choice, not a discovered truth.

Illustrative data

Illustrative data
ColumnValues
x1, 1.3, 0.7, 1.2, 5, 5.3, 4.7, 5.2, 3, 3.4, 2.6, 3.1
y1, 0.7, 1.2, 1.4, 1, 0.7, 1.3, 1.4, 5, 4.8, 5.3, 5.5

Click the highlighted code to change the result.

scaled = StandardScaler().fit_transform(example[["x", "y"]])
example_model = KMeans(n_clusters=, n_init=20, random_state=42)
example_model.fit(scaled)
centres = example_model.cluster_centers_
3 fitted groups; seed 42, n_init=20; inertia 0.503, silhouette 0.864. Crosses mark the actual fitted centroids. The same 12 observations and fitted scale are used for every k. Group IDs and shapes identify this fit; they are not known classes.-2-20022Standardised xScaled yg0g1g2

3 groups · inertia 0.503 · silhouette 0.864; crosses are fitted centroids. Crosses mark centroids; group IDs are not known classes.

What the syntax does

KMeans(n_clusters=3, n_init=20, random_state=42)
  • Creates a K-Means model that groups observations around fitted centres.
  • n_clusters: 2, 3, 4
  • n_init: "auto", 10, 20
  • random_state: 42, None
  • n_clusters sets a positive group count; n_init=20 tries twenty initialisations.
  • Scaling defines the distance.
  • Group IDs are arbitrary and silhouette measures geometry, not truth.
model.cluster_centers_
  • Stores fitted centroids in the scaled coordinate system.

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.
CLUSTER36 · first 8 rows
length_mmwidth_cm
1106.656.36006
1262.6613.292
317.1385.44237
1044.748.89315
994.127.01435
1307.7912.7223
1023.1113.9453
1163.636.99248
Your task
  1. Fit three K-Means clusters to scaled CLUSTER36.
  2. Store the fitted centroid coordinates in answer.
answer
Hints

Think: Centroids live in the same coordinate system used for fitting.

Tools: KMeans, n_clusters, n_init and cluster_centers_.

Approach: Fit the declared reproducible three-centroid model to scaled values and inspect its centres.

Explained solution
model=KMeans(n_clusters=3,n_init=20,random_state=42).fit(scaled)
answer=model.cluster_centers_

Multiple starts reduce dependence on a single initialisation; the resulting centres are in standardised units.

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.