Compare logistic regression coefficients from statsmodels and scikit-learn

Quick Overview

Write a function that fits one logistic regression three ways (statsmodels Logit, scikit-learn with its default L2 penalty, and scikit-learn with no penalty) and returns the coefficients in a shared intercept-first layout. It tests which objective each library optimizes, how each handles the intercept, and what regularization does to the slopes.

Compare logistic regression coefficients from statsmodels and scikit-learn

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Write `statsmodels_vs_sklearn_coefs(X, y)`. `X` is a 2-D float array of shape `(n, p)` with no intercept column, and `y` is a 1-D integer array of 0/1 labels. Fit three logistic regressions to the same data and return the tuple `(sm_params, sk_default_params, sk_unpenalized_params)`. Each element is a 1-D `float64` array of shape `(p+1,)` ordered `[intercept, beta_1, ..., beta_p]`. The three fits are specified exactly: - `sm_params` is the `.params` of `sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0)`. - `sk_default_params` comes from `LogisticRegression(penalty='l2', C=1.0, solver='lbfgs', max_iter=10000, tol=1e-10, random_state=0)`. - `sk_unpenalized_params` comes from `LogisticRegression(penalty=None, solver='lbfgs', max_iter=10000, tol=1e-10, random_state=0)`. Fit both scikit-learn models on the raw `X`, not on the `add_constant` matrix, and assemble each parameter vector with `np.concatenate([model.intercept_, model.coef_[0]])`. The grader checks each array against its own reference at `rtol=1e-4`. It also checks that `sk_unpenalized_params` matches `sm_params` at `rtol=1e-3`, and that every slope in `sk_default_params` is strictly smaller in absolute value than the matching slope in `sk_unpenalized_params`. ```hint Compare the objectives Write down the function each of the three fits optimizes. Two of them are the same function. ``` ```hint Count the intercepts scikit-learn adds its own intercept by default. Think about what design matrix each model actually sees. ``` ### Constraints & Assumptions - The grader builds one fixed, non-separable dataset in-process with `n = 80` rows and `p = 2` features. - `X` contains no constant column, and `y` contains only 0 and 1. - All three outputs must be `float64` with shape `(p+1,)`, intercept first. ### What a Strong Answer Covers - The right input to each library: `add_constant(X)` for statsmodels, raw `X` for both scikit-learn models, and one `[intercept, slopes]` layout for all three outputs. - Why statsmodels `Logit` and scikit-learn with `penalty=None` reach the same estimate, and what limits how closely they agree. - What the `C=1.0` L2 penalty does to the slopes, and how scikit-learn treats the intercept under the lbfgs solver. - The usual traps: a duplicated intercept, the `(1, p)` shape of `coef_`, and iteration limits that stop the optimizer early. ### Follow-up Questions - Why does `C=1.0` shrink the slopes while leaving the intercept comparatively less affected? - How would standardizing `X` change the comparison between the three fits? - What happens to each fit on perfectly separable data?

Overview: Write a function that fits one logistic regression three ways (statsmodels Logit, scikit-learn with its default L2 penalty, and scikit-learn with no penalty) and returns the coefficients in a shared intercept-first layout. It tests which objective each library optimizes, how each handles the intercept, and what regularization does to the slopes.

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

Write statsmodels_vs_sklearn_coefs(X, y). X is a 2-D float array of shape (n, p) with no intercept column, and y is a 1-D integer array of 0/1 labels. Fit three logistic regressions to the same data and return the tuple (sm_params, sk_default_params, sk_unpenalized_params). Each element is a 1-D float64 array of shape (p+1,) ordered [intercept, beta_1, ..., beta_p].

The three fits are specified exactly:

  • sm_params is the .params of sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0) .
  • sk_default_params comes from LogisticRegression(penalty='l2', C=1.0, solver='lbfgs', max_iter=10000, tol=1e-10, random_state=0) .
  • sk_unpenalized_params comes from LogisticRegression(penalty=None, solver='lbfgs', max_iter=10000, tol=1e-10, random_state=0) .

Fit both scikit-learn models on the raw X, not on the add_constant matrix, and assemble each parameter vector with np.concatenate([model.intercept_, model.coef_[0]]).

The grader checks each array against its own reference at rtol=1e-4. It also checks that sk_unpenalized_params matches sm_params at rtol=1e-3, and that every slope in sk_default_params is strictly smaller in absolute value than the matching slope in sk_unpenalized_params.

Constraints & Assumptions

  • The grader builds one fixed, non-separable dataset in-process with n = 80 rows and p = 2 features.
  • X contains no constant column, and y contains only 0 and 1.
  • All three outputs must be float64 with shape (p+1,) , intercept first.

What a Strong Answer Covers Guidance

  • The right input to each library: add_constant(X) for statsmodels, raw X for both scikit-learn models, and one [intercept, slopes] layout for all three outputs.
  • Why statsmodels Logit and scikit-learn with penalty=None reach the same estimate, and what limits how closely they agree.
  • What the C=1.0 L2 penalty does to the slopes, and how scikit-learn treats the intercept under the lbfgs solver.
  • The usual traps: a duplicated intercept, the (1, p) shape of coef_ , and iteration limits that stop the optimizer early.

Follow-up Questions Guidance

  • Why does C=1.0 shrink the slopes while leaving the intercept comparatively less affected?
  • How would standardizing X change the comparison between the three fits?
  • What happens to each fit on perfectly separable data?