Repository navigation
Conversation
Estimate diag and spherical variances as the weighted second moment about the component mean. Subtracting mean**2 from E[X**2] cancels in float32 and can be largely negative, so reg_covar cannot keep a collapsed component valid. Non-regression test for scikit-learngh-30382. Co-authored-by: Olivier Grisel <[email protected]>
Without the explicit release, the previous iteration's buffer was still alive while the next one was allocated, doubling the peak temporary. Co-authored-by: Olivier Grisel <[email protected]>
Keep the single-GEMM E[X**2] - mean**2 estimate and recompute, from the second moment about the mean, only the components whose variance falls below a relative threshold of E[X**2] (1e-2 for float32, 1e-8 for float64). Typical data keeps the previous speed; ill-conditioned components get the stable estimate. Non-regression test for scikit-learngh-30382. Co-authored-by: Olivier Grisel <[email protected]>
ogrisel
commented
Nov 19, 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 scikit-learn#30382.
What does this implement/fix? Explain your changes.
GaussianMixturewithcovariance_type="diag"or"spherical"computes the per-component variance asE[X**2] - mean**2. For float32 data with a large feature magnitude relative to its spread, the two terms cancel. The result can be largely negative, which makesfitfail even with a generousreg_covar, or can be far above the true variance (for example, 112 instead of about 0 forX = [[9999], [0]]).The change keeps the single-GEMM estimate as the fast path. A component is recomputed from the weighted second moment about its mean (the diagonal of the full-covariance estimator, which is already centered) when any feature has
variance <= rel_tol * E[X**2], withrel_tol = 1e-2for float32 and1e-8for float64.The threshold is relative rather than absolute because the fast path's error is governed by
variance / E[X**2], not by the variance alone.Things to check in review:
unstablecheck and per-component loop add a host sync on array-API backends that are not NumPy. Only NumPy array-API tests were run locally._estimate_gaussian_parameters, which this PR does not change.nkis padded by10 * eps, which slightly biases the fast path. This is unchanged.Changelog fragments are in
doc/whats_new/upcoming_changes/sklearn.mixture/andchanged-models/, currently named30382.*.rstafter the issue. They need renaming to the upstream PR number once it exists, because towncrier uses that number.Performance
Estimator timings, float32, 4-core VM:
fit, 15 EM iterations: 1.00x at 100k x 10 x 10; 1.27x at 50k x 50 x 10 fordiag/spherical. Always-centered computation would be about 2.6x.Peak memory is unchanged from the previous estimator (one
X-sized temporary).Tests
test_estimate_diag_covariance_float32_large_magnitude: positivity and agreement with a float64 reference on float32 data near 1e4.test_estimate_diag_covariance_matches_float64_reference: parametrized over offsets 0, 10 and 1e3, for float32 and float64. The offset-1e3 float32 case fails with the previous formula (relative error 2.4) and passes now (2.1e-6).test_gaussian_mixture_collapsed_components_large_float32: fits the issue's[[9999], [0]]example fordiagandspherical, in float32 and float64, and checks covariances are within1e-3ofreg_covar.test_gaussian_suffstat_sk_spherical: the per-component mean is now passed with shape(n_components, n_features), which is what the estimator expects.Run with
SKLEARN_RUN_FLOAT32_TESTS=1andSCIPY_ARRAY_API=1for the array-API cases:sklearn/mixturepasses (155 passed, 224 skipped for unavailable backends).AI usage disclosure
This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.
I used AI assistance for: