Count Zero Coefficients Along an L1 Logistic Regression Path Over C

Quick Overview

Fit a fresh L1-penalized logistic regression for each value in a grid of C values and return the number of zero coefficients at each one as an int64 array. It tests how regularization strength controls sparsity, reproducible liblinear settings, and careful reasoning about how an L1 path behaves.

Count Zero Coefficients Along an L1 Logistic Regression Path Over C

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `l1_sparsity_path(X, y, Cs)`. For each `C` in `Cs`, fit a fresh binary L1 logistic regression and count its (numerically) zero coefficients. Return only the zero counts. ```python def l1_sparsity_path(X: np.ndarray, y: np.ndarray, Cs: np.ndarray) -> np.ndarray: ``` The function must: - Fit `LogisticRegression` 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 `np.sum(np.isclose(model.coef_.ravel(), 0.0, atol=1e-8))` (the intercept is not counted). - Return an `np.ndarray` of shape `(len(Cs),)`, dtype `np.int64`: element `i` is the zero count at `Cs[i]`. Example of the output format: ```python Cs = np.array([0.01, 0.1, 1.0, 10.0]) l1_sparsity_path(X, y, Cs) # array([18, 11, 4, 1]) dtype int64, shape (4,) ``` The counts depend on `X` and `y`, so this output only shows the shape of the answer: one int64 count per value of `C`, in the same order as `Cs`. ```hint Keep the fits independent Each value of `C` gets its own model. Decide what, if anything, should carry over from one iteration to the next. ``` ```hint Check the shape of the path Larger `C` means a weaker penalty, and the example counts fall as `C` grows. Before your code depends on that pattern, ask whether an L1 path guarantees it. ``` ### Constraints & Assumptions - `X` has shape `(n_samples, n_features)` and is fit as given; the function does not scale it. - `y` has shape `(n_samples,)`, uses labels 0 and 1, and contains both classes. - `Cs` is a 1-D array of positive floats. The example lists them in ascending order, but element `i` of the result must match `Cs[i]` whatever the order. ### What a Strong Answer Covers - A fresh fit for each `C` with exactly the required settings, visited in the order given by `Cs`. - Counting near-zero entries of `coef_` with the given tolerance, with the intercept left out. - Returning an int64 array of shape `(len(Cs),)`. - Why the zero count tends to fall as `C` grows, and whether that is guaranteed at every step of an L1 path. - The cost of running one independent fit per value of `C`. ### Follow-up Questions - Why does `solver='liblinear'` matter for the L1 path here? - Why standardize `X` before fitting an L1 model? - How would you choose a sensible grid of `C` values, starting from the point where the first coefficient becomes non-zero? - Can a coefficient that is non-zero at one `C` return to zero at a larger `C`? What would that tell you about the features?

Overview: Fit a fresh L1-penalized logistic regression for each value in a grid of C values and return the number of zero coefficients at each one as an int64 array. It tests how regularization strength controls sparsity, reproducible liblinear settings, and careful reasoning about how an L1 path behaves.

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

Implement l1_sparsity_path(X, y, Cs). For each C in Cs, fit a fresh binary L1 logistic regression and count its (numerically) zero coefficients. Return only the zero counts.

def l1_sparsity_path(X: np.ndarray, y: np.ndarray, Cs: np.ndarray) -> np.ndarray:

The function must:

  • Fit LogisticRegression 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 np.sum(np.isclose(model.coef_.ravel(), 0.0, atol=1e-8)) (the intercept is not counted).
  • Return an np.ndarray of shape (len(Cs),) , dtype np.int64 : element i is the zero count at Cs[i] .

Example of the output format:

Cs = np.array([0.01, 0.1, 1.0, 10.0])
l1_sparsity_path(X, y, Cs)
# array([18, 11, 4, 1])  dtype int64, shape (4,)

The counts depend on X and y, so this output only shows the shape of the answer: one int64 count per value of C, in the same order as Cs.

Constraints & Assumptions

  • X has shape (n_samples, n_features) and is fit as given; the function does not scale it.
  • y has shape (n_samples,) , uses labels 0 and 1, and contains both classes.
  • Cs is a 1-D array of positive floats. The example lists them in ascending order, but element i of the result must match Cs[i] whatever the order.

What a Strong Answer Covers Guidance

  • A fresh fit for each C with exactly the required settings, visited in the order given by Cs .
  • Counting near-zero entries of coef_ with the given tolerance, with the intercept left out.
  • Returning an int64 array of shape (len(Cs),) .
  • Why the zero count tends to fall as C grows, and whether that is guaranteed at every step of an L1 path.
  • The cost of running one independent fit per value of C .

Follow-up Questions Guidance

  • Why does solver='liblinear' matter for the L1 path here?
  • Why standardize X before fitting an L1 model?
  • How would you choose a sensible grid of C values, starting from the point where the first coefficient becomes non-zero?
  • Can a coefficient that is non-zero at one C return to zero at a larger C ? What would that tell you about the features?