Skip to content

MAINT Parameter validation for sklearn.neighbors.neighbors_graph - #26568

Merged
jeremiedbb merged 11 commits into
scikit-learn:mainfrom
StefanieSenger:kneighbors_graph
Aug 30, 2023
Merged

jeremiedbb merged 11 commits into
scikit-learn:mainfrom
StefanieSenger:kneighbors_graph

Conversation

@StefanieSenger

Copy link
Copy Markdown
Member

Reference Issues/PRs

#24862

What does this implement/fix? Explain your changes.

Parameter validation for sklearn.neighbors.neighbors_graph

Any other comments?

Regarding "metric": "no_validation", I am not sure if then then param validation is entirely skipped if X is array-like.

If this was the case, I was wondering if instead I could import VALID_METRICS from the _base file and copy the code used there, which would be: "metric": [StrOptions(set(itertools.chain(*VALID_METRICS.values()))), callable],

?

@adrinjalali

Copy link
Copy Markdown
Member

ping @glemaitre

@glemaitre
glemaitre self-requested a review June 14, 2023 17:42

@glemaitre glemaitre 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.

Here are some comments to make some additional changes.

Comment thread sklearn/neighbors/_graph.py Resolved
Comment thread sklearn/neighbors/_graph.py Resolved
Comment thread sklearn/neighbors/_graph.py Resolved
Comment thread sklearn/neighbors/_graph.py Outdated

@validate_params(
{
"X": ["array-like", BallTree, KNeighborsMixin],

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 don't know if we should check for KNeighborsMixin or just an estimator that defines a kneighbors_graph function since this is what we expect in the implementation. I think that it would make more sense. @jeremiedbb WDYT?

We also need to update the documentation.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just something I noticed:, the current implementation would allow for KNeighborsClassifier and KNeighborsTransformer, but it would raise for RadiusNeighborsClassifier and RadiusNeighborsTransformer, which are otherwise models kneighbors_graph accepts.

So, either we want to add RadiusNeighborsMixin as well, or we individually add all those models.

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.

Oh yes, I see. Soo your solution of using a KNeighborsMixin is probably the safest then. We only impose third-party libraries to inherit from this mixin class.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It was @adrinjalali's suggestion. But don't we need to include RadiusNeighborsMixin as well?

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 the function is specialized for k-neighbors (kneighbors_graph) then I don't think it makes sense to add RadiusNeighborsMixin here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm, sorry to ask again, but kneighbors_graph is currently functioning with RadiusNeighborsClassifier and RadiusNeighborsTransformer as inputs for the X param, even though it might not have been intended to be used that way. (I believe there is radius_neighbors_graph for that purpose.)
So, if we now validate the param X not to be any of the radius neighbors estimators anymore, somebodie's code could break, correct?

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.

So, if we now validate the param X not to be any of the radius neighbors estimators anymore, somebodie's code could break, correct?

I have to think a bit more about it to know if this is a normal use case or if this is just a bug. If this is a feature then we need to be lenient; if this is a bug then we can break people's code and add an entry in the changelog.

But I have to check more the details of how this function is used ;)

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 don't know if we should check for KNeighborsMixin or just an estimator that defines a kneighbors_graph function

the implementation explicitly checks for a KNeighborsMixin. I don't even think a BallTree makes sense here. The 2 valid options are an array or a fitted neighbors model. To me the docstring is wrong and needs to be fixed

Comment thread sklearn/neighbors/_graph.py Resolved
@jeremiedbb jeremiedbb added the Validation related to input validation label Jun 26, 2023
@github-actions

github-actions Bot commented Jun 27, 2023 •

Copy link
Copy Markdown

✔️ Linting Passed

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

Generated for commit: b3b3138. Link to the linter CI: here

@glemaitre
glemaitre self-requested a review June 29, 2023 09:07

@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.

Thanks for the PR @StefanieSenger. The param constraints look good. Here are some comments. I think the test test_errors_for_p_values can be removed. I explained why for each param combination below.

Comment thread sklearn/neighbors/_graph.py Outdated
"include_self": ["boolean", StrOptions({"auto"})],
"n_jobs": [Integral, None],
},
prefer_skip_nested_validation=True,

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
prefer_skip_nested_validation=True,
prefer_skip_nested_validation=False, # metric is not validated yet

Comment thread sklearn/neighbors/_graph.py Outdated

@validate_params(
{
"X": ["array-like", BallTree, KNeighborsMixin],

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 don't know if we should check for KNeighborsMixin or just an estimator that defines a kneighbors_graph function

the implementation explicitly checks for a KNeighborsMixin. I don't even think a BallTree makes sense here. The 2 valid options are an array or a fitted neighbors model. To me the docstring is wrong and needs to be fixed

Comment thread sklearn/neighbors/_graph.py Resolved
Comment thread sklearn/neighbors/tests/test_graph.py Resolved
Comment thread sklearn/neighbors/tests/test_graph.py Resolved
Comment thread sklearn/neighbors/tests/test_graph.py Resolved
Comment thread sklearn/neighbors/tests/test_graph.py Resolved
@StefanieSenger

Copy link
Copy Markdown
Member Author

Thank you, @jeremiedbb for reviewing. I have removed the test and also the BallTree from the validation and the documentation. @adrinjalali pointed out, that BallTree wouldn't be supported as an input into the function, as it doesn't have a .kneighbors_graph method.

@glemaitre glemaitre 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 solved the conflicts. LGTM on my side.

Comment thread sklearn/neighbors/_graph.py Resolved
@StefanieSenger

Copy link
Copy Markdown
Member Author

I solved the conflicts. LGTM on my side.

Thank you @glemaitre, I hadn't seen that there were conflicts. I will keep an eye out for those things in the future.

@adrinjalali
adrinjalali requested a review from jeremiedbb August 24, 2023 12:50
@glemaitre
glemaitre self-requested a review August 29, 2023 16:45

@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.

LGTM. Thanks @StefanieSenger

@jeremiedbb
jeremiedbb enabled auto-merge (squash) August 30, 2023 10:08
Comment thread sklearn/neighbors/_graph.py Resolved
@jeremiedbb
jeremiedbb merged commit 59e5070 into scikit-learn:main Aug 30, 2023
glemaitre added a commit to glemaitre/scikit-learn that referenced this pull request Sep 5, 2023
@StefanieSenger
StefanieSenger deleted the kneighbors_graph branch September 7, 2023 07:25
REDVM pushed a commit to REDVM/scikit-learn that referenced this pull request Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants