Skip to content

FIX prevent overflow in StandardScaler.partial_fit for large values - #34874

Closed
ShamikOfficial wants to merge 2 commits into
scikit-learn:mainfrom
ShamikOfficial:fix-incremental-mean-var-overflow-5602
Closed

ShamikOfficial wants to merge 2 commits into
scikit-learn:mainfrom
ShamikOfficial:fix-incremental-mean-var-overflow-5602

Conversation

@ShamikOfficial

@ShamikOfficial ShamikOfficial commented Sep 2, 2026 •

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #5602.

What does this implement/fix? Explain your changes.

_incremental_mean_and_var updates running statistics with
last_sum = last_mean * last_sample_count. For features with very large
values or long incremental streams, that product can overflow to inf,
which then poisons the mean and variance used by
StandardScaler.partial_fit.

This keeps the sum-based update as the primary path (more accurate in the
normal case), but falls back to an algebraically equivalent delta-based
update from Chan, Golub & LeVeque (1979) whenever the sum-based result is
not finite. The same idea is applied to the variance correction term.

Constant-feature detection also wraps its upper-bound check in
np.errstate(over="ignore") so it does not spuriously warn when the
mean itself is huge.

Introduce yourself

I use scikit-learn for applied ML and numerical/data pipelines. I picked
#5602 because it is a long-standing numerical correctness bug in a
widely used estimator, with a clear overflow mechanism and a standard
fallback algorithm.

This is among my first scikit-learn pull requests.

AI usage disclosure

I used AI assistance for:

  • Research and understanding
  • Test/benchmark generation
  • Drafting parts of the implementation

I reviewed, ran, and understand all of the changes in this PR.

Any other comments?

  • Changelog fragment is named 34874.fix.rst (PR number).
  • Local coverage includes overflow regression tests for
    _incremental_mean_and_var and end-to-end StandardScaler.partial_fit.

StandardScaler.partial_fit used last_mean * last_sample_count which
overflows for large values or long streams. Fall back to a delta-based
update (Chan et al.) when the sum-based result is non-finite.

Fixes scikit-learn#5602
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

Towncrier requires upcoming_changes fragments to use the pull request
number, not the issue number.

Co-authored-by: Cursor <[email protected]>
@ShamikOfficial

Copy link
Copy Markdown
Author

Closing this for now to free the open-PR slot — no maintainer feedback yet, and I'm focusing on a separate ready fix (#27777 / HuberRegressor ConvergenceWarning). Happy to reopen later if there's interest.

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: StandardScaler partial_fit overflows

1 participant