Build a Logistic Regression Odds Ratio Table in statsmodels

Quick Overview

Fit a logistic regression with statsmodels and return a pandas table that lists each coefficient next to its odds ratio, intercept first. Tests turning log-odds coefficients into odds ratios, reading a fitted model and producing an exact DataFrame with the right index and dtypes.

Build a Logistic Regression Odds Ratio Table in statsmodels

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `odds_ratio_table(X: pd.DataFrame, y: np.ndarray) -> pd.DataFrame`. `X` has a default `RangeIndex` and named `float64` columns with no intercept, and `y` is a 1-D int 0/1 array. Fit `sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0)` and return each coefficient with its odds ratio. Requirements: - The columns are exactly `['coef', 'odds_ratio']`, in that order, both `float64`. - `coef` is the fitted log-odds coefficient and `odds_ratio` is `exp(coef)`. - The index holds the parameter names as strings: `'const'` first, then the `X` columns in their original order. - The index has `name=None`. - Do not round any values. Example with a single binary column: ```python X = pd.DataFrame({"female": [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0]}) y = np.array([0, 0, 0, 1, 0, 1, 1, 1]) odds_ratio_table(X, y) ``` ```text coef odds_ratio const -1.098612 0.333333 female 2.197225 9.000000 ``` With one binary predictor the model is saturated, so the fit reproduces the group rates exactly. The `female = 0` group has 1 positive in 4, so its odds are 1/3 and `const` is $\ln(1/3)$. The `female = 1` group has 3 positives in 4, so its odds are 3, and the ratio of the two odds is 9. ```hint Start from the fit The fitted result already carries the coefficients under their parameter names. Check how that object is ordered and named before you reorder anything yourself. ``` ### Constraints & Assumptions - The test uses a fixed, non-separable frame with `n = 50` rows and `p = 3` columns named `['math', 'female', 'read']`. The expected index is `['const', 'math', 'female', 'read']`. - The result is compared with `pandas.testing.assert_frame_equal(result, expected, rtol=1e-5, check_dtype=True, check_like=False)`, so column order matters. ### What a Strong Answer Covers - Adding the intercept and fitting with the exact call given, so `'const'` comes first - Taking the coefficients from the fitted result and exponentiating them for the odds ratios - Why the exponential of a log-odds coefficient is the multiplicative change in the odds for a one-unit increase in that predictor - Producing the exact frame: two columns in order, `float64` values, a string index in fit order, no index name and no rounding ### Follow-up Questions - How would you add a 95% confidence interval for each odds ratio? - What happens if the data is perfectly separable? - If `math` were rescaled from points to tens of points, how would its coefficient and odds ratio change?

Overview: Fit a logistic regression with statsmodels and return a pandas table that lists each coefficient next to its odds ratio, intercept first. Tests turning log-odds coefficients into odds ratios, reading a fitted model and producing an exact DataFrame with the right index and dtypes.

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

Implement odds_ratio_table(X: pd.DataFrame, y: np.ndarray) -> pd.DataFrame. X has a default RangeIndex and named float64 columns with no intercept, and y is a 1-D int 0/1 array. Fit sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0) and return each coefficient with its odds ratio.

Requirements:

  • The columns are exactly ['coef', 'odds_ratio'] , in that order, both float64 .
  • coef is the fitted log-odds coefficient and odds_ratio is exp(coef) .
  • The index holds the parameter names as strings: 'const' first, then the X columns in their original order.
  • The index has name=None .
  • Do not round any values.

Example with a single binary column:

X = pd.DataFrame({"female": [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0]})
y = np.array([0, 0, 0, 1, 0, 1, 1, 1])
odds_ratio_table(X, y)
            coef  odds_ratio
const  -1.098612    0.333333
female  2.197225    9.000000

With one binary predictor the model is saturated, so the fit reproduces the group rates exactly. The female = 0 group has 1 positive in 4, so its odds are 1/3 and const is ln⁡(1/3)\ln(1/3). The female = 1 group has 3 positives in 4, so its odds are 3, and the ratio of the two odds is 9.

Constraints & Assumptions

  • The test uses a fixed, non-separable frame with n = 50 rows and p = 3 columns named ['math', 'female', 'read'] . The expected index is ['const', 'math', 'female', 'read'] .
  • The result is compared with pandas.testing.assert_frame_equal(result, expected, rtol=1e-5, check_dtype=True, check_like=False) , so column order matters.

What a Strong Answer Covers Guidance

  • Adding the intercept and fitting with the exact call given, so 'const' comes first
  • Taking the coefficients from the fitted result and exponentiating them for the odds ratios
  • Why the exponential of a log-odds coefficient is the multiplicative change in the odds for a one-unit increase in that predictor
  • Producing the exact frame: two columns in order, float64 values, a string index in fit order, no index name and no rounding

Follow-up Questions Guidance

  • How would you add a 95% confidence interval for each odds ratio?
  • What happens if the data is perfectly separable?
  • If math were rescaled from points to tens of points, how would its coefficient and odds ratio change?