L1 vs L2 Logistic Regression: Count Exactly-Zero Coefficients

Quick Overview

Fit an L1-penalized and an L2-penalized logistic regression with identical saga settings and count how many coefficients each sets exactly to zero. It tests why the L1 penalty produces sparse models, why L2 only shrinks weights, and how the regularization strength C controls sparsity.

L1 vs L2 Logistic Regression: Count Exactly-Zero Coefficients

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def l1_vs_l2_zero_counts(X: np.ndarray, y: np.ndarray) -> tuple` that fits two binary logistic regression models differing only in penalty and counts each model's exactly-zero coefficients. `X` is `(n_samples, n_features)` float64, already standardized, and `y` is `(n_samples,)` with labels in `{0, 1}`. Requirements: - Fit two `LogisticRegression` models on the same data: `penalty='l1'` and `penalty='l2'`. - Both use exactly `solver='saga'`, `C=0.05`, `fit_intercept=True`, `max_iter=50000`, `tol=1e-8`, `random_state=0`. - Count zeros via `np.sum(np.isclose(model.coef_.ravel(), 0.0, atol=1e-6))`. - Return `(n_zero_l1, n_zero_l2)` as two plain Python `int`s. For example, with one standardized feature: ```python X = np.array([[-1.0], [-1.0], [1.0], [1.0]]) y = np.array([0, 0, 1, 1]) n_zero_l1, n_zero_l2 = l1_vs_l2_zero_counts(X, y) # (n_zero_l1, n_zero_l2) == (1, 0) ``` On typical data with some weak or uninformative features, expect `n_zero_l1 > 0` and `n_zero_l2 == 0`. ```hint Look near zero Compare how each penalty pushes on a weight as the weight approaches zero: does the push keep a fixed size, or does it fade? ``` ### Constraints & Assumptions - `X` is float64 with shape `(n_samples, n_features)` and is already standardized; fit on it as given. - `y` has shape `(n_samples,)` with labels in `{0, 1}`. - The two fits differ only in `penalty`; every other argument is shared. ### What a Strong Answer Covers - Fits both models with the shared `saga` settings so the penalty is the only difference. - Counts zeros with the specified tolerance and returns two plain Python `int`s in the order `(n_zero_l1, n_zero_l2)`. - Explains why the L1 penalty yields exact zeros and the L2 penalty only shrinks. - Says when the expected pattern can fail, for example a dataset where every feature carries strong signal or a feature with an identically zero gradient. ### Follow-up Questions - Why does L1 produce exact zeros while L2 only shrinks? - Why must `solver='saga'` be used here? - How does the choice of `C` affect the sparsity level?

Overview: Fit an L1-penalized and an L2-penalized logistic regression with identical saga settings and count how many coefficients each sets exactly to zero. It tests why the L1 penalty produces sparse models, why L2 only shrinks weights, and how the regularization strength C controls sparsity.

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

Implement def l1_vs_l2_zero_counts(X: np.ndarray, y: np.ndarray) -> tuple that fits two binary logistic regression models differing only in penalty and counts each model's exactly-zero coefficients. X is (n_samples, n_features) float64, already standardized, and y is (n_samples,) with labels in {0, 1}.

Requirements:

  • Fit two LogisticRegression models on the same data: penalty='l1' and penalty='l2' .
  • Both use exactly solver='saga' , C=0.05 , fit_intercept=True , max_iter=50000 , tol=1e-8 , random_state=0 .
  • Count zeros via np.sum(np.isclose(model.coef_.ravel(), 0.0, atol=1e-6)) .
  • Return (n_zero_l1, n_zero_l2) as two plain Python int s.

For example, with one standardized feature:

X = np.array([[-1.0], [-1.0], [1.0], [1.0]])
y = np.array([0, 0, 1, 1])
n_zero_l1, n_zero_l2 = l1_vs_l2_zero_counts(X, y)
# (n_zero_l1, n_zero_l2) == (1, 0)

On typical data with some weak or uninformative features, expect n_zero_l1 > 0 and n_zero_l2 == 0.

Constraints & Assumptions

  • X is float64 with shape (n_samples, n_features) and is already standardized; fit on it as given.
  • y has shape (n_samples,) with labels in {0, 1} .
  • The two fits differ only in penalty ; every other argument is shared.

What a Strong Answer Covers Guidance

  • Fits both models with the shared saga settings so the penalty is the only difference.
  • Counts zeros with the specified tolerance and returns two plain Python int s in the order (n_zero_l1, n_zero_l2) .
  • Explains why the L1 penalty yields exact zeros and the L2 penalty only shrinks.
  • Says when the expected pattern can fail, for example a dataset where every feature carries strong signal or a feature with an identically zero gradient.

Follow-up Questions Guidance

  • Why does L1 produce exact zeros while L2 only shrinks?
  • Why must solver='saga' be used here?
  • How does the choice of C affect the sparsity level?