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
| Column | Values |
|---|---|
length_mm | 10, 11, 12, 25, 26, 40, 41, 42 |
width_cm | 1, 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.
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 onX; 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- 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 |
- Store the supplied Ward linkage matrix for
scaledCLUSTER36 inanswer.
answerHints
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.