Repository navigation
[MRG] Leave KernelKMeans unfitted when every initialization fails - #790
Merged
charavelg merged 1 commit intoNov 12, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #790 +/- ##
==========================================
+ Coverage 96.56% 96.58% +0.02%
==========================================
Files 89 89
Lines 9478 9511 +33
==========================================
+ Hits 9152 9186 +34
+ Misses 326 325 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #788
When every initialization of
KernelKMeansraisesEmptyClusterError,fit()returned normally with_X_fit = Nonebut kept thelabels_of the last failed attempt (assigned in_fit_one_initbefore the empty-cluster check raises), which has fewer thann_clustersclusters. No warning was emitted unlessverbose=True,fit_predict()returned those labels, andpredict()passedcheck_is_fitted(self, "_X_fit")because the attribute exists, then failed withAttributeError: 'NoneType' object has no attribute 'shape'.The open PR #769 fixes the same problem for
KShapeandTimeSeriesKMeansthroughTimeSeriesCentroidBasedClusteringMixin._post_fit.KernelKMeanshas its own retry loop and does not use that mixin, so this PR gives it the same behaviour:fitsetslabels_toNoneandinertia_toinf, as [MRG] Leave KShape/TimeSeriesKMeans unfitted when every initialization fails #769 does._X_fitis alreadyNone,n_iter_stays 0 andn_features_in_is not set.fitalso emits aConvergenceWarningworded like the one in [MRG] Leave KShape/TimeSeriesKMeans unfitted when every initialization fails #769.KernelKMeansimplements__sklearn_is_fitted__(fitted if and only if_X_fit is not None), andpredictcallscheck_is_fitted(self), so it raisesNotFittedErrorin that state. Deserialized models restore_X_fit, so they are unaffected.Tests:
test_kernel_kmeans_all_inits_empty_cluster, using the data from the issue's reproduction (15 series with 3 distinct values,n_clusters=4). It checks the warning, the reset attributes,NotFittedErrorfrompredictandNonefromfit_predict. It also checks that a later successful fit on the same instance works. The test fails onmainand passes with this change.n_clusters=101case intest_kernel_kmeansnow expects the warning and checks thatlabels_isNone.This PR and #769 change different parts of
kmeans.pyandtests/test_clustering.py. Both add the samesklearn.exceptionsimport line at the same place in the test module.