Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example notebook for using aeon distances with sklearn clusterers #2511

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

SalmanDeveloperz
Copy link

Reference Issues/PRs

Fixes #1241

What does this implement/fix? Explain your changes.

This pull request introduces a new Jupyter Notebook: sklearn_clustering_with_aeon_distances.ipynb. The notebook demonstrates how to integrate aeon's distance metrics with scikit-learn clustering algorithms. It includes:

Hierarchical Clustering: Using AgglomerativeClustering with metric="precomputed".
Density-Based Clustering: Using DBSCAN and OPTICS with metric="precomputed".
Spectral Clustering: Using SpectralClustering with affinity="precomputed" and the inverse of the distance matrix as the similarity matrix.
This addition enhances the clustering documentation, showing how to combine aeon’s distance metrics with widely-used scikit-learn clusterers.

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

No new dependencies introduced.

Any other comments?

  • The notebook has been tested locally, and all cells execute without errors.

  • A reference to this notebook has been added to the clustering section of the documentation.

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.

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@aeon-actions-bot aeon-actions-bot bot added the examples Example notebook related label Jan 22, 2025
@aeon-actions-bot
Copy link
Contributor

Thank you for contributing to aeon

I did not find any labels to add based on the title. Please add the [ENH], [MNT], [BUG], [DOC], [REF], [DEP] and/or [GOV] tags to your pull requests titles. For now you can add the labels manually.
I have added the following labels to this PR based on the changes made: [ $\color{#45FD64}{\textsf{examples}}$ ]. 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

@SalmanDeveloperz
Copy link
Author

Dear maintainers,
I have created a new notebook and would like to know where to add its reference in the following documentation:

Clustering Overview
Clustering with sklearn.cluster
Could you please guide me on the most appropriate sections in these files to include the reference to the new notebook? I want to ensure it integrates well with the existing content before committing the changes.
Looking forward to your advice.
Best Regards

@TonyBagnall
Copy link
Contributor

Dear maintainers, I have created a new notebook and would like to know where to add its reference in the following documentation:

Clustering Overview Clustering with sklearn.cluster Could you please guide me on the most appropriate sections in these files to include the reference to the new notebook? I want to ensure it integrates well with the existing content before committing the changes. Looking forward to your advice. Best Regards

hi, thanks for this, we will take a look

@@ -0,0 +1,274 @@
{
Copy link
Member

@SebastianSchmidl SebastianSchmidl Jan 25, 2025

Choose a reason for hiding this comment

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

  • In the beginning, the details on how to use the sklearn estimators are not necessary. Please remove the "with metric=..."-stuff from the introduction and the TOC.
  • The TOC does not render correctly
  • There is no new content in the _Introduction_-section; can be removed.
  • Please rename the section "Loading Data" to "Example Dataset"

Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Resolved conflicts and updated the notebook as per the review:

  • Removed unnecessary "metric=..." details.
  • Renamed "Loading Data" to "Example Dataset."
  • Deleted the redundant "Introduction" section.
    Please let me know if there are any additional changes required.

@@ -0,0 +1,274 @@
{
Copy link
Member

@SebastianSchmidl SebastianSchmidl Jan 25, 2025

Choose a reason for hiding this comment

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

Why so complicated? You could just do

from aeon.datasets import load_unit_test
X, y = load_unit_test(split="train")
# [...]

Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Simplified dataset loading as per your feedback. Let me know if further changes are needed!

@@ -0,0 +1,274 @@
{
Copy link
Member

@SebastianSchmidl SebastianSchmidl Jan 25, 2025

Choose a reason for hiding this comment

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

Please link to https://www.aeon-toolkit.org/en/stable/api_reference/distances.html for an overview of all aeon distances.


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Added a link to the aeon distances API reference in the "Computing Distance Matrices with aeon" section as required

@@ -0,0 +1,274 @@
{
Copy link
Member

@SebastianSchmidl SebastianSchmidl Jan 25, 2025

Choose a reason for hiding this comment

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

Please include:

  • AgglomerativeClustering is, as the name suggests, an agglomerative approach that works by merging clusters bottom up.
  • Not all linkage methods can be used with a precomputed distance matrix. single,complete,average , and weighted work with aeon distances.

Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Just updated the "Hierarchical Clustering" section with an explanation of AgglomerativeClustering and the supported linkage methods for precomputed distance matrices, as required. Let me know if further changes are required

@@ -0,0 +1,274 @@
{
Copy link
Member

@SebastianSchmidl SebastianSchmidl Jan 25, 2025

Choose a reason for hiding this comment

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

For all estimators: Please link to the corresponding scikit-learn documentation page.


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Added links to the corresponding scikit-learn documentation pages for AgglomerativeClustering, DBSCAN, OPTICS, and SpectralClustering, as requred. Let me know if there are additional updates required

@@ -0,0 +1,274 @@
{
Copy link
Member

@SebastianSchmidl SebastianSchmidl Jan 25, 2025

Choose a reason for hiding this comment

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

Line #7.    inverse_distance_matrix = 1 /(distance_matrix + epsilon)

Unfortunately, this conversion approach does not preserve the properties of the distance distribution.

aeon "distances" (they are rather dissimilarities) are in the interval $[0, \infty)$. Thus, we first need to normalize the values to $[0, 1]$, and can, then, simply subtract from 1 to convert to similarities: inverse_distance_matrix = 1 - (distance_matrix / distance_matrix.max())


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, for the feedback. Just updated the distance-to-similarity conversion to normalize the distance matrix to [0, 1] and subtract from 1, as required. This ensures that the properties of the distance distribution are preserved. Let me know if further updates are required!

@@ -0,0 +1,274 @@
{
Copy link
Member

@SebastianSchmidl SebastianSchmidl Jan 25, 2025

Choose a reason for hiding this comment

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

You do not cite any of those references. Please add inline references or remove.


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Removed the references section as it was not cited in the notebook, as suggested.

@SebastianSchmidl SebastianSchmidl added documentation Improvements or additions to documentation clustering Clustering package distances Distances package labels Jan 25, 2025
@SebastianSchmidl
Copy link
Member

Dear maintainers, I have created a new notebook and would like to know where to add its reference in the following documentation:

Clustering Overview Clustering with sklearn.cluster Could you please guide me on the most appropriate sections in these files to include the reference to the new notebook? I want to ensure it integrates well with the existing content before committing the changes. Looking forward to your advice. Best Regards

Please refer to the links in my comment of the corresponding issue.

- Removed "metric=..." details from TOC and introduction.
- Renamed "Loading Data" to "Example Dataset."
- Deleted redundant Introduction section.
"For a comprehensive overview of all available distance metrics in aeon, see the aeon distances API reference."
"AgglomerativeClustering is, as the name suggests, an agglomerative approach that works by merging clusters bottom-up."
Clarified Supported Linkage Methods:

Included the supported linkage methods (single, complete, average, weighted) for precomputed distance matrices.
…subtract from 1, ensuring proper preservation of distance distribution.
…istances.ipynb) in the Clustering Overview under Clustering Notebooks.
@SalmanDeveloperz
Copy link
Author

Please refer to the links in my comment of the corresponding issue.

Thank you for the guidance! I have added a reference in the clustering.ipynb notebook under the Clustering Notebooks section, as suggested.

Please let me know if there are additional updates or adjustments required!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clustering Clustering package distances Distances package documentation Improvements or additions to documentation examples Example notebook related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DOC] Hierarchical, spectral, or density-based clustering using sklearn and aeon distance metrics
3 participants