Skip to content

FIX Center X in GLMs and LogisticRegression for features with large offsets - #35030

Closed
cakedev0 wants to merge 31 commits into
scikit-learn:mainfrom
cakedev0:dense-mean-variance-guarded-centering
Closed

cakedev0 wants to merge 31 commits into
scikit-learn:mainfrom
cakedev0:dense-mean-variance-guarded-centering

Conversation

@cakedev0

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #35029

Alternative to #34793: includes its centering trick for Ridge cholesky, but guarded by an a priori check (as suggested in #34793 (comment)).

What does this implement/fix? Explain your changes.

  • Adds _dense_mean_variance_axis0, a Cython single-pass per-feature mean & variance for dense arrays. It is numerically stable for features with large offsets and its cost is close to a single np.mean (see benchmarks/bench_dense_mean_variance.py).
  • GLMs (PoissonRegressor, GammaRegressor, TweedieRegressor) and LogisticRegression (all solvers but liblinear, which penalizes the intercept): when fit_intercept=True and some feature has |mean| > 10 * std, fit on a centered copy of X and correct the intercept afterwards. Otherwise, no copy of X.
  • Ridge cholesky: centering trick from PRF Ridge - centering trick for cholesky solver #34793 (the Gram matrix is corrected algebraically instead of materializing a centered copy of X), but only when the mean/variance ratio of the features makes it numerically safe. Otherwise, fall back to explicit centering as in main.

Tests check the invariance of coef_ to feature offsets for all those estimators.

AI usage disclosure

I used AI assistance for:

  • Code generation
  • Test/benchmark generation
  • Research and understanding

🤖 Generated with Claude Code

cakedev0 and others added 28 commits August 21, 2026 22:21
… to svd

use_no_center_cholesky matched on solver in ("auto", "cholesky") but didn't
check the array namespace. With array API dispatch to a non-numpy namespace,
solver="auto" silently resolves to "svd" instead of "cholesky" (see
resolve_solver), which needs X to actually be centered. This left X
uncentered while running svd, causing
test_cross_val_predict_array_api_compliance[...-Ridge] failures on
array_api_strict and torch in CI.
This reverts commit 6d7549d.
This reverts commit f5ed039.
…rays

`_dense_mean_variance_axis0` reads each value of X once and accumulates in
float64. Values are accumulated by tiles around the running mean and tiles
are merged with Chan et al.'s pairwise update, which is stable for features
with a large offset relative to their standard deviation. C- and
F-contiguous inputs are processed without copies.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The algebraic centering of the cholesky solver computes
X.T @ X - n * outer(X_offset, X_offset), which suffers from catastrophic
cancellation for features with a large offset relative to their standard
deviation.

`_preprocess_data(skip_centering_if_safe=True)` now computes the mean and
variance of X in a single pass and only skips centering (and the copy of X)
when the rounding errors of working on uncentered data are small. Whether
centering was skipped is returned with `return_centering_skipped=True`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Compare Ridge cholesky against a float64 reference fitted on the same data:
for large offsets, the float32 mean computed by `solver="svd"` is not
accurate enough to be a reference. The tolerance on the intercept accounts
for the amplification of the errors on coef by the offset of the features.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ffsets

With an unpenalized intercept, the conditioning of the problem degrades like
1 + mean**2 / var: lbfgs, newton-cg, newton-cholesky, sag and saga silently
stop far from the optimum on features with a large offset relative to their
standard deviation.

Shifting X is an exact reparametrization, so these estimators now fit on
centered X when the offset of a feature exceeds 10 standard deviations, and
convert the intercept back (and the warm-started intercept to the centered
problem). liblinear is excluded as it penalizes the intercept. The step size
of sag/saga is computed again on the centered data.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
cakedev0 and others added 2 commits September 25, 2026 09:19
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Also skip float32 with offsets too large for float32 in Ridge cholesky test.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@cakedev0

Copy link
Copy Markdown
Member Author

Closing for now, I'm exploring some other ways, like cakedev0#38

@cakedev0 cakedev0 closed this Oct 30, 2026
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.

BUG: LogisticRegression (and other GLMs) can silently return badly wrong coefficients on off-centered data

2 participants