Compute odds ratios for dummy-encoded categories in a logistic regression

Quick Overview

Fit a statsmodels logistic regression with a one-hot encoded grade column and return the odds ratios of grades B and C against grade A as an exact pandas Series. It tests reference-level encoding, the dummy-variable trap and how to read odds ratios.

Compute odds ratios for dummy-encoded categories in a logistic regression

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `dummy_odds_ratios(df: pd.DataFrame, y: np.ndarray) -> pd.Series`. `df` has exactly two columns: `'score'` (float64) and `'grade'` (string categorical: `'A'`, `'B'`, `'C'`), `y` is a 1-D 0/1 int array. Return the odds ratios (`exp(coef)`) of the grade dummies relative to reference level `'A'`. The function must follow these rules: - Encode with `pd.get_dummies(df, columns=['grade'], drop_first=True, dtype=float)`. - Fit `sm.Logit(y, sm.add_constant(encoded)).fit(method='newton', maxiter=200, tol=1e-10, disp=0)`. - Return a float64 `pd.Series` indexed exactly `['grade_B', 'grade_C']` in that order, excluding the intercept. - Set `name=None` and `index.name=None`. ```hint Match more than the values Check which level `get_dummies` drops when `drop_first=True`, and what `assert_series_equal` compares besides the numbers: dtype, index labels and their order, and names. ``` ### Constraints & Assumptions - The data are a fixed frame with `n=60` rows that is not separable, so the fit converges. - The result is compared with `pandas.testing.assert_series_equal(result, expected, rtol=1e-5, check_dtype=True)`. - `statsmodels` (`sm`), `pandas`, and `numpy` are available. ### What a Strong Answer Covers - The encoding that makes `'A'` the reference level, and why one level must be dropped when the model has an intercept. - The fit as specified and the selection of only the two dummy coefficients. - Exponentiating the coefficients and reading each odds ratio correctly, holding `score` fixed. - A returned Series whose dtype, index order and names match the expected one. ### Follow-up Questions - How would the returned series change if you used `drop_first=False`? - How would you report confidence intervals for these odds ratios? - What breaks if `grade` arrives as a pandas `Categorical` whose categories are ordered `'C'`, `'B'`, `'A'`, and how would you guard against it?

Overview: Fit a statsmodels logistic regression with a one-hot encoded grade column and return the odds ratios of grades B and C against grade A as an exact pandas Series. It tests reference-level encoding, the dummy-variable trap and how to read odds ratios.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement dummy_odds_ratios(df: pd.DataFrame, y: np.ndarray) -> pd.Series. df has exactly two columns: 'score' (float64) and 'grade' (string categorical: 'A', 'B', 'C'), y is a 1-D 0/1 int array. Return the odds ratios (exp(coef)) of the grade dummies relative to reference level 'A'.

The function must follow these rules:

  • Encode with pd.get_dummies(df, columns=['grade'], drop_first=True, dtype=float) .
  • Fit sm.Logit(y, sm.add_constant(encoded)).fit(method='newton', maxiter=200, tol=1e-10, disp=0) .
  • Return a float64 pd.Series indexed exactly ['grade_B', 'grade_C'] in that order, excluding the intercept.
  • Set name=None and index.name=None .

Constraints & Assumptions

  • The data are a fixed frame with n=60 rows that is not separable, so the fit converges.
  • The result is compared with pandas.testing.assert_series_equal(result, expected, rtol=1e-5, check_dtype=True) .
  • statsmodels ( sm ), pandas , and numpy are available.

What a Strong Answer Covers Guidance

  • The encoding that makes 'A' the reference level, and why one level must be dropped when the model has an intercept.
  • The fit as specified and the selection of only the two dummy coefficients.
  • Exponentiating the coefficients and reading each odds ratio correctly, holding score fixed.
  • A returned Series whose dtype, index order and names match the expected one.

Follow-up Questions Guidance

  • How would the returned series change if you used drop_first=False ?
  • How would you report confidence intervals for these odds ratios?
  • What breaks if grade arrives as a pandas Categorical whose categories are ordered 'C' , 'B' , 'A' , and how would you guard against it?