Count Coefficients Zeroed by L1-Regularized Logistic Regression

Quick Overview

Fit an L1-penalized logistic regression in scikit-learn with fixed liblinear settings and count how many feature coefficients end up exactly zero. It tests how L1 regularization produces sparse models, how C controls the penalty strength, and careful handling of coef_, the intercept and return types.

Count Coefficients Zeroed by L1-Regularized Logistic Regression

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def count_zero_coefs_l1(X: np.ndarray, y: np.ndarray, C: float) -> int`. Fit a binary L1 `LogisticRegression` and return how many fitted coefficients are exactly zero. `X` is `(n_samples, n_features)` float64, already standardized. `y` is `(n_samples,)` with labels in `{0, 1}`. The function must: - Fit with exactly these settings: `penalty='l1'`, `C=C`, `solver='liblinear'`, `fit_intercept=True`, `max_iter=100000`, `tol=1e-10`, `random_state=0`. - Count zeros as `int(np.sum(np.isclose(model.coef_.ravel(), 0.0, atol=1e-8)))` (intercept excluded). - Return a plain Python `int`, not a NumPy scalar or array. ```hint Where the weights live Check the shape of `coef_` for a binary model, and whether the intercept is stored in it. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n_samples, n_features)` and is already standardized, so no extra scaling is applied. - `y` has shape `(n_samples,)`, uses labels 0 and 1, and contains both classes. - `C` is a positive float. Smaller values mean a stronger penalty. ### What a Strong Answer Covers - Building the model with exactly the given hyperparameters and fitting it on `X` as given. - Counting near-zero entries of `coef_` with the specified tolerance, with the intercept left out. - Returning a Python `int`. - Why an L1 penalty produces exact zeros and how the count moves as `C` changes. - Why the tight `tol` and the `atol` tolerance matter for a stable count. ### Follow-up Questions - How does the count change as `C` goes from very large to very small, and is there a value of `C` below which every coefficient is zero? - Two features are highly correlated. What does L1 tend to do with them, and how would elastic net change that? - Why was the data standardized first, and what happens to the zero count if one feature is on a much larger scale than the rest? - liblinear also penalizes the intercept. When does that matter, and how would you reduce its effect?

Overview: Fit an L1-penalized logistic regression in scikit-learn with fixed liblinear settings and count how many feature coefficients end up exactly zero. It tests how L1 regularization produces sparse models, how C controls the penalty strength, and careful handling of coef_, the intercept and return types.

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

Implement def count_zero_coefs_l1(X: np.ndarray, y: np.ndarray, C: float) -> int. Fit a binary L1 LogisticRegression and return how many fitted coefficients are exactly zero. X is (n_samples, n_features) float64, already standardized. y is (n_samples,) with labels in {0, 1}.

The function must:

  • Fit with exactly these settings: penalty='l1' , C=C , solver='liblinear' , fit_intercept=True , max_iter=100000 , tol=1e-10 , random_state=0 .
  • Count zeros as int(np.sum(np.isclose(model.coef_.ravel(), 0.0, atol=1e-8))) (intercept excluded).
  • Return a plain Python int , not a NumPy scalar or array.

Constraints & Assumptions

  • X is float64 with shape (n_samples, n_features) and is already standardized, so no extra scaling is applied.
  • y has shape (n_samples,) , uses labels 0 and 1, and contains both classes.
  • C is a positive float. Smaller values mean a stronger penalty.

What a Strong Answer Covers Guidance

  • Building the model with exactly the given hyperparameters and fitting it on X as given.
  • Counting near-zero entries of coef_ with the specified tolerance, with the intercept left out.
  • Returning a Python int .
  • Why an L1 penalty produces exact zeros and how the count moves as C changes.
  • Why the tight tol and the atol tolerance matter for a stable count.

Follow-up Questions Guidance

  • How does the count change as C goes from very large to very small, and is there a value of C below which every coefficient is zero?
  • Two features are highly correlated. What does L1 tend to do with them, and how would elastic net change that?
  • Why was the data standardized first, and what happens to the zero count if one feature is on a much larger scale than the rest?
  • liblinear also penalizes the intercept. When does that matter, and how would you reduce its effect?