Repository navigation
[BUG] Fix default clusterer predict_proba columns and check predict_proba agrees with predict - #3933
MatthewMiddlehurst wants to merge 17 commits into
Conversation
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.
Thank you for contributing to
|
…tch22Clusterer, KASBA) to allow CI to pass
… 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.
TonyBagnall
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
maybe change to euclidean and averaging_method to mean to be faster?
|
@MatthewMiddlehurst small conflict |
Reference Issues/PRs
Fixes #2802. Supersedes #3166,
What does this implement/fix? Explain your changes.
The default
BaseClusterer._predict_probanumbered its columns by the clusters present inX. WhenXmissed a cluster, the columns shifted, so the argmax ofpredict_probadisagreed withpredictand the width changed between calls. A negative label collapsed every case into one column. This affected every clusterer that uses the default, such asTimeSeriesKMeans,KASBAandKShape, and wrappers such asSummaryClustererthat fall back to it._predict_probanow uses labels as column indices, withmax(n_clusters, labels_.max() + 1)columns. Negative labels inlabels_, such as noise from a wrappedMeanShift(cluster_all=False), get no column. A negative prediction raises aValueError.check_clusterer_outputcheck thatpredictreturns a label with the highestpredict_probaprobability. Ties are accepted, since many estimators break ties at random inpredict.check_clusterer_outputrepeats that check on test cases missing a cluster, and checks that labels are non-negative integers belown_clustersthat appear inlabels_.check_clusterer_uses_all_clustersfails on empty clusters, following scikit-learn'scheck_clustering. It is a separate check so it can be excluded on its own.Any other comments?