Skip to content

FIX (HDBSCAN) : FutureWarning for copy parameter deprecation - #31973

Merged
jeremiedbb merged 21 commits into
scikit-learn:mainfrom
Sarthakpurii:hdbscan-copy-deprecated
Sep 9, 2025
Merged

jeremiedbb merged 21 commits into
scikit-learn:mainfrom
Sarthakpurii:hdbscan-copy-deprecated

Conversation

@Sarthakpurii

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #31907

What does this implement/fix? Explain your changes.

What’s the bug:
When using metric = 'precomputed', scikit-learn’s HDBSCAN modified the input matrix in place.
This breaks the expected contract that input data passed to fit remain unchanged.
On further evaluation, copy parameter in all estimators are set to True, HDBSCAN is the only estimator having copy = False as default, breaking the overall consistency.

What this PR does:

  • Changes the default copy parameter from False to "warn".
  • Allows "warns" in _parameter_constraints for copy to enable the new default.
  • In fit(), warns the user about default value change deprecation of copy parameter, if copy if not explicitly defined by the user. Additionally, it sets the copy to False if it is "warn" given the current default value of copy.
  • Updates class documentation regarding the copy parameter deprecation in version 1.10 .

Why:
This ensures the consistency of HDBSCAN's copy parameter with scikit-learn other estimators. Also, it will fix the default working of HDBSCAN to not alter the original matrix if the given matrix is precomputed matrix in the future version (1.10).

@github-actions

github-actions Bot commented Aug 19, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 2d19d3e. Link to the linter CI: here

@Sarthakpurii
Sarthakpurii force-pushed the hdbscan-copy-deprecated branch from 4067f81 to f648148 Compare August 20, 2025 00:48
@Sarthakpurii

Copy link
Copy Markdown
Contributor Author

@adrinjalali @Micky774 for the DocTest of HDBSCAN which approach should I consider to get over the FutureWarning being raised? I can silence the warning like this in\doc\conftest.py:

def pytest_collection_modifyitems(config, items):
    .
    .
    for item in items:
        if isinstance(item, DoctestItem):
            item.dtest.globs = {}
            if "hdbscan.py" in str(item.fspath):
                item.add_marker(pytest.mark.filterwarnings(
                    "ignore:The default value of `copy` will change from False to True in 1.10"
                ))

or I can modify the DocTest code in HDBSCAN class documentation like hdb = HDBSCAN(min_cluster_size=20, copy=True) in order that it does not trigger the warning.

Additionally, for the Changelog, should I create 31973.fix.rst in \doc\whats_new\upcoming_changes\sklearn.cluster?

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

or I can modify the DocTest code in HDBSCAN class documentation like hdb = HDBSCAN(min_cluster_size=20, copy=True) in order that it does not trigger the warning.

That's an okay solution.

Additionally, for the Changelog, should I create 31973.fix.rst in \doc\whats_new\upcoming_changes\sklearn.cluster?

Yes

Comment thread sklearn/cluster/_hdbscan/hdbscan.py Outdated
"allow_single_cluster": ["boolean"],
"store_centers": [None, StrOptions({"centroid", "medoid", "both"})],
"copy": ["boolean"],
"copy": ["boolean", StrOptions({"warn"})],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

warn should be Hidden

Comment thread sklearn/cluster/_hdbscan/hdbscan.py Outdated
"""
if self.copy == "warn":
warn(
"The default value of `copy` will change from False to True in 1.10",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"The default value of `copy` will change from False to True in 1.10",
"The default value of `copy` will change from False to True in 1.10."
" Explicitly set a value for `copy` to silence this warning.",

Comment thread sklearn/cluster/_hdbscan/hdbscan.py Outdated
"The default value of `copy` will change from False to True in 1.10",
FutureWarning,
)
self._copy = False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

since this is only used in fit here, no need to set an attribute, could simply be a local variable.

Comment thread sklearn/cluster/tests/test_hdbscan.py Outdated
Comment on lines +31 to +33
pytestmark = pytest.mark.filterwarnings(
"ignore:The default value of `copy` will change from False to True in 1.10"
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

instead of this, the tests which raise should set the value of copy explicitly, and have a test which tests the right warning is raised at the right time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree we should create an additional test to check if the deprecation warning is being raised correctly. I'll add that test.

However, for the first part, that we set the value of copy explicitly - I think keeping the file level marker will be a better choice because this file contains only the HDBSCAN testcases and there are 25+ testcases which needs to have the copy parameter set explicitly. Also, in the future when the version 1.10 will be released, we will have to explicitly remove the copy parameter to get the default behavior back. So, I think we can keep this warning as it is (I'll update the changes in warning suggested above and a #TODO as well).

What do you think?
@adrinjalali

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

here we'd set the value of copy to what the new value would be (True) and therefore after 1.10 no change is needed. This is how we've handled other deprecation cases as well.



@pytest.mark.filterwarnings("ignore::sklearn.exceptions.ConvergenceWarning")
@pytest.mark.filterwarnings(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
@pytest.mark.filterwarnings(
# TODO(1.10): remove filter
@pytest.mark.filterwarnings(

Comment thread sklearn/cluster/_hdbscan/hdbscan.py Resolved
@Sarthakpurii

Copy link
Copy Markdown
Contributor Author

@adrinjalali I made the suggested changes. Please have a look.

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please submit a commit (can be empty) with a [doc build] message to trigger a full doc build to make sure there are no warnings there. You probably need to fix the usages of HDBSCAN in the docs first.

@Sarthakpurii
Sarthakpurii force-pushed the hdbscan-copy-deprecated branch from fafe75a to d8abea9 Compare August 22, 2025 13:50
@Sarthakpurii

Copy link
Copy Markdown
Contributor Author

@adrinjalali I am waiting for your review. Please check if everything is fine now.

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Maybe @jeremiedbb could have a look

@adrinjalali adrinjalali added the Waiting for Second Reviewer First reviewer is done, need a second one! label Aug 26, 2025

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I just pushed a commit to use copy=False when possible in the tests to reduce memory usage.

LGTM. Thanks @Sarthakpurii !

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

Labels

module:cluster Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HDBSCAN modifies input precomputed distance matrix

3 participants