Fit L2-Regularized Logistic Regression and Return the Coefficient Norm for a Given C

Quick Overview

Fit an L2-regularized logistic regression in scikit-learn and return the Euclidean norm of its coefficients for a given C. It tests how the inverse regularization strength shrinks the weights, why the norm is monotone in C, and why feature scaling matters.

Fit L2-Regularized Logistic Regression and Return the Coefficient Norm for a Given C

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def coef_l2_norm_at_C(X: np.ndarray, y: np.ndarray, C: float) -> float`. Fit a binary `sklearn.linear_model.LogisticRegression` on `X` (shape `(n_samples, n_features)`, float64) and `y` (int labels in `{0, 1}`), and return the Euclidean norm of the fitted coefficients. - Fit with exactly these settings: `penalty='l2'`, `C=C`, `solver='lbfgs'`, `fit_intercept=True`, `max_iter=5000`, `tol=1e-10`. - Return `float(np.linalg.norm(model.coef_.ravel()))`: a Python `float` (not a NumPy scalar or 0-d array), with the intercept excluded. - The function is tested with `C` in `{0.001, 0.1, 1.0, 100.0}`, and the norm must shrink monotonically toward 0 as `C` shrinks. ```hint Where C enters Write down the objective scikit-learn minimizes for this model and look at which term `C` multiplies. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n_samples, n_features)`, and `y` holds int labels 0 and 1 with both classes present. - The tests use `C` values of 0.001, 0.1, 1.0 and 100.0. ### What a Strong Answer Covers - The exact model settings, and a plain float for the coefficient norm with the intercept left out. - Where `C` appears in the objective and why a smaller value pulls the coefficients toward zero. - Why the norm is monotone in `C`, and why tight convergence settings matter for seeing that trend. - How feature scale affects a comparison of norms. ### Follow-up Questions - Why does a smaller `C` shrink the coefficients? - Why standardize features before comparing L2 norms across `C`? - How would you choose `C` for a model you plan to deploy?

Overview: Fit an L2-regularized logistic regression in scikit-learn and return the Euclidean norm of its coefficients for a given C. It tests how the inverse regularization strength shrinks the weights, why the norm is monotone in C, and why feature scaling matters.

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

Implement def coef_l2_norm_at_C(X: np.ndarray, y: np.ndarray, C: float) -> float. Fit a binary sklearn.linear_model.LogisticRegression on X (shape (n_samples, n_features), float64) and y (int labels in {0, 1}), and return the Euclidean norm of the fitted coefficients.

  • Fit with exactly these settings: penalty='l2' , C=C , solver='lbfgs' , fit_intercept=True , max_iter=5000 , tol=1e-10 .
  • Return float(np.linalg.norm(model.coef_.ravel())) : a Python float (not a NumPy scalar or 0-d array), with the intercept excluded.
  • The function is tested with C in {0.001, 0.1, 1.0, 100.0} , and the norm must shrink monotonically toward 0 as C shrinks.

Constraints & Assumptions

  • X is float64 with shape (n_samples, n_features) , and y holds int labels 0 and 1 with both classes present.
  • The tests use C values of 0.001, 0.1, 1.0 and 100.0.

What a Strong Answer Covers Guidance

  • The exact model settings, and a plain float for the coefficient norm with the intercept left out.
  • Where C appears in the objective and why a smaller value pulls the coefficients toward zero.
  • Why the norm is monotone in C , and why tight convergence settings matter for seeing that trend.
  • How feature scale affects a comparison of norms.

Follow-up Questions Guidance

  • Why does a smaller C shrink the coefficients?
  • Why standardize features before comparing L2 norms across C ?
  • How would you choose C for a model you plan to deploy?