Skip to content

MAINT Parameters validation for cluster.estimate_bandwidth #24869

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions sklearn/cluster/_mean_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from numbers import Integral, Real

from collections import defaultdict
from ..utils._param_validation import Interval
from ..utils._param_validation import Interval, validate_params
from ..utils.validation import check_is_fitted
from ..utils.fixes import delayed
from ..utils import check_random_state, gen_batches, check_array
Expand All @@ -30,11 +30,22 @@
from .._config import config_context


@validate_params(
{
"X": ["array-like"],
"quantile": [Interval(Real, 0, 1, closed="both")],
"n_samples": [Interval(Integral, 1, None, closed="left"), None],
"random_state": ["random_state"],
"n_jobs": [Integral, None],
}
)
def estimate_bandwidth(X, *, quantile=0.3, n_samples=None, random_state=0, n_jobs=None):
"""Estimate the bandwidth to use with the mean-shift algorithm.

Copy link
Member

@glemaitre glemaitre Nov 10, 2022

Choose a reason for hiding this comment

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

I think that we should update the comment below:

This function takes time at least quadratic in `n_samples`. For large
datasets, it is wise to subsample by setting `n_samples`. Alternatively,
the parameter `bandwidth` can be set to a small value without estimating
it.

That this function takes time at least quadratic in n_samples. For large
datasets, it's wise to set that parameter to a small value.
This function takes time at least quadratic in `n_samples`. For large
datasets, it is wise to subsample by setting `n_samples`. Alternatively,
the parameter `bandwidth` can be set to a small value without estimating
it.

Parameters
----------
Expand Down
10 changes: 0 additions & 10 deletions sklearn/cluster/tests/test_mean_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import warnings
import pytest

from scipy import sparse

from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_allclose

Expand Down Expand Up @@ -76,14 +74,6 @@ def test_mean_shift(
assert cluster_centers.dtype == global_dtype


def test_estimate_bandwidth_with_sparse_matrix():
# Test estimate_bandwidth with sparse matrix
X = sparse.lil_matrix((1000, 1000))
msg = "A sparse matrix was passed, but dense data is required."
with pytest.raises(TypeError, match=msg):
estimate_bandwidth(X)


def test_parallel(global_dtype):
centers = np.array([[1, 1], [-1, -1], [1, -1]]) + 10
X, _ = make_blobs(
Expand Down
1 change: 1 addition & 0 deletions sklearn/tests/test_public_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


PARAM_VALIDATION_FUNCTION_LIST = [
"sklearn.cluster.estimate_bandwidth",
"sklearn.cluster.kmeans_plusplus",
"sklearn.metrics.accuracy_score",
"sklearn.metrics.mean_absolute_error",
Expand Down