Find the row whose removal changes a Pearson correlation the most (leave-one-out influence)

Quick Overview

Write a pandas function that recomputes a Pearson correlation with each row left out and reports the row whose removal changes it the most. It tests leave-one-out diagnostics, deterministic tie-breaking, exact output formatting, and how to bring a quadratic scan down to linear time.

Find the row whose removal changes a Pearson correlation the most (leave-one-out influence)

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `most_influential_observation(df, col_a, col_b)`, which takes a DataFrame and two numeric column names. Compute the full-sample Pearson correlation, then recompute it with each single row removed. Return a `pd.Series` of `float64` describing the row whose removal changes $r$ by the largest absolute amount. The function must meet these requirements: - The Series index is exactly `['r_full', 'r_without', 'delta', 'index_label']`, and every value is a float. - `r_full` is the Pearson correlation (`.corr()`) over all rows. `r_without` is the correlation after removing the most influential row. - `delta` is `abs(r_without - r_full)`. - `index_label` is that row's index label cast to `float`. - On a tie in influence, pick the smaller index label. Example: ```python df = pd.DataFrame({'x': [1, 2, 3, 4, 50], 'y': [1, 2, 3, 4, 5]}) most_influential_observation(df, 'x', 'y') # r_full 0.743294 # r_without 1.000000 # delta 0.256706 # index_label 4.000000 # dtype: float64 ``` Row 4 (`x = 50`) is the outlier. Without it the remaining points lie exactly on the line `y = x`, so the correlation rises from about 0.743 to 1, a larger change than any other single removal. ```hint Ranking the candidates Decide how you will rank the rows so that two equal changes always resolve to the smaller label, whatever order your loop visits them in. ``` ```hint Same observation, both columns The row you drop has to disappear from both columns at once, and the label you report must be the one you dropped. ``` ### Constraints & Assumptions - Both columns are numeric, and the correlation stays defined after any single removal. - Index labels are unique and numeric, so each one can be cast to `float`. ### Clarifying Questions to Ask - Should rows with a missing value in either column be dropped before the scan, or left for `.corr()` to skip pairwise? - How large can the frame get? That decides whether recomputing the correlation once per row is acceptable. ### What a Strong Answer Covers - Computing the full-sample correlation once and a leave-one-out correlation for every row, with the same observation removed from both columns. - Selecting the largest absolute change with a deterministic tie-break on the smaller label. - Returning a `float64` Series with the four labels in the required order. - The cost of the direct scan and how to reduce it. - What a large `delta` says about how far the headline correlation can be trusted. ### Follow-up Questions - How would you make this efficient for large n instead of recomputing r from scratch n times? - How would you report a robust correlation alongside the flagged row? - Two outliers sit close together and each one hides the other's effect. Why does this single-row scan miss them, and what would you use instead?

Overview: Write a pandas function that recomputes a Pearson correlation with each row left out and reports the row whose removal changes it the most. It tests leave-one-out diagnostics, deterministic tie-breaking, exact output formatting, and how to bring a quadratic scan down to linear time.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Write most_influential_observation(df, col_a, col_b), which takes a DataFrame and two numeric column names. Compute the full-sample Pearson correlation, then recompute it with each single row removed. Return a pd.Series of float64 describing the row whose removal changes rr by the largest absolute amount.

The function must meet these requirements:

  • The Series index is exactly ['r_full', 'r_without', 'delta', 'index_label'] , and every value is a float.
  • r_full is the Pearson correlation ( .corr() ) over all rows. r_without is the correlation after removing the most influential row.
  • delta is abs(r_without - r_full) .
  • index_label is that row's index label cast to float .
  • On a tie in influence, pick the smaller index label.

Example:

df = pd.DataFrame({'x': [1, 2, 3, 4, 50], 'y': [1, 2, 3, 4, 5]})
most_influential_observation(df, 'x', 'y')
# r_full         0.743294
# r_without      1.000000
# delta          0.256706
# index_label    4.000000
# dtype: float64

Row 4 (x = 50) is the outlier. Without it the remaining points lie exactly on the line y = x, so the correlation rises from about 0.743 to 1, a larger change than any other single removal.

Constraints & Assumptions

  • Both columns are numeric, and the correlation stays defined after any single removal.
  • Index labels are unique and numeric, so each one can be cast to float .

Clarifying Questions to Ask Guidance

  • Should rows with a missing value in either column be dropped before the scan, or left for .corr() to skip pairwise?
  • How large can the frame get? That decides whether recomputing the correlation once per row is acceptable.

What a Strong Answer Covers Guidance

  • Computing the full-sample correlation once and a leave-one-out correlation for every row, with the same observation removed from both columns.
  • Selecting the largest absolute change with a deterministic tie-break on the smaller label.
  • Returning a float64 Series with the four labels in the required order.
  • The cost of the direct scan and how to reduce it.
  • What a large delta says about how far the headline correlation can be trusted.

Follow-up Questions Guidance

  • How would you make this efficient for large n instead of recomputing r from scratch n times?
  • How would you report a robust correlation alongside the flagged row?
  • Two outliers sit close together and each one hides the other's effect. Why does this single-row scan miss them, and what would you use instead?