Repository navigation
Conversation
The null space now comes from the eigendecomposition of the weighted Gram matrix [X, 1]' diag(sw) [X, 1] instead of a full SVD of [X, 1]: - no n_samples x n_samples matrix, which ran out of memory on tall X; - works for sparse X; - zero sample weights are equivalent to removing samples. The projection is skipped for penalized problems, whose solution is unique, and the intercept is not part of the norm, like LinearRegression and the alpha -> 0 limit of the L2 penalty. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The loop over a set called pytest.xfail on reaching newton-cd-gram, so the solvers after it in hash order were never checked and the whole test always reported XFAIL. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… fit scipy.linalg.solve does not always flag an exactly singular Hessian, e.g. for a full one-hot encoding with an intercept (12 out of 50 random datasets), so the minimum norm projection did not run. finalize now checks the eigenvalues of the Hessian for every unpenalized fit, which costs one eigvalsh of shape (n_dof, n_dof) per fit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The LinAlgWarning of inner_solve depended on LAPACK flagging the Hessian, which it does not always do for a rank deficient X. finalize now raises a single warning: the rank of X when it found a null space (and returned the minimum norm solution), else the ill-conditioned Hessian message if inner_solve met one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
This PR exceeds the recommended size of 500 lines changed. Consider splitting it into smaller PRs to ease the review process. Please make sure you read our Contributing guide and in particular our Pull Request guidelines |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment on lines
+149
to
+159
| # The null space of diag(sqrt(sw)) Xe is the one of the Gram matrix | ||
| # G = Xe' diag(sw) Xe | ||
| # of shape (n_dof, n_dof), also for sparse X. Contrary to the Hessian | ||
| # Xe' diag(sw * h) Xe, it does not depend on the pointwise hessians h, which can | ||
| # underflow to zero for some samples. | ||
| G = np.empty((n_dof, n_dof), dtype=X.dtype) | ||
| G[:n_features, :n_features] = sandwich_dot(X, sample_weight) | ||
| if fit_intercept: | ||
| G[:n_features, -1] = G[-1, :n_features] = X.T @ sample_weight | ||
| G[-1, -1] = np.sum(sample_weight) | ||
| eigenvalues, eigenvectors = scipy.linalg.eigh(G) |
Member
There was a problem hiding this comment.
As said elsewhere, using the Hessian squares the condition number. This is numerically very unfavorable, especially for this particular case where we know something is off (= high condition number).
Note that the numerical precision is proportional to the condition number.
Comment on lines
+1000
to
+1004
| # Without penalty, the solution is not unique for rank deficient X and we | ||
| # pick the minimum norm one. As H = Xe' diag(sw * h) Xe, a rank deficient | ||
| # Xe makes the Hessian singular. LAPACK in inner_solve does not always | ||
| # detect it, so we check the eigenvalues of H, which is cheap compared to | ||
| # computing H. |
Member
There was a problem hiding this comment.
I am a bit reluctant to always compute the eigenvalues. Is this really that cheap?
Member
Author
|
Closed in favor of #34339 |
cakedev0
added a commit
to cakedev0/my-scikit-learn-backlog
that referenced
this pull request
Nov 3, 2026
drop: Linear: answer lorentzenchr's inline concerns on [sklearn#35196](scikit-learn/scikit-learn#35196) (minimum norm newton-cholesky), closed without merging in favor of [sklearn#34339](scikit-learn/scikit-learn#34339) todo: move [sklearn#35267](scikit-learn/scikit-learn#35267) from Now to Waiting, ArturoAmorQ's wording suggestions applied todo: david-cortes-intel's note on the OpenMP OpenBLAS build on [blog#228](scikit-learn/blog#228) Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
Alternative to #34339. This branch is #34339 (by @lorentzenchr) merged with main, plus the fixes below, so it also contains the commits of #34339. See my comments there for the context.
What does this implement/fix? Explain your changes.
It keeps the eigendecomposition fallback of #34339 in
inner_solve, and changes howfinalizecomputes the minimum norm solution:scipy.linalg.null_spacedoes a full SVD of[X, 1], with U of shape (n_samples, n_samples): 80 GB for 100k samples. The null space is now computed from the eigendecomposition of the weighted Gram matrix[X, 1]' diag(sw) [X, 1], of shape (n_dof, n_dof), which costs about one Hessian evaluation.finalizeraisedValueError: zero-dimensional arrays cannot be concatenatedfor a sparse X with collinear features (newton-cholesky accepts CSR/CSC). On main, those fits fall back to lbfgs and work.LinAlgWarning(e.g.alpha=1e-17with a full one-hot encoding), which moves them away from the penalized optimum. Their solution is unique, so the projection now only runs foralpha=0.LinearRegression(centering) and as the limit of the L2 penalty foralpha -> 0, which never penalizes the intercept (see FIX the tests for convergence to the minimum norm solution of unpenalized ridge / OLS #25948). With the intercept in the norm, the coefficients depend on the units of y: for a Poisson fit on a full one-hot encoding, fitting1000 * yinstead ofychanged every dummy coefficient. Now only the intercept changes, bylog(1000).[X, 1]ignoredsample_weight, so zero-weight rows could hide the collinearity of the other rows.finalizeonly projected afterscipy.linalg.solveraised aLinAlgWarning, but it doesn't always do so for an exactly singular Hessian: 21 out of 100 fits in the example from my comment on #34339, which are then not the minimum norm solution for either convention. With this PR, all 100 fits warn and return the minimum norm solution, with the intercept out of the norm.finalizenow checks the eigenvalues of the Hessian for every unpenalized fit. That's oneeigvalshof shape (n_dof, n_dof) per fit: ~0.1% of the fit time with 50 features, ~1.5% with 300, ~10% with 1000 (laptop). Penalized fits don't pay it.LinAlgWarningper fit, raised infinalize: the rank of X when it is rank deficient (and the minimum norm solution was returned), otherwise the "ill-conditioned Hessian" message of ENH minimum norm solution for newton-cholesky solver in GLMs #34339 ifinner_solvemet one. Before, whether you got a warning for a rank deficient X depended on LAPACK.Also:
test_logistic_regression_solvers_multiclass_unpenalizedlooped over asetand calledpytest.xfailwhen reaching newton-cd-gram, so the solvers after it in hash order were never checked and the test always reported XFAIL.alphadocstrings said X "must have full column rank" foralpha=0. They now say the solution is only unique in that case, and the newton-cholesky docs say when the minimum norm solution is returned.Not changed: multiclass (follow-up, as agreed in #34339), and the absolute
max_eval <= sqrt(eps)threshold of the eigendecomposition fallback.AI usage disclosure
I used AI assistance for: