Fit an Unregularized Logistic Regression with statsmodels Logit

Quick Overview

Fit an unpenalized binary logistic regression with statsmodels Logit and Newton's method, and return the intercept followed by the slope coefficients. It tests adding the intercept column explicitly, pinning optimizer settings, and recognizing when perfect separation makes the maximum likelihood estimate fail to exist.

Fit an Unregularized Logistic Regression with statsmodels Logit

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fit_logit_params(X: np.ndarray, y: np.ndarray) -> np.ndarray`. `X` is a 2-D float64 `(n, p)` array with no intercept column and `y` is a 1-D int 0/1 array. Fit an unregularized logistic regression with `statsmodels` and return coefficients ordered `[intercept, beta_1, ..., beta_p]`. Requirements: - Prepend the intercept via `Xc = sm.add_constant(X)` (statsmodels does not add one). - Fit exactly with `sm.Logit(y, Xc).fit(method='newton', maxiter=200, tol=1e-10, disp=0)` (plain `Logit`, not `fit_regularized`). - `disp=0` is required: no optimizer output may be printed. - Return `res.params` as a 1-D float64 array of shape `(p+1,)`. Example (12 rows in three groups: four with both features 0, four with `x1 = 1`, four with `x2 = 1`): ```python X = np.array([[0, 0]] * 4 + [[1, 0]] * 4 + [[0, 1]] * 4, dtype=np.float64) y = np.array([1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0]) fit_logit_params(X, y) # -> approximately array([-1.098612, 1.098612, 2.197225]) ``` The positive rates are 1/4, 1/2 and 3/4 in the three groups. With an intercept and two indicators the model fits each group's rate exactly, so the intercept is $\ln(1/3) = -\ln 3$, `beta_1` is ${0 - (-\ln 3) = \ln 3}$ and `beta_2` is $\ln 3 - (-\ln 3) = \ln 9$. ```hint Intercept placement Check what `sm.Logit` assumes about the columns of the design matrix you pass it, and where `sm.add_constant` puts the column it adds. ``` ### Constraints & Assumptions - `X` has shape `(n, p)`, full column rank, and no constant column. `y` has length `n`. - The data is not perfectly separable, so the maximum likelihood estimate exists. - The result is compared to a reference with `numpy.testing.assert_allclose(rtol=1e-5)` and must have shape `(p+1,)`. ### What a Strong Answer Covers - Adding the intercept column explicitly and placing it first, so the parameter order matches `[intercept, beta_1, ..., beta_p]`. - Calling plain `sm.Logit(...).fit` with the pinned Newton settings and `disp=0`, and why each pin is there. - Returning a 1-D `float64` array of shape `(p+1,)`. - What happens to the fit under perfect separation, and how to detect it. ### Follow-up Questions - Why does perfect separation break the `newton` fit, and how would you detect it? - How would the return order change if you used `prepend=False`? - Why would scikit-learn's `LogisticRegression()` with default settings return different coefficients on the same data?

Overview: Fit an unpenalized binary logistic regression with statsmodels Logit and Newton's method, and return the intercept followed by the slope coefficients. It tests adding the intercept column explicitly, pinning optimizer settings, and recognizing when perfect separation makes the maximum likelihood estimate fail to exist.

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

Implement fit_logit_params(X: np.ndarray, y: np.ndarray) -> np.ndarray. X is a 2-D float64 (n, p) array with no intercept column and y is a 1-D int 0/1 array. Fit an unregularized logistic regression with statsmodels and return coefficients ordered [intercept, beta_1, ..., beta_p].

Requirements:

  • Prepend the intercept via Xc = sm.add_constant(X) (statsmodels does not add one).
  • Fit exactly with sm.Logit(y, Xc).fit(method='newton', maxiter=200, tol=1e-10, disp=0) (plain Logit , not fit_regularized ).
  • disp=0 is required: no optimizer output may be printed.
  • Return res.params as a 1-D float64 array of shape (p+1,) .

Example (12 rows in three groups: four with both features 0, four with x1 = 1, four with x2 = 1):

X = np.array([[0, 0]] * 4 + [[1, 0]] * 4 + [[0, 1]] * 4, dtype=np.float64)
y = np.array([1, 0, 0, 0,   1, 1, 0, 0,   1, 1, 1, 0])

fit_logit_params(X, y)
# -> approximately array([-1.098612, 1.098612, 2.197225])

The positive rates are 1/4, 1/2 and 3/4 in the three groups. With an intercept and two indicators the model fits each group's rate exactly, so the intercept is ln⁡(1/3)=−ln⁡3\ln(1/3) = -\ln 3, beta_1 is 0−(−ln⁡3)=ln⁡3{0 - (-\ln 3) = \ln 3} and beta_2 is ln⁡3−(−ln⁡3)=ln⁡9\ln 3 - (-\ln 3) = \ln 9.

Constraints & Assumptions

  • X has shape (n, p) , full column rank, and no constant column. y has length n .
  • The data is not perfectly separable, so the maximum likelihood estimate exists.
  • The result is compared to a reference with numpy.testing.assert_allclose(rtol=1e-5) and must have shape (p+1,) .

What a Strong Answer Covers Guidance

  • Adding the intercept column explicitly and placing it first, so the parameter order matches [intercept, beta_1, ..., beta_p] .
  • Calling plain sm.Logit(...).fit with the pinned Newton settings and disp=0 , and why each pin is there.
  • Returning a 1-D float64 array of shape (p+1,) .
  • What happens to the fit under perfect separation, and how to detect it.

Follow-up Questions Guidance

  • Why does perfect separation break the newton fit, and how would you detect it?
  • How would the return order change if you used prepend=False ?
  • Why would scikit-learn's LogisticRegression() with default settings return different coefficients on the same data?