Repository navigation
FIX prevent overflow in _incremental_mean_and_var for large values - #34664
Closed
EdenRochmanSharabi wants to merge 3 commits into
Closed
EdenRochmanSharabi wants to merge 3 commits into
EdenRochmanSharabi wants to merge 3 commits into
Conversation
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.
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! |
This was referenced Oct 27, 2026
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
Fixes #5602. See also #11549 (closed, addressed only the mean).
What does this implement/fix? Explain your changes.
_incremental_mean_and_varoverflows when the accumulated sum (last_mean * last_sample_count) exceeds the float64 range, which happens for long streams or large-valued features. This turnsStandardScaler.partial_fitstatistics intoinf/naneven 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:
xp.where(isfinite(sum_mean), sum_mean, shift_mean)with the shift formlast_mean + delta * (new_count / total_count)._is_constant_feature: silenced a spurious overflow warning in the upper-bound computation (an infinite bound is semantically correct there)._incremental_mean_and_varlevel, one atStandardScalerlevel) usingnp.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.