Implement a greedy correlation filter that drops redundant features

Quick Overview

Implement a selector with fit and transform methods that removes redundant features by greedily resolving highly correlated pairs, using correlation with the target to choose which feature to drop. It tests correlation math on constant columns, exact tie rules, input validation and fitted state.

Implement a greedy correlation filter that drops redundant features

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `CorrelationFilter(threshold=0.9)` with `fit(X, y)`, `transform(X)` and `fit_transform(X, y)`. `fit` greedily drops one member of each highly correlated feature pair. `transform` applies the saved column list to new data. The selector must follow these rules: - Use absolute Pearson correlation. Zero-variance columns get correlation `0` (never `NaN`). Pairs count only if `corr > threshold`. - Process pairs by descending correlation (ties: column position order). Skip a pair if either member is already dropped. Drop the member with the lower absolute correlation to the target. On a tie, drop the later column. - Set `feature_correlations_` (DataFrame), `target_correlations_` (Series), `selected_features_` (original column order) and `dropped_features_` (drop order). Return `self`. - A non-DataFrame `X` raises `TypeError`. A row count mismatch between `X` and `y` raises `ValueError`. Never mutate the inputs. - `transform` returns a copy of the selected columns in fit order, preserving the index. A missing column raises `KeyError`. Calling it before `fit` raises `RuntimeError`. For example: ```python X = pd.DataFrame({"f1": [1, 2, 3, 4, 5], "f2": [1, 2, 3, 4, 6], "f3": [2, 1, 2, 1, 2]}) y = [1, 2, 3, 4, 6] sel = CorrelationFilter(threshold=0.9).fit(X, y) sel.selected_features_ # ['f2', 'f3'] sel.dropped_features_ # ['f1'] (f2 matches y exactly) ``` Only `f1` and `f2` are correlated above 0.9. Since `f2` equals `y`, it has the higher target correlation, so `f1` is the one dropped. ```hint Computing every correlation at once Center the columns and the target first. A single matrix product of the centered data then gives every covariance, and the column norms turn those into correlations. Think about what that division does for a constant column. ``` ```hint Detecting a constant column A column of identical float values does not always center to exact zeros, because the computed mean can be off in the last bit. Decide how to recognize a constant column without relying on its centered norm. ``` ### What a Strong Answer Covers - Computing absolute Pearson correlations between features and against the target without producing `NaN` for constant columns, and without letting a constant float column look correlated. - Building the candidate pair list with the strict threshold and the specified ordering, and skipping pairs whose members are already dropped. - Applying the drop rule and the tie rule exactly as stated. - Input validation (`TypeError`, `ValueError`, `KeyError`, `RuntimeError`) and leaving `X` and `y` unmodified. - Time and memory cost in terms of the number of rows and features. ### Follow-up Questions - Why is greedy pair-wise dropping order-dependent, and what are the alternatives? - How would you scale this to tens of thousands of features? - When is Pearson correlation the wrong measure here?

Overview: Implement a selector with fit and transform methods that removes redundant features by greedily resolving highly correlated pairs, using correlation with the target to choose which feature to drop. It tests correlation math on constant columns, exact tie rules, input validation and fitted state.

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

Implement CorrelationFilter(threshold=0.9) with fit(X, y), transform(X) and fit_transform(X, y). fit greedily drops one member of each highly correlated feature pair. transform applies the saved column list to new data.

The selector must follow these rules:

  • Use absolute Pearson correlation. Zero-variance columns get correlation 0 (never NaN ). Pairs count only if corr > threshold .
  • Process pairs by descending correlation (ties: column position order). Skip a pair if either member is already dropped. Drop the member with the lower absolute correlation to the target. On a tie, drop the later column.
  • Set feature_correlations_ (DataFrame), target_correlations_ (Series), selected_features_ (original column order) and dropped_features_ (drop order). Return self .
  • A non-DataFrame X raises TypeError . A row count mismatch between X and y raises ValueError . Never mutate the inputs.
  • transform returns a copy of the selected columns in fit order, preserving the index. A missing column raises KeyError . Calling it before fit raises RuntimeError .

For example:

X = pd.DataFrame({"f1": [1, 2, 3, 4, 5], "f2": [1, 2, 3, 4, 6], "f3": [2, 1, 2, 1, 2]})
y = [1, 2, 3, 4, 6]
sel = CorrelationFilter(threshold=0.9).fit(X, y)
sel.selected_features_  # ['f2', 'f3']
sel.dropped_features_   # ['f1']  (f2 matches y exactly)

Only f1 and f2 are correlated above 0.9. Since f2 equals y, it has the higher target correlation, so f1 is the one dropped.

What a Strong Answer Covers Guidance

  • Computing absolute Pearson correlations between features and against the target without producing NaN for constant columns, and without letting a constant float column look correlated.
  • Building the candidate pair list with the strict threshold and the specified ordering, and skipping pairs whose members are already dropped.
  • Applying the drop rule and the tie rule exactly as stated.
  • Input validation ( TypeError , ValueError , KeyError , RuntimeError ) and leaving X and y unmodified.
  • Time and memory cost in terms of the number of rows and features.

Follow-up Questions Guidance

  • Why is greedy pair-wise dropping order-dependent, and what are the alternatives?
  • How would you scale this to tens of thousands of features?
  • When is Pearson correlation the wrong measure here?