Elastic-Net Logistic Regression Zero-Coefficient Mask

Quick Overview

Implement a function that fits a binary elastic-net logistic regression in scikit-learn with a fixed saga configuration and returns a boolean mask of the coefficients that end up exactly zero. It tests how the L1 part of the penalty creates sparsity and how C and l1_ratio control it.

Elastic-Net Logistic Regression Zero-Coefficient Mask

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def elasticnet_zero_mask(X, y, l1_ratio)` that fits a binary elastic-net logistic regression and reports which coefficients are exactly zero. `X` has shape `(n_samples, n_features)` (float64, already standardized: do not re-standardize) and `y` has shape `(n_samples,)` with labels in `{0, 1}`. Requirements: - Fit `LogisticRegression` with exactly `penalty='elasticnet'`, `l1_ratio=l1_ratio`, `C=0.1`, `solver='saga'`, `fit_intercept=True`, `max_iter=50000`, `tol=1e-8`, `random_state=0`. - Return an `np.ndarray` of shape `(n_features,)`, dtype `bool`. - Element `j` is `np.isclose(model.coef_.ravel(), 0.0, atol=1e-6)[j]`. ```hint Solver and shape Check which solver accepts `penalty='elasticnet'`, and look at the shape of `coef_` for a binary problem before you compare it with zero. ``` ### 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}`. - `l1_ratio` is a float in `[0, 1]`: the share of the penalty that is L1. ### What a Strong Answer Covers - Builds the model with every pinned argument, including the `saga` solver that elastic net needs. - Flattens `coef_` from `(1, n_features)` to `(n_features,)` and returns a boolean array of that shape, using the specified `np.isclose` tolerance. - Explains why the L1 part of the penalty produces exact zeros while the L2 part only shrinks. - Describes how `l1_ratio` and `C` change the number of zeros. - Names edge cases such as `l1_ratio=0`, a fit that hits `max_iter`, and re-standardizing the input. ### Follow-up Questions - Why does L1 produce exact zeros while L2 does not? - How would `C` interact with `l1_ratio` in controlling sparsity? - If the fit stops at `max_iter` with a `ConvergenceWarning`, how far can you trust the mask? - At the same `C`, would doubling the number of samples tend to give more or fewer zero coefficients, and why?

Overview: Implement a function that fits a binary elastic-net logistic regression in scikit-learn with a fixed saga configuration and returns a boolean mask of the coefficients that end up exactly zero. It tests how the L1 part of the penalty creates sparsity and how C and l1_ratio control it.

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

Implement def elasticnet_zero_mask(X, y, l1_ratio) that fits a binary elastic-net logistic regression and reports which coefficients are exactly zero. X has shape (n_samples, n_features) (float64, already standardized: do not re-standardize) and y has shape (n_samples,) with labels in {0, 1}.

Requirements:

  • Fit LogisticRegression with exactly penalty='elasticnet' , l1_ratio=l1_ratio , C=0.1 , solver='saga' , fit_intercept=True , max_iter=50000 , tol=1e-8 , random_state=0 .
  • Return an np.ndarray of shape (n_features,) , dtype bool .
  • Element j is np.isclose(model.coef_.ravel(), 0.0, atol=1e-6)[j] .

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} .
  • l1_ratio is a float in [0, 1] : the share of the penalty that is L1.

What a Strong Answer Covers Guidance

  • Builds the model with every pinned argument, including the saga solver that elastic net needs.
  • Flattens coef_ from (1, n_features) to (n_features,) and returns a boolean array of that shape, using the specified np.isclose tolerance.
  • Explains why the L1 part of the penalty produces exact zeros while the L2 part only shrinks.
  • Describes how l1_ratio and C change the number of zeros.
  • Names edge cases such as l1_ratio=0 , a fit that hits max_iter , and re-standardizing the input.

Follow-up Questions Guidance

  • Why does L1 produce exact zeros while L2 does not?
  • How would C interact with l1_ratio in controlling sparsity?
  • If the fit stops at max_iter with a ConvergenceWarning , how far can you trust the mask?
  • At the same C , would doubling the number of samples tend to give more or fewer zero coefficients, and why?