Prune Correlated Ensemble Members by Spearman Rank Correlation

Quick Overview

Compute a Spearman rank correlation matrix for ensemble member predictions with average ranks for ties, then greedily prune members that correlate too strongly with better-scoring ones. It tests rank-based correlation, tie handling, greedy selection order and edge cases such as constant predictions.

Prune Correlated Ensemble Members by Spearman Rank Correlation

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `spearman_correlation_matrix(predictions)`, which returns the `m x m` Spearman correlation matrix for a `members x samples` array, and `prune_correlated_models(predictions, scores, threshold, greater_is_better=True)`, which greedily removes redundant members and returns the kept indices as a sorted list. Requirements: - Rank ties get average ranks (the result must match `scipy.stats.spearmanr`). The diagonal is `1.0`. A constant row has correlation `0.0` with every other member. - Visit members best score first (descending if `greater_is_better`, else ascending). Score ties go to the lower index. - Keep a member only if its signed correlation with every already-kept member is `<= threshold` (prune on strictly greater). Pruned members never block later ones. - Raise `ValueError` if `predictions` is not 2-D or `len(scores)` differs from the member count. Example: ```python preds = [[1, 2, 3, 4], [10, 20, 30, 40], # rho(0,1) = 1.0 [4, 3, 2, 1]] # rho(2,*) = -1.0 prune_correlated_models(preds, [0.8, 0.9, 0.7], 0.95) # -> [1, 2] (member 0 pruned by better-scoring member 1) prune_correlated_models(preds, [0.8, 0.9, 0.7], 0.95, greater_is_better=False) # -> [0, 2] ``` Members 0 and 1 rank the samples identically, and member 2 reverses that order. With `greater_is_better=False` the visiting order is 2, 0, 1, so member 1 is the one dropped. ```hint Ranking Spearman correlation is Pearson correlation computed on ranks, so most of the work is a ranking function that treats ties the way `scipy.stats.spearmanr` does. ``` ```hint Rounding at the boundary Two identical members should have correlation exactly 1.0. Check what floating-point rounding in your formula can do to that value, and what it means for a call with `threshold=1.0`. ``` ### Constraints & Assumptions - `predictions` is array-like with shape `(members, samples)` and numeric values. - `scores` has one value per member, and `threshold` is a correlation value, normally in `[-1, 1]`. - `scipy.stats.spearmanr` is the reference for the correlation values. ### What a Strong Answer Covers - Uses average ranks for ties and computes correlations that agree with `scipy.stats.spearmanr`, including the constant-row and diagonal rules. - Orders members by score in the requested direction with a deterministic rule for score ties. - Compares each candidate only against members already kept, using the signed correlation and the strict `>` pruning rule. - Validates the input shapes and raises `ValueError` as specified. - States the time and space cost and how the matrix step grows with the number of members. ### Follow-up Questions - Should strongly negatively correlated members also be pruned? - How would you scale this to thousands of members? - What are the weaknesses of greedy correlation pruning?

Overview: Compute a Spearman rank correlation matrix for ensemble member predictions with average ranks for ties, then greedily prune members that correlate too strongly with better-scoring ones. It tests rank-based correlation, tie handling, greedy selection order and edge cases such as constant predictions.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement spearman_correlation_matrix(predictions), which returns the m x m Spearman correlation matrix for a members x samples array, and prune_correlated_models(predictions, scores, threshold, greater_is_better=True), which greedily removes redundant members and returns the kept indices as a sorted list.

Requirements:

  • Rank ties get average ranks (the result must match scipy.stats.spearmanr ). The diagonal is 1.0 . A constant row has correlation 0.0 with every other member.
  • Visit members best score first (descending if greater_is_better , else ascending). Score ties go to the lower index.
  • Keep a member only if its signed correlation with every already-kept member is <= threshold (prune on strictly greater). Pruned members never block later ones.
  • Raise ValueError if predictions is not 2-D or len(scores) differs from the member count.

Example:

preds = [[1, 2, 3, 4],
         [10, 20, 30, 40],   # rho(0,1) = 1.0
         [4, 3, 2, 1]]       # rho(2,*) = -1.0
prune_correlated_models(preds, [0.8, 0.9, 0.7], 0.95)
# -> [1, 2]   (member 0 pruned by better-scoring member 1)
prune_correlated_models(preds, [0.8, 0.9, 0.7], 0.95, greater_is_better=False)
# -> [0, 2]

Members 0 and 1 rank the samples identically, and member 2 reverses that order. With greater_is_better=False the visiting order is 2, 0, 1, so member 1 is the one dropped.

Constraints & Assumptions

  • predictions is array-like with shape (members, samples) and numeric values.
  • scores has one value per member, and threshold is a correlation value, normally in [-1, 1] .
  • scipy.stats.spearmanr is the reference for the correlation values.

What a Strong Answer Covers Guidance

  • Uses average ranks for ties and computes correlations that agree with scipy.stats.spearmanr , including the constant-row and diagonal rules.
  • Orders members by score in the requested direction with a deterministic rule for score ties.
  • Compares each candidate only against members already kept, using the signed correlation and the strict > pruning rule.
  • Validates the input shapes and raises ValueError as specified.
  • States the time and space cost and how the matrix step grows with the number of members.

Follow-up Questions Guidance

  • Should strongly negatively correlated members also be pruned?
  • How would you scale this to thousands of members?
  • What are the weaknesses of greedy correlation pruning?