Flag target-leakage suspects by Pearson correlation in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `leakage_suspects(df, target, threshold=0.98)`. For every numeric column other than `target`, compute its Pearson correlation with `df[target]` (for example with `df.corrwith` or `Series.corr`) and return the features flagged as likely leakage.
The function must meet these requirements:
- Consider only numeric columns, excluding `target`.
- Keep a feature only when its absolute correlation is strictly greater than `threshold`.
- Return exactly the columns `['feature', 'corr_with_target', 'abs_corr']`, with `feature` as `object` and the other two as `float64`.
- Sort by `abs_corr` descending, then `feature` ascending, then call `reset_index(drop=True)`.
- When there are no suspects, return an empty DataFrame with the same three columns and dtypes. A schema-less frame or `None` does not meet the contract.
Example, with one honest predictor, one rescaled copy of the target, one noise column and one text column:
```python
import pandas as pd
df = pd.DataFrame({
"label": [0, 1, 0, 1, 1, 0],
"honest_feat": [1.0, 2.5, 1.5, 2.0, 3.0, 2.0],
"leaked": [5, 15, 5, 15, 15, 5], # 10 * label + 5
"noise": [3, 1, 2, 2, 3, 1],
"city": ["NY", "SF", "NY", "LA", "SF", "LA"],
})
leakage_suspects(df, target="label", threshold=0.98)
# feature corr_with_target abs_corr
# 0 leaked 1.0 1.0
leakage_suspects(df.drop(columns="leaked"), target="label")
# Empty DataFrame
# Columns: [feature, corr_with_target, abs_corr]
# Index: []
```
The correlations with `label` are about 0.775 for `honest_feat`, 1.0 for `leaked` (an exact linear rescale, up to floating-point rounding) and 0.0 for `noise`. `city` is not numeric and is skipped.
```hint Test the healthy case
Call your function on a frame where nothing passes the threshold and inspect `result.dtypes`, not only the column names.
```
```hint Undefined correlations
Work out what Pearson correlation a constant column gets and how your filter treats that value.
```
### Constraints & Assumptions
- `df` is a pandas DataFrame that can mix numeric columns with text, datetime or boolean columns.
- `target` names a numeric column of `df`.
- `threshold` is a float between 0 and 1; the default is 0.98.
- The grader uses a fixed frame with one honest predictor, one rescaled copy of the target and one noise column.
### Clarifying Questions to Ask
- Do boolean columns count as numeric here, or only integer and float dtypes?
- For rows where a feature or the target is missing, should the correlation use the remaining complete pairs?
- Should a constant column, whose correlation is undefined, appear anywhere in the output?
### What a Strong Answer Covers
- Selecting the numeric columns and excluding the target before computing correlations.
- Filtering on the absolute correlation with a strict comparison, so negative leaks are caught and a value exactly at the threshold is not.
- The two-key sort and index reset that make the output order deterministic.
- Returning the same three columns and dtypes when nothing is flagged.
- Edge cases such as constant columns, missing values and boolean columns.
### Follow-up Questions
- Why is a threshold on Pearson correlation insufficient to catch all leakage?
- How would you extend this to flag leaky categorical or datetime columns?
- A feature is flagged with an absolute correlation of 0.99, and its owner says it is legitimate. How would you confirm or rule out leakage?
Overview: Write a pandas function that flags numeric features whose Pearson correlation with the target is above a threshold, a quick screen for target leakage. It tests vectorized correlation, strict filtering, a deterministic sort and returning a typed empty DataFrame when nothing is flagged.