Ward's Linkage Merge Cost

Quick Overview

Compute the within-cluster sum of squares, the increase in it when two clusters merge, and the matching Ward height used by scipy's hierarchical linkage. It tests the algebra behind Ward's criterion, the height scale used by clustering libraries and numerically careful NumPy code.

Ward's Linkage Merge Cost

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Given clusters as 2D arrays of shape `(n, d)`, implement three functions. `wcss(X)` returns the sum of squared Euclidean distances of rows to their centroid. `ward_merge_cost(A, B)` returns the increase in WCSS from merging `A` and `B`, and `ward_linkage_distance(A, B)` returns that cost on scipy's `linkage(method="ward")` height scale: `sqrt(2 * merge_cost)`. Example: ```python wcss([[0,0],[2,0],[1,3]]) # 8.0 A = [[0,0],[2,0]]; B = [[10,0]] ward_merge_cost(A, B) # 54.0 ward_linkage_distance(A, B) # sqrt(108) ≈ 10.392 ward_linkage_distance([[1,-2,4]], [[4,2,4]]) # 5.0 (singletons → Euclidean) ``` ```hint Avoid building the union Write the centroid of the merged cluster as a size-weighted average of the two centroids, then expand the squared deviations of each original cluster about that point. ``` ### Constraints & Assumptions - Inputs are array-likes convertible to float, each with at least one row. - `A` and `B` have the same number of features `d`. - All functions return a Python `float`. Results must match to `rtol=1e-7`. ### What a Strong Answer Covers - Computes WCSS by centring the rows on their column means and summing the squared deviations. - Gives a closed form for the merge cost in terms of cluster sizes and centroids, and shows where it comes from. - Discusses whether to compute the cost by building the union and subtracting WCSS values, and what that does to time and floating-point precision. - Connects the merge cost to SciPy's height scale and checks it on the singleton example. - Validates the inputs (at least one row, matching feature counts) and returns Python floats. ### Follow-up Questions - Why does scipy's Ward height include the factor of 2 and the square root? - How would you update Ward distances after a merge without recomputing centroids, and what does a full agglomeration cost? - What does the merge cost formula imply about how Ward linkage treats a large cluster compared with a small one?

Overview: Compute the within-cluster sum of squares, the increase in it when two clusters merge, and the matching Ward height used by scipy's hierarchical linkage. It tests the algebra behind Ward's criterion, the height scale used by clustering libraries and numerically careful NumPy code.

PracHub logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Given clusters as 2D arrays of shape (n, d), implement three functions. wcss(X) returns the sum of squared Euclidean distances of rows to their centroid. ward_merge_cost(A, B) returns the increase in WCSS from merging A and B, and ward_linkage_distance(A, B) returns that cost on scipy's linkage(method="ward") height scale: sqrt(2 * merge_cost).

Example:

wcss([[0,0],[2,0],[1,3]])           # 8.0
A = [[0,0],[2,0]]; B = [[10,0]]
ward_merge_cost(A, B)              # 54.0
ward_linkage_distance(A, B)        # sqrt(108) ≈ 10.392
ward_linkage_distance([[1,-2,4]], [[4,2,4]])  # 5.0 (singletons → Euclidean)

Constraints & Assumptions

  • Inputs are array-likes convertible to float, each with at least one row.
  • A and B have the same number of features d .
  • All functions return a Python float . Results must match to rtol=1e-7 .

What a Strong Answer Covers Guidance

  • Computes WCSS by centring the rows on their column means and summing the squared deviations.
  • Gives a closed form for the merge cost in terms of cluster sizes and centroids, and shows where it comes from.
  • Discusses whether to compute the cost by building the union and subtracting WCSS values, and what that does to time and floating-point precision.
  • Connects the merge cost to SciPy's height scale and checks it on the singleton example.
  • Validates the inputs (at least one row, matching feature counts) and returns Python floats.

Follow-up Questions Guidance

  • Why does scipy's Ward height include the factor of 2 and the square root?
  • How would you update Ward distances after a merge without recomputing centroids, and what does a full agglomeration cost?
  • What does the merge cost formula imply about how Ward linkage treats a large cluster compared with a small one?