Skip to content

[ENH] NearestCentroidClassifier#3527

Open
chrisholder wants to merge 2 commits into
soft-barycentre-averagingfrom
nearest-centroid-classifier
Open

[ENH] NearestCentroidClassifier#3527
chrisholder wants to merge 2 commits into
soft-barycentre-averagingfrom
nearest-centroid-classifier

Conversation

@chrisholder

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fourth and final PR of the soft-distance stack. Stacked on #3526 (soft barycentre averaging) → #3508#3483. Please review/merge those first; this branch targets #3526 and the diff shrinks as they merge.

What does this implement/fix?

Adds NearestCentroidClassifier, a nearest-centroid (Rocchio) time series classifier. It computes one centroid per class by averaging that class's training series, then classifies a new series by the nearest class centroid under a chosen elastic distance.

  • Naming. Called NearestCentroidClassifier to avoid colliding with sklearn.neighbors.NearestCentroid (the same reason aeon's KNN is KNeighborsTimeSeriesClassifier).
  • Defaults. distance="dtw" and average_method=None, which resolves to DBA (petitjean) for ordinary distances and to the gradient-based soft barycentre for soft distances.
  • Soft coupling. A soft distance auto-promotes to average_method="soft"; explicitly pairing a soft distance with a non-soft averaging method (or vice versa) raises ValueError. Centroids are computed through elastic_barycenter_average, so this validation is shared with the averaging module rather than re-implemented.
  • Full numpydoc docstring, _tags, _get_test_params, a runnable Examples doctest, and an entry in the classification API reference.

Does your contribution introduce a new dependency?

No.

Test plan

  • New test_nearest_centroid.py: per-class centroid shapes, label prediction, the soft auto-promote, the validation matrix (soft+hard and hard+soft both raise), the DBA default, and that mean vs DBA centroids differ on warped data.
  • Passes aeon's general estimator checks (check_estimator, 22/22) and the doctest.
  • Verified locally with euclidean/dtw/soft-DTW/soft-MSM configurations. Pre-commit passes.

@aeon-actions-bot aeon-actions-bot Bot added classification Classification package enhancement New feature, improvement request or other non-bug code enhancement labels Jun 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: [ enhancement ].
I have added the following labels to this PR based on the changes made: [ classification ]. 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

Add a nearest-centroid (Rocchio) time series classifier. It builds one centroid
per class by averaging the class's training series, then assigns a test series
to the nearest class centroid under a chosen elastic distance.

- Named `NearestCentroidClassifier` to avoid colliding with
  `sklearn.neighbors.NearestCentroid` (mirrors `KNeighborsTimeSeriesClassifier`).
- Defaults to `distance="dtw"` with `average_method=None`, which resolves to
  DBA (petitjean) for ordinary distances and to the gradient-based soft
  barycentre for soft distances. A soft distance and `average_method="soft"`
  must be used together; mismatches raise `ValueError`.
- Centroids are computed via `elastic_barycenter_average` (or `mean_average`
  for `"mean"`), so the soft-coupling validation is shared with the averaging
  module.
- Full numpydoc docstring, tags, `_get_test_params`, unit tests, and an entry
  in the classification API reference. Passes the general estimator checks.
- add a `random_state` constructor parameter, forwarded to the barycentre
  averaging (mirroring how `n_jobs` is passed). Previously a seed could only be
  set via the undiscoverable `average_params={"random_state": ...}`. It affects
  the stochastic averaging methods and random init only.
- add tests: multivariate fit+predict end-to-end, and reproducibility under a
  fixed random_state with a stochastic averaging method.
@chrisholder chrisholder force-pushed the soft-barycentre-averaging branch from 13fcd3c to 4a175f4 Compare June 23, 2026 18:24
@chrisholder chrisholder force-pushed the nearest-centroid-classifier branch from 2c2f901 to 82305d1 Compare June 23, 2026 18:24
@chrisholder chrisholder marked this pull request as ready for review June 23, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

classification Classification package enhancement New feature, improvement request or other non-bug code enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant