Skip to content

[BUG] Fix default clusterer predict_proba columns and check predict_proba agrees with predict - #3933

Open
MatthewMiddlehurst wants to merge 17 commits into
aeon-toolkit:mainfrom
MatthewMiddlehurst:mm/predict-proba-columns
Open

MatthewMiddlehurst wants to merge 17 commits into
aeon-toolkit:mainfrom
MatthewMiddlehurst:mm/predict-proba-columns

Conversation

@MatthewMiddlehurst

@MatthewMiddlehurst MatthewMiddlehurst commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #2802. Supersedes #3166,

What does this implement/fix? Explain your changes.

The default BaseClusterer._predict_proba numbered its columns by the clusters present in X. When X missed a cluster, the columns shifted, so the argmax of predict_proba disagreed with predict and the width changed between calls. A negative label collapsed every case into one column. This affected every clusterer that uses the default, such as TimeSeriesKMeans, KASBA and KShape, and wrappers such as SummaryClusterer that fall back to it.

  • _predict_proba now uses labels as column indices, with max(n_clusters, labels_.max() + 1) columns. Negative labels in labels_, such as noise from a wrapped MeanShift(cluster_all=False), get no column. A negative prediction raises a ValueError.
  • check_clusterer_output check that predict returns a label with the highest predict_proba probability. Ties are accepted, since many estimators break ties at random in predict.
  • check_clusterer_output repeats that check on test cases missing a cluster, and checks that labels are non-negative integers below n_clusters that appear in labels_.
  • check_clusterer_uses_all_clusters fails on empty clusters, following scikit-learn's check_clustering. It is a separate check so it can be excluded on its own.

Any other comments?

  • Labels that are not cluster indices are no longer mapped, so labels [3, 22, 32] give 33 columns instead of 3. predict is documented to return cluster indices, and no aeon clusterer returns labels like these.

The default BaseClusterer._predict_proba renumbered predictions by the
clusters present in X, so when X missed a cluster the argmax of
predict_proba disagreed with predict, and the width could change between
calls when n_clusters was None. The in-place renumbering also collapsed
every case into one column when a label was negative.

Columns now follow the fitted clusterer: predictions are used directly
when they and labels_ are indices in [0, n_clusters), otherwise they map
to the columns of np.unique(labels_), which keeps support for labels that
are not cluster indices (aeon-toolkit#1739) and gives negative labels their own
column. A predicted label missing from labels_ raises a ValueError.
@aeon-actions-bot aeon-actions-bot Bot added bug Something isn't working clustering Clustering package labels Oct 21, 2026
@aeon-actions-bot

Copy link
Copy Markdown
Contributor

Thank you for contributing to aeon

I have added the following labels to this PR based on the title: bug.
I have added the following labels to this PR based on the changes made: clustering. Feel free to change these if they do not properly represent the PR.

The Checks tab will show the status of our automated tests. You can click on individual test runs in the tab or "Details" in the panel below to see more information if there is a failure.

If our pre-commit code quality check fails, please run pre-commit locally and push the fixes to your PR branch.

Don't hesitate to ask questions on the aeon Discord channel if you have any.

PR CI actions

These checkboxes will add labels to enable or disable CI functionality for this PR. This may not take effect immediately, and a new commit may be required to run the new configuration.

  • Run pre-commit checks for all files
  • Run mypy typecheck tests
  • Run all pytest tests and configurations
  • Run all notebook example tests
  • Run numba-disabled codecov tests
  • Disable numba cache loading
  • Regenerate expected results for testing
  • Push an empty commit to re-run CI checks

@MatthewMiddlehurst
MatthewMiddlehurst marked this pull request as draft October 21, 2026 18:22
satwiksps and others added 7 commits October 21, 2026 19:28
… cluster

The clusterers excluded from the predict_proba argmax check failed because
of the default _predict_proba column bug fixed earlier in this branch, not
from non-determinism, so they no longer need excluding. Also check the
argmax on test cases not assigned to the first predicted cluster, which
catches columns that depend on the clusters present in X, and fix the
classifier exclusion comment.
The classifiers excluded from the predict_proba argmax check were not
inconsistent. The TDE variants and HIVECOTEV2 are unseeded in their test
parameters and break nearest neighbour ties at random, so two predict calls
can disagree. The rest break exact probability ties at random, while argmax
takes the first. Seed the clone in check_classifier_output and accept any
class tied for the highest probability, so no exclusions are needed.
… clusterers

Move the comparison of predict with the highest predict_proba probability
into _assert_predict_is_max_probability, used by both output checks. Any
tied label is accepted, and labels map to columns through classes_ for
classifiers and directly as indices for clusterers. Also seed the clone in
check_clusterer_output so repeated predict calls agree.
Negative labels such as noise are not clusters. Build the default
predict_proba columns from the non-negative labels_ only, and raise a
ValueError if _predict returns a negative label. Before, a wrapped
MeanShift(cluster_all=False), which has -1 in labels_ but never predicts
it, got an always-zero noise column that shifted every cluster's column
by one.
…ests

Simplify the default _predict_proba to use labels directly as column
indices, with max(n_clusters, max(labels_) + 1) columns, and raise if
predict returns a negative label or one with no column. This drops the
mapping for labels that are not cluster indices, which aeon's _predict
contract does not allow.

Add two general clusterer checks, following scikit-learn's
check_clustering: check_clusterer_labels tests that labels_ and predict on
unseen data are non-negative integers below n_clusters and that predicted
labels appear in labels_, and check_clusterer_uses_all_clusters tests that
labels_ has no empty clusters and has n_clusters clusters when set. They
are separate so the stricter check can be excluded on its own.
predict and fit do not validate labels, so drop the out-of-range check
from the default _predict_proba and rely on check_clusterer_labels. Keep
the negative label check, as numpy would otherwise silently put -1 in the
last column. Also split the width calculation over a few lines.
Move the label validity assertions into check_clusterer_output, which
already fits on every data type, so they need no extra fit. Keep
check_clusterer_uses_all_clusters separate, so the stricter rule can be
excluded on its own. Yield check_clusterer_saving_loading_deep_learning
once per class instead of once per instance, and lay the yields out like
the classification checks. Break up the check_clusterer_output docstring.

Move the label test clusterer from test_base.py into the mock estimators
as MockLabelCluster, and list it and MockTransductiveCluster in the API
reference.
@MatthewMiddlehurst MatthewMiddlehurst changed the title [BUG] Map default clusterer predict_proba columns to the fitted clusters [BUG] Fix default clusterer predict_proba columns and check predict_proba agrees with predict Oct 24, 2026
@MatthewMiddlehurst MatthewMiddlehurst added the testing Testing related issue or pull request label Oct 24, 2026
@MatthewMiddlehurst
MatthewMiddlehurst marked this pull request as ready for review October 24, 2026 12:33

@TonyBagnall TonyBagnall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

one tiny non blocking comment, otherwsie lgtm

X, _ = make_example_3d_numpy(
n_cases=20, n_channels=1, n_timepoints=10, random_state=0
)
km = TimeSeriesKMeans(n_clusters=3, random_state=0).fit(X)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe change to euclidean and averaging_method to mean to be faster?

@TonyBagnall

Copy link
Copy Markdown
Contributor

@MatthewMiddlehurst small conflict

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working clustering Clustering package testing Testing related issue or pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH] Ensure that predict and predict_proba produce comparable output

3 participants