Standardized Gap Report: Rank Numeric Features by Cohen's d With pandas

Quick Overview

Write a pandas function that compares every numeric feature across a binary target, computing class means, the pooled sample standard deviation and Cohen's d. It tests effect size reasoning, handling of zero variance and missing values, and a deterministic sort that must match an expected DataFrame exactly.

Standardized Gap Report: Rank Numeric Features by Cohen's d With pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `standardized_gap_report(df, target)` with pandas. `target` names an integer column that holds 0 or 1. For every other numeric column (non-numeric columns are ignored), compute the mean in each class, the pooled sample standard deviation and Cohen's d, then return the features ranked by how strongly they separate the two classes. The report must follow these rules: - `mean_0` and `mean_1` are the feature's mean where `target == 0` and where `target == 1`. - `pooled_std = sqrt(((n0-1)*s0**2 + (n1-1)*s1**2) / (n0+n1-2))`, where `n0` and `n1` are the class sizes and `s0` and `s1` are the sample standard deviations (`ddof=1`). - `cohens_d = (mean_1 - mean_0) / pooled_std`, or `numpy.nan` (never `inf`) when `pooled_std == 0`. - The columns are exactly `['feature', 'mean_0', 'mean_1', 'pooled_std', 'cohens_d']`. `feature` has dtype `object` and the other four are `float64`. The target column is never reported as a feature. - Rows are sorted by absolute `cohens_d` descending, then by `feature` ascending, and the index is reset with `reset_index(drop=True)`. Rows with a NaN `cohens_d` go last, still ordered by `feature`. - The result is compared with an expected frame using `pandas.testing.assert_frame_equal`, so column order, dtypes and row order all count. ```hint Reuse the class masks Build the two boolean masks on the target column once and apply them to each feature. ``` ```hint Sort on magnitude A large negative d separates the classes as well as a large positive one. Decide what the sort key should be and how to keep it out of the final columns. ``` ### Constraints & Assumptions - `df` is a pandas DataFrame with any mix of numeric and non-numeric columns. - The `target` column is an integer column whose values are 0 or 1. ### Clarifying Questions to Ask - Should missing values in a feature be skipped, with `n0` and `n1` counting only the non-missing values? - What should the report show when a class has one value or no values for a feature? - Do boolean columns count as numeric features? ### What a Strong Answer Covers - Selecting the numeric feature columns and excluding the target. - Class means, sample variances with `ddof=1`, and the pooled standard deviation exactly as specified. - The zero-spread guard that returns NaN for `cohens_d`. - A deterministic sort on absolute d with the name tie-break, plus the exact column names and dtypes. - Edge cases: missing values, tiny or empty classes, and a frame with no numeric features besides the target. ### Follow-up Questions - How would you extend this report to a target with more than two classes? - Why does Cohen's d divide by a pooled standard deviation computed with `ddof=1`, and when would a different denominator be the better choice? - How would you correct d for small samples? - How would you compute the same report without a Python loop over features?

Overview: Write a pandas function that compares every numeric feature across a binary target, computing class means, the pooled sample standard deviation and Cohen's d. It tests effect size reasoning, handling of zero variance and missing values, and a deterministic sort that must match an expected DataFrame exactly.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement standardized_gap_report(df, target) with pandas. target names an integer column that holds 0 or 1. For every other numeric column (non-numeric columns are ignored), compute the mean in each class, the pooled sample standard deviation and Cohen's d, then return the features ranked by how strongly they separate the two classes.

The report must follow these rules:

  • mean_0 and mean_1 are the feature's mean where target == 0 and where target == 1 .
  • pooled_std = sqrt(((n0-1)*s0**2 + (n1-1)*s1**2) / (n0+n1-2)) , where n0 and n1 are the class sizes and s0 and s1 are the sample standard deviations ( ddof=1 ).
  • cohens_d = (mean_1 - mean_0) / pooled_std , or numpy.nan (never inf ) when pooled_std == 0 .
  • The columns are exactly ['feature', 'mean_0', 'mean_1', 'pooled_std', 'cohens_d'] . feature has dtype object and the other four are float64 . The target column is never reported as a feature.
  • Rows are sorted by absolute cohens_d descending, then by feature ascending, and the index is reset with reset_index(drop=True) . Rows with a NaN cohens_d go last, still ordered by feature .
  • The result is compared with an expected frame using pandas.testing.assert_frame_equal , so column order, dtypes and row order all count.

Constraints & Assumptions

  • df is a pandas DataFrame with any mix of numeric and non-numeric columns.
  • The target column is an integer column whose values are 0 or 1.

Clarifying Questions to Ask Guidance

  • Should missing values in a feature be skipped, with n0 and n1 counting only the non-missing values?
  • What should the report show when a class has one value or no values for a feature?
  • Do boolean columns count as numeric features?

What a Strong Answer Covers Guidance

  • Selecting the numeric feature columns and excluding the target.
  • Class means, sample variances with ddof=1 , and the pooled standard deviation exactly as specified.
  • The zero-spread guard that returns NaN for cohens_d .
  • A deterministic sort on absolute d with the name tie-break, plus the exact column names and dtypes.
  • Edge cases: missing values, tiny or empty classes, and a frame with no numeric features besides the target.

Follow-up Questions Guidance

  • How would you extend this report to a target with more than two classes?
  • Why does Cohen's d divide by a pooled standard deviation computed with ddof=1 , and when would a different denominator be the better choice?
  • How would you correct d for small samples?
  • How would you compute the same report without a Python loop over features?