Repository navigation
Conversation
The centered column norm cancelled catastrophically: for a constant column, whose mean is only accurate up to rounding, it was a tiny positive or even negative value instead of 0, and coordinate descent diverged on it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
cakedev0
added a commit
to cakedev0/my-scikit-learn-backlog
that referenced
this pull request
Nov 3, 2026
todo: move [sklearn#35295](scikit-learn/scikit-learn#35295) from Now to Waiting, lorentzenchr's review addressed todo: [fork#40](cakedev0/scikit-learn#40) is the fix for the red CI of [sklearn#34339](scikit-learn/scikit-learn#34339), waiting on lorentzenchr Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nalty Co-Authored-By: Claude Opus 5.5 <[email protected]>
lorentzenchr
reviewed
Nov 6, 2026
lorentzenchr
left a comment
There was a problem hiding this comment.
Could you check if this has a small performance impact, in particular on the multitask lasso? (super simple benchmark is sufficient)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
Draft on my fork, before opening it upstream.
Found through the red CI of scikit-learn#34339: it adds sparse X to
test_glm_regression_unpenalized_hstacked_X, whose X has a column of ones whenfit_intercept=True, andnewton-cddiverges on it for ~4% of the seeds. The bug is already on main, sparse X just wasn't tested there.What does this implement/fix? Explain your changes.
newton-cddiverges on sparse X with a constant column and an intercept:For sparse X, coordinate descent centers implicitly, with
X_meanpassed on the side. With sample weights,R_and_X_colnorm2_sparsecomputed the centered squared norm of a column assum_nz(sw * (x - m)**2 - sw * m**2) + sw_sum * m**2, which cancels catastrophically. For a constant column, the norm should be 0 but it isn't. The formula alone gives a nonzero norm even whenmis exactly 1 (266/1000 random weights, 12 samples). In real fitsmis only accurate up to rounding as well: on 300newton-cdfits with 100 samples, it isn't exactly 1 in 7667/7759 calls and the norm isn't 0 in 7582/7759 calls, typically a few1e-15and sometimes negative. Coordinate descent only skips a column whose norm is exactly 0, so it updates this one by dividing a rounding error by another, and the coefficients blow up within one call.The fix is in a helper now shared with
R_and_X_colnorm2_multi_task, which had the same formula:sum_nz(sw * (x - m)**2) + (sw_sum - sw_nz_sum) * m**2, the second term only when the column has implicit zeros.sklearn.preprocessing._data._is_constant_feature(the oneStandardScaleruses), except that it usesFLT_EPSILONfor float32 where_is_constant_featurealways uses the float64 eps. See the known problem below.Both are needed. On 200
newton-cdfits with sparse X and a constant column, 23 diverge or differ from dense X on main, 27 with the first change only, and 0 with both.Sparse
LassoandElasticNetdiverge too, withalpha=0andsample_weight. Withoutsample_weightI saw no wrong fit (0/500 forLasso). On 100 seeds with 100 samples, Linux x86-64:PoissonRegressor(alpha=0, solver="newton-cd")LogisticRegression(C=np.inf, solver="newton-cd")Lasso(alpha=0)The
newton-cd-csr_matrixcases oftest_glm_regression_unpenalized_hstacked_Xfrom scikit-learn#34339 (all seeds) fail 37/1600 times on main and 0/1600 with this PR.Known problem, to fix before opening this upstream
The bound also catches real columns, and this is a regression on float32. With float32, a column of mean 1 and std 0.05, and 1M samples,
Lasso(alpha=1e-6)gives a coef of 20.0 on main (same as dense) but 0 with this PR, with aConvergenceWarning:The bound grows with
(n_samples * mean * eps)**2, so it triggers whenstd / mean < n_samples * eps.Not a regression: the same kind of float64 column is already wrong on main (0 instead of ~9e4 for mean 1e6, std 1e-5, 1M samples), and a sparse float32 Poisson fit on a low-variance column is wrong on both (-1.9e14 on main, 0 with this PR, 29.9 on dense).
Pending decision: how to fix this. Using the float64 eps in the bound wouldn't catch float32 constant columns, so there are two options:
I'd go for the exact check.
Any other comments?
The same cancellation (sum of squares minus
n * mean**2) is in other places, not fixed here. It loses ~eps * (mean / std)**2in relative precision, so it shows on features with a large offset:RidgeCVwith sparse X (X.T @ X - sw_sum * X_mean X_mean.T): with a feature of mean 1e7 and std 1, its coef is 1.60 instead of 0.999 (dense).Ridge(solver="sparse_cg")is fine.r_regressionandf_regression, dense and sparse X: r = 0.733 instead of 0.703 at mean 1e7. For constant columns, it's f_regression takes square root of negative values when constant columns are present scikit-learn/scikit-learn#11395.AI usage disclosure
I used AI assistance for:
🤖 Generated with Claude Code