Cophenetic Distances and Correlation from a Linkage Matrix

Quick Overview

Compute the cophenetic distance matrix from a SciPy-style linkage matrix, then the cophenetic correlation against the original distances, without calling SciPy's cophenet. It tests how hierarchical clustering merges are encoded, pair bookkeeping over a dendrogram and the edge cases of a Pearson correlation.

Cophenetic Distances and Correlation from a Linkage Matrix

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Given a SciPy-style linkage matrix `Z` of shape `(n-1, 4)`, implement `cophenetic_distances(Z)` returning the `n x n` matrix where entry `(i, j)` is the merge height at which observations `i` and `j` first join the same cluster. Also implement `cophenetic_correlation(Z, D)`: the Pearson correlation between the upper-triangle (excluding diagonal) entries of square distance matrix `D` and the cophenetic matrix, returned as a `float`. Example: ```python Z = [[0, 1, 1.0, 2], [2, 3, 2.0, 2], [4, 5, 5.0, 4]] cophenetic_distances(Z) # [[0, 1, 5, 5], # [1, 0, 5, 5], # [5, 5, 0, 2], # [5, 5, 2, 0]] Z = [[0, 1, 1.0, 2], [2, 3, 3.0, 3]] D = [[0, 1, 2], [1, 0, 4], [2, 4, 0]] cophenetic_correlation(Z, D) # sqrt(4/7) ~ 0.7559 ``` ```hint Replay the tree Process the rows of `Z` in order and keep track of which leaves belong to each cluster id. Ask which leaf pairs get their value at each merge. ``` ```hint Cluster ids A merged cluster gets a new id, and the indices in `Z` may be stored as floats. Check how you convert them and where you look them up. ``` ```hint The correlation Use each pair once and leave out the diagonal. Decide how you will detect a constant vector exactly, given floating-point means. ``` ### Constraints & Assumptions - Row `k` of `Z` merges clusters `Z[k,0]` and `Z[k,1]` at height `Z[k,2]` into new cluster id `n + k` (ids `< n` are leaves). Indices may be stored as floats. - The output matrix is symmetric with a zero diagonal and must match `scipy.cluster.hierarchy.cophenet` to `rtol=1e-7`. - `D` is a symmetric `n x n` matrix. If either vector has zero variance, return `float('nan')`. - 2 ≤ `n` ≤ 1000. Do not call `scipy.cluster.hierarchy.cophenet`. ### What a Strong Answer Covers - A correct reading of the linkage matrix: which clusters each row merges, the `n + k` numbering for new clusters, and float-stored indices. - Every leaf pair assigned exactly once, at the height where its two leaves first share a cluster, giving a symmetric matrix with a zero diagonal. - Pearson correlation over the strict upper triangle only, with zero variance detected reliably and returned as `nan`. - Time and space complexity, including the cost of tracking cluster members on a skewed tree. ### Follow-up Questions - What is the complexity of your approach, and can it beat O(n²)? - Why does single linkage guarantee cophenetic distance ≤ original distance? - How would you use the cophenetic correlation to choose a linkage method?

Overview: Compute the cophenetic distance matrix from a SciPy-style linkage matrix, then the cophenetic correlation against the original distances, without calling SciPy's cophenet. It tests how hierarchical clustering merges are encoded, pair bookkeeping over a dendrogram and the edge cases of a Pearson correlation.

PracHub logo
General
Nov 24, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Given a SciPy-style linkage matrix Z of shape (n-1, 4), implement cophenetic_distances(Z) returning the n x n matrix where entry (i, j) is the merge height at which observations i and j first join the same cluster. Also implement cophenetic_correlation(Z, D): the Pearson correlation between the upper-triangle (excluding diagonal) entries of square distance matrix D and the cophenetic matrix, returned as a float.

Example:

Z = [[0, 1, 1.0, 2], [2, 3, 2.0, 2], [4, 5, 5.0, 4]]
cophenetic_distances(Z)
# [[0, 1, 5, 5],
#  [1, 0, 5, 5],
#  [5, 5, 0, 2],
#  [5, 5, 2, 0]]

Z = [[0, 1, 1.0, 2], [2, 3, 3.0, 3]]
D = [[0, 1, 2], [1, 0, 4], [2, 4, 0]]
cophenetic_correlation(Z, D)  # sqrt(4/7) ~ 0.7559

Constraints & Assumptions

  • Row k of Z merges clusters Z[k,0] and Z[k,1] at height Z[k,2] into new cluster id n + k (ids < n are leaves). Indices may be stored as floats.
  • The output matrix is symmetric with a zero diagonal and must match scipy.cluster.hierarchy.cophenet to rtol=1e-7 .
  • D is a symmetric n x n matrix. If either vector has zero variance, return float('nan') .
  • 2 ≤ n ≤ 1000. Do not call scipy.cluster.hierarchy.cophenet .

What a Strong Answer Covers Guidance

  • A correct reading of the linkage matrix: which clusters each row merges, the n + k numbering for new clusters, and float-stored indices.
  • Every leaf pair assigned exactly once, at the height where its two leaves first share a cluster, giving a symmetric matrix with a zero diagonal.
  • Pearson correlation over the strict upper triangle only, with zero variance detected reliably and returned as nan .
  • Time and space complexity, including the cost of tracking cluster members on a skewed tree.

Follow-up Questions Guidance

  • What is the complexity of your approach, and can it beat O(n²)?
  • Why does single linkage guarantee cophenetic distance ≤ original distance?
  • How would you use the cophenetic correlation to choose a linkage method?