Skip to content
Closed
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
31 changes: 24 additions & 7 deletions sklearn/neighbors/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
from ._base import NeighborsBase
from ._unsupervised import NearestNeighbors
from ..base import TransformerMixin, ClassNamePrefixFeaturesOutMixin
from ..utils._param_validation import StrOptions
from ..metrics.pairwise import distance_metrics
from ..utils._param_validation import (
StrOptions,
Interval,
Integral,
validate_params,
)
from ..utils.validation import check_is_fitted


Expand Down Expand Up @@ -224,6 +230,23 @@ def radius_neighbors_graph(
return X.radius_neighbors_graph(query, radius, mode)


@validate_params(
{
"mode": [StrOptions({"distance", "connectivity"}), None],
"n_neighbors": [Interval(Integral, 1, None, closed="left"), None],
"algorithm": [StrOptions({"auto", "ball_tree", "kd_tree", "brute"}), None],
"leaf_size": [Interval(Integral, 1, None, closed="left"), None],
"metric": [
StrOptions(set(distance_metrics())),
StrOptions({"minkowski"}),
callable,
None,
],
"p": [Interval(Integral, 1, None, closed="left"), None],
"metric_params": [dict, None],
"n_jobs": [Integral, None],
}
)
class KNeighborsTransformer(
ClassNamePrefixFeaturesOutMixin, KNeighborsMixin, TransformerMixin, NeighborsBase
):
Expand Down Expand Up @@ -342,12 +365,6 @@ class KNeighborsTransformer(
(178, 178)
"""

_parameter_constraints: dict = {
Copy link
Member

Choose a reason for hiding this comment

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

for classes, parameter validation for the constructor arguments is done via _parameter_constraints class attribute as you see here, and not via the @validate_params decorator.

And as you can see, it is implemented already for this class, so nothing left to be done for KNeighborsTransformer.

@validate_params is used for functions, and we're adding them to most public functions, for example, this one: #26086. Therefore this PR can be closed.

**NeighborsBase._parameter_constraints,
"mode": [StrOptions({"distance", "connectivity"})],
}
_parameter_constraints.pop("radius")

def __init__(
self,
*,
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 @@ -263,6 +263,7 @@ def _check_function_param_validation(
"sklearn.model_selection.permutation_test_score",
"sklearn.model_selection.train_test_split",
"sklearn.model_selection.validation_curve",
"sklearn.neighbors.KNeighborsTransformer",
"sklearn.neighbors.sort_graph_by_row_values",
"sklearn.preprocessing.add_dummy_feature",
"sklearn.preprocessing.binarize",
Expand Down