Compare coefficient norms of unpenalized and L2-penalized logistic regression in scikit-learn

Quick Overview

Fit an unpenalized and an L2-penalized logistic regression in scikit-learn on the same standardized data and compare their coefficient norms. It tests how C sets regularization strength, why the penalized norm cannot exceed the unpenalized one, and what separable data does to the MLE.

Compare coefficient norms of unpenalized and L2-penalized logistic regression in scikit-learn

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `def penalized_vs_unpenalized_norm(X: np.ndarray, y: np.ndarray) -> tuple`. `X` has shape `(n_samples, n_features)` (float64, already standardized) and `y` has shape `(n_samples,)` with values in `{0, 1}`, and the classes are not linearly separable. Fit an unpenalized and an L2-penalized binary `LogisticRegression` and return their coefficient norms. Requirements: - Model (a): `penalty=None` (the Python `None`; the string `'none'` raises). - Model (b): `penalty='l2'`, `C=0.01`. - Both use exactly `solver='lbfgs'`, `fit_intercept=True`, `max_iter=20000`, `tol=1e-10`. - Return `(norm_unpenalized, norm_l2)` as Python `float`s, each computed as `float(np.linalg.norm(model.coef_.ravel()))`. Be ready to say which norm is larger and why. ```hint Defaults Check what `LogisticRegression` fits when `penalty` and `C` are left at their defaults. ``` ```hint Compare the objectives Write down both objectives. Ask what the unpenalized minimizer's loss tells you about the penalized solution's loss and its norm. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n_samples, n_features)` and already standardized; `y` is binary in `{0, 1}`, and the classes are not linearly separable. - Only the penalty settings differ between the two models. ### What a Strong Answer Covers - The exact estimator settings: `penalty=None` as the Python object for model (a), L2 with `C=0.01` for model (b), and the shared solver, intercept, iteration and tolerance pins. - Norms computed on `coef_` only, returned as plain floats in the required order. - A reason the L2-penalized norm can never exceed the unpenalized norm, and how `C` sets the penalty strength. - What non-separability guarantees for the unpenalized fit and what happens without it. ### Follow-up Questions - Why does smaller `C` produce a smaller coefficient norm? - Why can the unpenalized fit's norm blow up on separable data? - Why does the same `C=0.01` shrink the coefficients less on a dataset with 100 times more rows? - Would the ordering of the two norms still be guaranteed with an L1 penalty?

Overview: Fit an unpenalized and an L2-penalized logistic regression in scikit-learn on the same standardized data and compare their coefficient norms. It tests how C sets regularization strength, why the penalized norm cannot exceed the unpenalized one, and what separable data does to the MLE.

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

Implement def penalized_vs_unpenalized_norm(X: np.ndarray, y: np.ndarray) -> tuple. X has shape (n_samples, n_features) (float64, already standardized) and y has shape (n_samples,) with values in {0, 1}, and the classes are not linearly separable. Fit an unpenalized and an L2-penalized binary LogisticRegression and return their coefficient norms.

Requirements:

  • Model (a): penalty=None (the Python None ; the string 'none' raises).
  • Model (b): penalty='l2' , C=0.01 .
  • Both use exactly solver='lbfgs' , fit_intercept=True , max_iter=20000 , tol=1e-10 .
  • Return (norm_unpenalized, norm_l2) as Python float s, each computed as float(np.linalg.norm(model.coef_.ravel())) .

Be ready to say which norm is larger and why.

Constraints & Assumptions

  • X is float64 with shape (n_samples, n_features) and already standardized; y is binary in {0, 1} , and the classes are not linearly separable.
  • Only the penalty settings differ between the two models.

What a Strong Answer Covers Guidance

  • The exact estimator settings: penalty=None as the Python object for model (a), L2 with C=0.01 for model (b), and the shared solver, intercept, iteration and tolerance pins.
  • Norms computed on coef_ only, returned as plain floats in the required order.
  • A reason the L2-penalized norm can never exceed the unpenalized norm, and how C sets the penalty strength.
  • What non-separability guarantees for the unpenalized fit and what happens without it.

Follow-up Questions Guidance

  • Why does smaller C produce a smaller coefficient norm?
  • Why can the unpenalized fit's norm blow up on separable data?
  • Why does the same C=0.01 shrink the coefficients less on a dataset with 100 times more rows?
  • Would the ordering of the two norms still be guaranteed with an L1 penalty?