Skip to content

FIX prevent overflow in _incremental_mean_and_var for large values - #34664

Closed
EdenRochmanSharabi wants to merge 3 commits into
scikit-learn:mainfrom
EdenRochmanSharabi:fix/incremental-mean-var-overflow
Closed

EdenRochmanSharabi wants to merge 3 commits into
scikit-learn:mainfrom
EdenRochmanSharabi:fix/incremental-mean-var-overflow

Conversation

@EdenRochmanSharabi

@EdenRochmanSharabi EdenRochmanSharabi commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #5602. See also #11549 (closed, addressed only the mean).

What does this implement/fix? Explain your changes.

_incremental_mean_and_var overflows when the accumulated sum (last_mean * last_sample_count) exceeds the float64 range, which happens for long streams or large-valued features. This turns StandardScaler.partial_fit statistics into inf/nan even though the mean and variance themselves are representable.

The fix computes both a sum-based result (the current, most accurate path) and a delta-based result following Chan, Golub & LeVeque (1979), which never reconstructs cumulative sums. For each feature the sum-based value is used when it is finite; where it overflows, the delta-based value takes over. This makes the function bit-exact with the old implementation wherever the old code was finite, and correct where it overflowed.

Changes:

  • Mean update: xp.where(isfinite(sum_mean), sum_mean, shift_mean) with the shift form last_mean + delta * (new_count / total_count).
  • Variance correction term: same isfinite-fallback between the sum-based and delta-based forms of the Chan et al. cross-term.
  • _is_constant_feature: silenced a spurious overflow warning in the upper-bound computation (an infinite bound is semantically correct there).
  • Two non-regression tests (one at _incremental_mean_and_var level, one at StandardScaler level) using np.errstate(over="raise") to verify no overflow occurs.

First time contributor introduction

Not my first contribution.

AI usage disclosure

I used AI to checksum and verify human-written code.

Any other comments?

The sparse counterpart (utils/sparsefuncs_fast.incr_mean_variance_axis0) has the same sum-based structure and could benefit from an analogous fix. Left out of scope here to keep the PR focused.

When the accumulated sum (last_mean * last_sample_count) exceeds the
float64 range, StandardScaler.partial_fit returns inf/nan statistics.
Add a delta-based fallback (Chan, Golub & LeVeque 1979) that activates
per feature only where the sum-based result overflows, keeping the
function bit-exact with the old implementation elsewhere.
@EdenRochmanSharabi

Copy link
Copy Markdown
Contributor Author

Hi @adrinjalali, I opened #34664 to fix this issue but I'm unable to mark it as ready for review. GitHub shows "Author has reached the open pull request limit" even though I only have 1 non-draft PR open (plus 2 drafts). I've closed several PRs trying to get under the limit but it still won't let me convert the draft. Could you help me understand what the limit is or convert it from draft? Thanks!

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