Repository navigation
Conversation
4efb87c to
32c87de
Compare
| # implementation or the random seed this can either lead to a bad | ||
| # estimate of the covariance or a ValueErorr caused by a LinalgError in | ||
| # the Cholesky decomposition. | ||
| pytest.xfail("The 'tied' covariance type fails to estimates the covariance.") |
There was a problem hiding this comment.
For information, I naively tried to hack the tied covariance estimation as follows:
diff --git a/sklearn/mixture/_gaussian_mixture.py b/sklearn/mixture/_gaussian_mixture.py
index cf8896ce7c..093aa6ce2a 100644
--- a/sklearn/mixture/_gaussian_mixture.py
+++ b/sklearn/mixture/_gaussian_mixture.py
@@ -202,7 +202,10 @@ def _estimate_gaussian_covariances_tied(resp, X, nk, means, reg_covar):
avg_means2 = np.dot(nk * means.T, means)
covariance = avg_X2 - avg_means2
covariance /= nk.sum()
- covariance.flat[:: len(covariance) + 1] += reg_covar
+ diag = np.diag(covariance)
+ diag = np.clip(diag, 0, None)
+ diag += reg_covar
+ covariance.flat[:: len(covariance) + 1] = diag
return covariance
But this does not fix this particular test failure.
Calling the full method and averaging the estimated covariance matrices across the components makes the test pass, but it's not as computationally efficient as the current code (and I am not sure that it's more numerically stable in general).
|
I am not sure if clipping for the tied/diagonal covariance cases is always a good idea. It does remove some negative rounding errors, but there still are errors in the other direction that are not clipped. Another alternative would be to accept that fitting GMM on unscaled float32 data is fundamentally problematic and that scaling or converting the input to float64 is the best way forward. |
|
We should probably not merge this PR as is: I had not realized that Let's close this PR and instead open PRs to:
|
Hackish tentative fix for #30382.
I am not sure if this is the correct way to handle this but would like to discuss it (if the tests pass).