Skip to content

[MRG] Leave KernelKMeans unfitted when every initialization fails - #790

Merged
charavelg merged 1 commit into
tslearn-team:mainfrom
wleeaf:fix-kernelkmeans-all-inits-empty
Nov 12, 2026
Merged

charavelg merged 1 commit into
tslearn-team:mainfrom
wleeaf:fix-kernelkmeans-all-inits-empty

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #788

When every initialization of KernelKMeans raises EmptyClusterError, fit() returned normally with _X_fit = None but kept the labels_ of the last failed attempt (assigned in _fit_one_init before the empty-cluster check raises), which has fewer than n_clusters clusters. No warning was emitted unless verbose=True, fit_predict() returned those labels, and predict() passed check_is_fitted(self, "_X_fit") because the attribute exists, then failed with AttributeError: 'NoneType' object has no attribute 'shape'.

The open PR #769 fixes the same problem for KShape and TimeSeriesKMeans through TimeSeriesCentroidBasedClusteringMixin._post_fit. KernelKMeans has its own retry loop and does not use that mixin, so this PR gives it the same behaviour:

Tests:

  • New 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, NotFittedError from predict and None from fit_predict. It also checks that a later successful fit on the same instance works. The test fails on main and passes with this change.
  • The existing n_clusters=101 case in test_kernel_kmeans now expects the warning and checks that labels_ is None.
  • Added a CHANGELOG entry.

This PR and #769 change different parts of kmeans.py and tests/test_clustering.py. Both add the same sklearn.exceptions import line at the same place in the test module.

When every initialization raised EmptyClusterError, fit() kept the
labels_ of the last failed attempt and predict() failed with an
AttributeError. Reset labels_ and inertia_, emit a ConvergenceWarning
and make predict() raise NotFittedError, as #769 does for KShape and
TimeSeriesKMeans.

Fixes #788
Copilot AI balanced review requested due to automatic review settings October 24, 2026 04:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.58%. Comparing base (ee25250) to head (3acfc87).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@charavelg
charavelg merged commit cd4f522 into tslearn-team:main Nov 12, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KernelKMeans.fit() silently returns invalid labels_ when every initialization hits an empty cluster

3 participants