Skip to content

ENH Minimum norm solution for the newton-cholesky GLM solver, alternative to #34339 - #35196

Closed
cakedev0 wants to merge 28 commits into
scikit-learn:mainfrom
cakedev0:glm/newton_min_norm
Closed

cakedev0 wants to merge 28 commits into
scikit-learn:mainfrom
cakedev0:glm/newton_min_norm

Conversation

@cakedev0

@cakedev0 cakedev0 commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

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 how finalize computes the minimum norm solution:

  1. No n_samples x n_samples matrix. scipy.linalg.null_space does 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.
  2. Sparse X. finalize raised ValueError: zero-dimensional arrays cannot be concatenated for a sparse X with collinear features (newton-cholesky accepts CSR/CSC). On main, those fits fall back to lbfgs and work.
  3. Penalized fits are not projected. ENH minimum norm solution for newton-cholesky solver in GLMs #34339 also projected penalized fits that raised a LinAlgWarning (e.g. alpha=1e-17 with a full one-hot encoding), which moves them away from the penalized optimum. Their solution is unique, so the projection now only runs for alpha=0.
  4. The intercept is not part of the norm, as for LinearRegression (centering) and as the limit of the L2 penalty for alpha -> 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, fitting 1000 * y instead of y changed every dummy coefficient. Now only the intercept changes, by log(1000).
  5. Zero sample weights are equivalent to removing samples. The null space of [X, 1] ignored sample_weight, so zero-weight rows could hide the collinearity of the other rows.
  6. Singular Hessians that LAPACK does not flag. finalize only projected after scipy.linalg.solve raised a LinAlgWarning, 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. finalize now checks the eigenvalues of the Hessian for every unpenalized fit. That's one eigvalsh of 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.
  7. One LinAlgWarning per fit, raised in finalize: 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 if inner_solve met one. Before, whether you got a warning for a rank deficient X depended on LAPACK.

Also:

  • test_logistic_regression_solvers_multiclass_unpenalized looped over a set and called pytest.xfail when reaching newton-cd-gram, so the solvers after it in hash order were never checked and the test always reported XFAIL.
  • The alpha docstrings said X "must have full column rank" for alpha=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.
  • New tests: the minimum norm solution and its single warning for a full one-hot encoding (dense and sparse, including the singular Hessians LAPACK misses), zero sample weights, and the single warning for multiclass.

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:

lorentzenchr and others added 26 commits June 20, 2026 09:58
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]>
… 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]>
@github-actions

Copy link
Copy Markdown

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

cakedev0 and others added 2 commits October 21, 2026 17:27
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)

@lorentzenchr lorentzenchr Oct 26, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit reluctant to always compute the eigenvalues. Is this really that cheap?

@cakedev0

cakedev0 commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

Closed in favor of #34339

@cakedev0 cakedev0 closed this Nov 3, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants