Measure how L2 logistic regression collapses to the base rate as C goes to 0
Quick Overview
Fit an L2-regularized logistic regression in scikit-learn and measure how far its predicted probabilities sit from the training base rate as the inverse regularization strength C shrinks. Tests how the penalty treats weights and the intercept, and how regularization strength scales with sample size.
Measure how L2 logistic regression collapses to the base rate as C goes to 0
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `def base_rate_gap(X: np.ndarray, y: np.ndarray, C: float) -> float`. `X` is standardized float64 of shape `(n_samples, n_features)` and `y` has labels in `{0, 1}`. Fit a binary logistic regression and return the maximum absolute deviation of the fitted positive-class probabilities from `y.mean()`.
- Fit with exactly `penalty='l2'`, `C=C`, `solver='lbfgs'`, `fit_intercept=True`, `max_iter=20000`, `tol=1e-12`.
- Return `float(np.max(np.abs(model.predict_proba(X)[:, 1] - y.mean())))` as a native Python `float`.
```python
gap = base_rate_gap(X, y, C=1e-6)
# gap is close to 0.0: the coefficients are crushed and the model predicts the base rate for every row
gap = base_rate_gap(X, y, C=1.0)
# gap > 0: the coefficients are free and the predictions spread away from y.mean()
```
```hint Look at what the penalty touches
Write out the objective that scikit-learn minimizes with `lbfgs` and check which fitted parameters appear in the L2 term.
```
```hint The intercept's own equation
Set the derivative of the objective with respect to the intercept to zero. What does that condition say about the average fitted probability, for any value of `C`?
```
### Constraints & Assumptions
- `X` is float64 with standardized columns; `y` contains both classes.
- The grader calls the function with `C` in `{1e-6, 1e-3, 1.0}` and checks that the gap shrinks monotonically toward 0 as `C` decreases.
### What a Strong Answer Covers
- Fits with exactly the given hyperparameters and returns the maximum absolute deviation as a Python float.
- States which parameters the L2 penalty shrinks under `lbfgs` and which it leaves free.
- Identifies the model the fit approaches as `C` goes to 0 and the value its intercept converges to.
- Explains why the gap falls as `C` decreases, what controls how quickly it falls, and how far the monotone behavior can be guaranteed.
### Follow-up Questions
- Why does only the intercept survive as `C` goes to 0?
- How would the result change with `fit_intercept=False`?
- Would the same limit hold with `solver='liblinear'`? Why or why not?
- Why might `C=1e-6` fail to give a near-zero gap on a dataset with millions of rows?
Overview: Fit an L2-regularized logistic regression in scikit-learn and measure how far its predicted probabilities sit from the training base rate as the inverse regularization strength C shrinks. Tests how the penalty treats weights and the intercept, and how regularization strength scales with sample size.
Measure how L2 logistic regression collapses to the base rate as C goes to 0
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0
Implement def base_rate_gap(X: np.ndarray, y: np.ndarray, C: float) -> float. X is standardized float64 of shape (n_samples, n_features) and y has labels in {0, 1}. Fit a binary logistic regression and return the maximum absolute deviation of the fitted positive-class probabilities from y.mean().
Fit with exactly
penalty='l2'
,
C=C
,
solver='lbfgs'
,
fit_intercept=True
,
max_iter=20000
,
tol=1e-12
.
Return
float(np.max(np.abs(model.predict_proba(X)[:, 1] - y.mean())))
as a native Python
float
.
gap = base_rate_gap(X, y, C=1e-6)
# gap is close to 0.0: the coefficients are crushed and the model predicts the base rate for every row
gap = base_rate_gap(X, y, C=1.0)
# gap > 0: the coefficients are free and the predictions spread away from y.mean()
Constraints & Assumptions
X
is float64 with standardized columns;
y
contains both classes.
The grader calls the function with
C
in
{1e-6, 1e-3, 1.0}
and checks that the gap shrinks monotonically toward 0 as
C
decreases.
What a Strong Answer Covers Guidance
Fits with exactly the given hyperparameters and returns the maximum absolute deviation as a Python float.
States which parameters the L2 penalty shrinks under
lbfgs
and which it leaves free.
Identifies the model the fit approaches as
C
goes to 0 and the value its intercept converges to.
Explains why the gap falls as
C
decreases, what controls how quickly it falls, and how far the monotone behavior can be guaranteed.
Follow-up Questions Guidance
Why does only the intercept survive as
C
goes to 0?
How would the result change with
fit_intercept=False
?
Would the same limit hold with
solver='liblinear'
? Why or why not?
Why might
C=1e-6
fail to give a near-zero gap on a dataset with millions of rows?