Skip to content

[ENH] KNN n_jobs and updated kneighbours method#2578

Merged
chrisholder merged 229 commits intomainfrom
knn-update
Aug 12, 2025
Merged

[ENH] KNN n_jobs and updated kneighbours method#2578
chrisholder merged 229 commits intomainfrom
knn-update

Conversation

@chrisholder
Copy link
Copy Markdown
Contributor

Reference Issues/PRs

This PR depends on #2545

What does this implement/fix? Explain your changes.

This PR passes n_jobs to the pairwise distance function so that is now can run on multiple threads. In addition I changed the k_neighbors method to be compatible with sklearns which can be used in various ways. See: https://github.com/scikit-learn/scikit-learn/blob/98ed9dc73/sklearn/neighbors/_base.py#L763.

Does your contribution introduce a new dependency? If yes, which one?

Any other comments?

PR checklist

For all contributions
  • I've added myself to the list of contributors. Alternatively, you can use the @all-contributors bot to do this for you after the PR has been merged.
  • The PR title starts with either [ENH], [MNT], [DOC], [BUG], [REF], [DEP] or [GOV] indicating whether the PR topic is related to enhancement, maintenance, documentation, bugs, refactoring, deprecation or governance.
For new estimators and functions
  • I've added the estimator/function to the online API documentation.
  • (OPTIONAL) I've added myself as a __maintainer__ at the top of relevant files and want to be contacted regarding its maintenance. Unmaintained files may be removed. This is for the full file, and you should not add yourself if you are just making minor changes or do not want to help maintain its contents.
For developers with write access
  • (OPTIONAL) I've updated aeon's CODEOWNERS to receive notifications about future changes to these files.

@aeon-actions-bot aeon-actions-bot bot added classification Classification package distances Distances package enhancement New feature, improvement request or other non-bug code enhancement labels Feb 28, 2025
@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: [ $\color{#FEF1BE}{\textsf{enhancement}}$ ].
I have added the following labels to this PR based on the changes made: [ $\color{#BCAE15}{\textsf{classification}}$, $\color{#5209C9}{\textsf{distances}}$ ]. 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, any trivial fixes will automatically be pushed to your PR unless it is a draft.

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

PR CI actions

These checkboxes will add labels to enable/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
  • Stop automatic pre-commit fixes (always disabled for drafts)
  • Disable numba cache loading
  • Push an empty commit to re-run CI checks

@chrisholder chrisholder requested a review from dguijo as a code owner March 4, 2025 14:39
@chrisholder chrisholder added the no numba cache Disable numba cache loading on a PR label Aug 4, 2025
@chrisholder
Copy link
Copy Markdown
Contributor Author

For both the KNNClassifier and KNNRegressor I've replaced the _kneighbors function with a public kneighbors. There are two reasons I have made this change:

  1. It simplifies the code structure and makes it easier to understand
  2. It make it compatiable with sklearns API where they define a public kneighbors function. See: https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier.kneighbors

Copy link
Copy Markdown
Contributor

@TonyBagnall TonyBagnall left a comment

Choose a reason for hiding this comment

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

one comment

TonyBagnall
TonyBagnall previously approved these changes Aug 8, 2025
Copy link
Copy Markdown
Contributor

@TonyBagnall TonyBagnall left a comment

Choose a reason for hiding this comment

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

nice one

@MatthewMiddlehurst
Copy link
Copy Markdown
Member

Interested to see how this compares to the current threading.

TonyBagnall
TonyBagnall previously approved these changes Aug 11, 2025
Copy link
Copy Markdown
Contributor

@TonyBagnall TonyBagnall left a comment

Choose a reason for hiding this comment

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

lgtm

@chrisholder
Copy link
Copy Markdown
Contributor Author

chrisholder commented Aug 11, 2025

Just some very high level benchmarking increases number of cases with n_jobs = 4 comparing the main branch implementation and this PRs branch. In the graph dtw (before) and msm (before) are running the KNN on main. The dtw (after) and msm (after) is running the KNN on this PR.

image

There is no real noticeable performance difference here. The reason however, i'd advocate for the threading version in this PR over the one in main is because it achieves the same performance and requires no additional logic to be implemented on the model side. This means this model and other distance-based models can just pass n_jobs and not have to worry about implementing additional threaded logic.

@chrisholder chrisholder merged commit 2b46d54 into main Aug 12, 2025
19 checks passed
@chrisholder chrisholder deleted the knn-update branch August 12, 2025 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

classification Classification package distances Distances package enhancement New feature, improvement request or other non-bug code enhancement no numba cache Disable numba cache loading on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.