Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Clustering and Discovery lessons
Hierarchical discovery · ML-U07 · 20–30 MIN

Hierarchical merging

Interpret Ward linkage and merge height.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferCompute and inspect the evidence
  4. ApplyIntegrate the methods taught so far

Understand the idea

Agglomerative clustering begins with individual observations and repeatedly merges groups. Ward chooses merges based on the increase in within-cluster variation. Dendrogram height represents its merge distance, not probability.

Illustrative data

Illustrative data
ColumnValues
length_mm10, 11, 12, 25, 26, 40, 41, 42
width_cm1, 1.3, 0.7, 6, 6.2, 2, 2.4, 1.7

Click the highlighted code to change the result.

coordinates = example[["length_mm", "width_cm"]]
linkage_matrix = linkage(coordinates, method=)
Original coordinates: final Ward merge distance 46.904. Scaling changes the distance calculation.Ward distance · original coordinates46.90w8w6w7w4w5w3w1w2

Original coordinates: final Ward merge distance 46.904. Scaling changes the distance calculation.

What the syntax does

linkage(scaled, method='ward')
  • Builds a hierarchy by repeatedly joining the pair with the smallest increase in within-group variation.
  • method: "single", "complete", "average", "weighted", "centroid", "median", "ward"
  • Use Euclidean measurement geometry for Ward. method=ward chooses this merge rule; other methods use different distances between groups.
  • Sampling and scale change the represented population and merges.
linkage_matrix[:, 2]
  • The third linkage column stores each merge distance; rows follow merge order.

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)
linkage_matrix
Ward linkage matrix of scaled_sample.
linkage_matrix = linkage(scaled, method='ward')

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. Store the supplied Ward linkage matrix for scaled CLUSTER36 in answer.
answer
Hints

Think: A linkage matrix records merges rather than one final assignment per row.

Tools: The supplied Ward linkage_matrix.

Approach: Inspect and return the prepared merge record for the scaled population.

Explained solution
answer=linkage_matrix

Each merge row represents the hierarchical construction; a later cut is needed to obtain a chosen grouping.

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.