8000 [MRG] MNT use check_scalar to validate scalar in SpectralClustering by hvassard · Pull Request #21881 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG] MNT use check_scalar to validate scalar in SpectralClustering #21881

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
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
5 changes: 5 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ Changelog
is faster on many datasets, and its results are identical, hence the change.
:pr:`21735` by :user:`Aurélien Geron <ageron>`.

- |Enhancement| :class:`cluster.SpectralClustering` now raises consistent
error messages when passed invalid values for `n_clusters`, `n_init`,
`gamma`, `n_neighbors`, `eigen_tol` or `degree`.
:pr:`21881` by :user:`Hugo Vassard <hvassard>`.

:mod:`sklearn.cross_decomposition`
..................................

Expand Down
53 changes: 52 additions & 1 deletion sklearn/cluster/_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Wei LI <kuantkid@gmail.com>
# Andrew Knyazev <Andrew.Knyazev@ucdenver.edu>
# License: BSD 3 clause

import numbers
import warnings

import numpy as np
Expand All @@ -14,7 +16,7 @@
from scipy.sparse import csc_matrix

from ..base import BaseEstimator, ClusterMixin
from ..utils import check_random_state, as_float_array
from ..utils import as_float_array, check_random_state, check_scalar
from ..utils.deprecation import deprecated
from ..metrics.pairwise import pairwise_kernels
from ..neighbors import kneighbors_graph, NearestNeighbors
Expand Down Expand Up @@ -662,6 +664,55 @@ def fit(self, X, y=None):
"set ``affinity=precomputed``."
)

check_scalar(
self.n_clusters,
"n_clusters",
target_type=numbers.Integral,
min_val=1,
include_boundaries="left",
)

check_scalar(
self.n_init,
"n_init",
target_type=numbers.Integral,
min_val=1,
include_boundaries="left",
)

check_scalar(
self.gamma,
"gamma",
target_type=numbers.Real,
min_val=1.0,
include_boundaries="left",
)

check_scalar(
self.n_neighbors,
"n_neighbors",
target_type=numbers.Integral,
min_val=1,
include_boundaries="left",
)

if self.eigen_solver == "arpack":
check_scalar(
self.eigen_tol,
"eigen_tol",
target_type=numbers.Real,
min_val=0,
include_boundaries="left",
)

check_scalar(
self.degree,
"degree",
target_type=numbers.Integral,
min_val=1,
include_boundaries="left",
)

if self.affinity == "nearest_neighbors":
connectivity = kneighbors_graph(
X, n_neighbors=self.n_neighbors, include_self=True, n_jobs=self.n_jobs
Expand Down
52 changes: 52 additions & 0 deletions sklearn/cluster/tests/test_spectral.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
except ImportError:
amg_loaded = False

centers = np.array([[1, 1], [-1, -1], [1, -1]]) + 10
X, _ = make_blobs(
n_samples=60,
n_features=2,
centers=centers,
cluster_std=0.4,
shuffle=True,
random_state=0,
)


@pytest.mark.parametrize("eigen_solver", ("arpack", "lobpcg"))
@pytest.mark.parametrize("assign_labels", ("kmeans", "discretize", "cluster_qr"))
Expand Down Expand Up @@ -102,6 +112,48 @@ def test_spectral_unknown_assign_labels():
spectral_clustering(S, n_clusters=2, random_state=0, assign_labels="<unknown>")


@pytest.mark.parametrize(
"input, params, err_type, err_msg",
[
(X, {"n_clusters": -1}, ValueError, "n_clusters == -1, must be >= 1"),
(X, {"n_clusters": 0}, ValueError, "n_clusters == 0, must be >= 1"),
(
X,
{"n_clusters": 1.5},
TypeError,
"n_clusters must be an instance of <class 'numbers.Integral'>,"
" not <class 'float'>",
),
(X, {"n_init": -1}, ValueError, "n_init == -1, must be >= 1"),
(X, {"n_init": 0}, ValueError, "n_init == 0, must be >= 1"),
(
X,
{"n_init": 1.5},
TypeError,
"n_init must be an instance of <class 'numbers.Integral'>,"
" not <class 'float'>",
),
(X, {"gamma": -1}, ValueError, "gamma == -1, must be >= 1"),
(X, {"gamma": 0}, ValueError, "gamma == 0, must be >= 1"),
(X, {"n_neighbors": -1}, ValueError, "n_neighbors == -1, must be >= 1"),
(X, {"n_neighbors": 0}, ValueError, "n_neighbors == 0, must be >= 1"),
(
X,
{"eigen_tol": -1, "eigen_solver": "arpack"},
ValueError,
"eigen_tol == -1, must be >= 0",
),
(X, {"degree": -1}, ValueError, "degree == -1, must be >= 1"),
(X, {"degree": 0}, ValueError, "degree == 0, must be >= 1"),
],
)
Comment on lines +115 to +149
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for this extensive test!

def test_spectral_params_validation(input, params, err_type, err_msg):
"""Check the parameters validation in `SpectralClustering`."""
est = SpectralClustering(**params)
with pytest.raises(err_type, match=err_msg):
est.fit(input)

4BB6

@pytest.mark.parametrize("assign_labels", ("kmeans", "discretize", "cluster_qr"))
def test_spectral_clustering_sparse(assign_labels):
X, y = make_blobs(
Expand Down
0