Standardized Logistic Regression Coefficients

Quick Overview

Standardize each predictor with the population standard deviation, fit an unregularized logistic regression in statsmodels, and return the intercept and per-SD slopes. It tests parameter ordering, why the log-likelihood does not change under standardization, and how to read and convert standardized coefficients.

Standardized Logistic Regression Coefficients

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `standardized_coefs(X: np.ndarray, y: np.ndarray) -> np.ndarray`. `X` is an `(n, p)` float array of raw predictors and `y` is a 1-D 0/1 int array. Standardize each column of `X`, fit an unregularized logistic regression, and return the fitted parameters. The function must: - Standardize with the population SD: `Z = (X - X.mean(axis=0)) / X.std(axis=0, ddof=0)`. - Fit `sm.Logit(y, sm.add_constant(Z)).fit(method='newton', maxiter=200, tol=1e-10, disp=0)`. - Return `res.params` as a 1-D float64 array of shape `(p+1,)`, ordered `[intercept, beta_1_std, ..., beta_p_std]`. ```hint Check the SD convention NumPy, pandas and the `statistics` module do not share the same default degrees of freedom for a standard deviation. The grader's tolerance is tight enough to tell the two conventions apart, so confirm which one you are computing. ``` ### Constraints & Assumptions - The grader uses a fixed, non-separable dataset with `n=60` and `p=2`. It checks that the output has shape `(3,)` and compares the values with `numpy.testing.assert_allclose(rtol=1e-5)`. - It also checks that the standardized fit's `llf` matches the `llf` of the same model fitted on raw `X`, to `rtol=1e-6`. - `statsmodels` and NumPy are available. ### What a Strong Answer Covers - Standardization with the population SD (`ddof=0`) and the intercept-first parameter order that `sm.add_constant` produces. - Why an unregularized fit has the same log-likelihood and fitted probabilities before and after standardizing, and how the standardized slopes relate to the raw ones. - How to read a standardized slope: the change in log-odds for a one-SD increase in that feature. - Inputs that break the fit, such as a zero-variance column or perfectly separated classes. ### Follow-up Questions - Why does standardizing leave `llf` and predictions unchanged, but regularizing would not? - How would you recover the raw-scale coefficients from the standardized ones? - Can you rank features by importance using the size of their standardized coefficients?

Overview: Standardize each predictor with the population standard deviation, fit an unregularized logistic regression in statsmodels, and return the intercept and per-SD slopes. It tests parameter ordering, why the log-likelihood does not change under standardization, and how to read and convert standardized coefficients.

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

Implement standardized_coefs(X: np.ndarray, y: np.ndarray) -> np.ndarray. X is an (n, p) float array of raw predictors and y is a 1-D 0/1 int array. Standardize each column of X, fit an unregularized logistic regression, and return the fitted parameters.

The function must:

  • Standardize with the population SD: Z = (X - X.mean(axis=0)) / X.std(axis=0, ddof=0) .
  • Fit sm.Logit(y, sm.add_constant(Z)).fit(method='newton', maxiter=200, tol=1e-10, disp=0) .
  • Return res.params as a 1-D float64 array of shape (p+1,) , ordered [intercept, beta_1_std, ..., beta_p_std] .

Constraints & Assumptions

  • The grader uses a fixed, non-separable dataset with n=60 and p=2 . It checks that the output has shape (3,) and compares the values with numpy.testing.assert_allclose(rtol=1e-5) .
  • It also checks that the standardized fit's llf matches the llf of the same model fitted on raw X , to rtol=1e-6 .
  • statsmodels and NumPy are available.

What a Strong Answer Covers Guidance

  • Standardization with the population SD ( ddof=0 ) and the intercept-first parameter order that sm.add_constant produces.
  • Why an unregularized fit has the same log-likelihood and fitted probabilities before and after standardizing, and how the standardized slopes relate to the raw ones.
  • How to read a standardized slope: the change in log-odds for a one-SD increase in that feature.
  • Inputs that break the fit, such as a zero-variance column or perfectly separated classes.

Follow-up Questions Guidance

  • Why does standardizing leave llf and predictions unchanged, but regularizing would not?
  • How would you recover the raw-scale coefficients from the standardized ones?
  • Can you rank features by importance using the size of their standardized coefficients?