You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LocalOutlierFactor exposes methods that it shouldn't #21876
these appear to be inherited from the parent class, and should not be exposed.
Suggest a potential alternative/fix
Rather than using KNeighborsMixin and NeighborsBase, the implementation should probably just use an instance of a nearest neighbor search internally, and not expose this as part of the local outlier factor API.
As mentioned in the summary, one needs to inherit from NeighborsBase and not KNeigborsMixin. This will remove the two methods. However, we don't want to remove the methods but instead warn that in 2 versions, no one will be able to use them using a deprecation cycle.
Hi @glemaitre, I have added deprecation cycle to kneighbors and kneighbors_graph method which generated the HTML file as below
but when I run "pytest .\sklearn\tests\test_common.py -k kneighbors" it's giving below errors:
@functools.wraps(fun)
def wrapped(*args, **kwargs):
> warnings.warn(msg, category=FutureWarning)
E FutureWarning: Function kneighbors is deprecated; `kneighbors` is deprecated in 1.0 and will be removed in 1.2.
sklearn\utils\deprecation.py:87: FutureWarning
Rather than using KNeighborsMixin and NeighborsBase, the implementation should probably just use an instance of a nearest neighbor search internally, and not expose this as part of the local outlier factor API.
However, is this really a problem to expose those methods? I am not sure this is worth the hassle of refactoring this LocalOutlierFactor with a deprecation cycle and all.
Going through comments in here and in #22312, here are my findings:-
Suggested by @thomasjpfan was to directly using KNeighborsClassifier. This is non-trivial as KNeighborsClassifier expects both X and Y values ( while the fit call for LocalOutlierFactor does not actually use y argument at all)
Hi, I am new here and I want to contribute.
So basically what's left to be done is just to take @Kshitij68 commit, test locally and make a PR out of it - or am I missing something?
@oswinoswin The most acceptable solution is the one suggested in #21876 (comment). I do not think it is a simple task for someone new. I recommend #24862 as a good first issue.
Activity
glemaitre commentedon Dec 4, 2021
pinging @ngoix @agramfort Was there any use case for exposing those methods?
agramfort commentedon Dec 5, 2021
Agreed it shouldn’t be public methods
glemaitre commentedon Dec 5, 2021
OK so we should make a deprecation and go ahead with the proposal of @kno10
purna135 commentedon Jan 17, 2022
Is this issue still open?
If yes may I work on this?
Thanks
glemaitre commentedon Jan 24, 2022
@purnachandramansingh The issue is still open and you can go forward deprecating the methods.
purna135 commentedon Jan 24, 2022
Thanks, @glemaitre, It's my pleasure
purna135 commentedon Jan 27, 2022
Hi @glemaitre, I am a little bit confused about this case, could you please guide me on how to proceed.
Thanks
glemaitre commentedon Jan 27, 2022
As mentioned in the summary, one needs to inherit from
NeighborsBaseand notKNeigborsMixin. This will remove the two methods. However, we don't want to remove the methods but instead warn that in 2 versions, no one will be able to use them using a deprecation cycle.Regarding how to deprecate a method/function/variable in scikit-learn, please refer to the contribution guide: https://scikit-learn.org/dev/developers/contributing.html#deprecation
Feel free to open an early pull request. It is easier to provide feedback on tangible contributions.
purna135 commentedon Jan 27, 2022
Thanks @glemaitre
purna135 commentedon Jan 27, 2022
Hi @glemaitre, I have added deprecation cycle to kneighbors and kneighbors_graph method which generated the HTML file as below

but when I run "
pytest .\sklearn\tests\test_common.py -k kneighbors" it's giving below errors:I am making a PR please suggest
ogrisel commentedon Jan 27, 2022
This is not what @kno10 suggested. He suggested to use composition instead of inheritance:
However, is this really a problem to expose those methods? I am not sure this is worth the hassle of refactoring this
LocalOutlierFactorwith a deprecation cycle and all.kno10 commentedon Jan 27, 2022
@ogrisel it clutters the documentation at
https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html
which lists non-LOF functions such as kneighbors and kneighbors_graph as part of the class.
I don't think its worth deprecating them, because they were never useful in the first place.
Kshitij68 commentedon Sep 11, 2022
Going through comments in here and in #22312, here are my findings:-
KNeighborsClassifier. This is non-trivial asKNeighborsClassifierexpects both X and Y values ( while the fit call forLocalOutlierFactordoes not actually useyargument at all)KNeighborsMixinas parent class is possible, but I did not really like the approach to be honest. Here's what I am talking about -> https://github.com/scikit-learn/scikit-learn/compare/main...Kshitij68:scikit-learn:create-custom-object?expand=1Proposed Solution
Looking more in how sphinx generates documentation, I found that one can declare
:meta private:in docstrings to treat it like private function for sphinx. See here -> https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.htmlHowever, this functionality is currently not compatible with
automsummaryfeature of sphinx that is used. See here -> sphinx-doc/sphinx#8700I think waiting for upstream fixes and then using this feature should be the way to go.
kno10 commentedon Sep 12, 2022
Hiding them in the documentation is a hack, not a solution:
The methods will still show up in tab completion in IDEs, and still pollute the API.
agramfort commentedon Sep 12, 2022
then the solution to me is to introduce a new mixin class that LOF would not inherit from.
oswinoswin commentedon Nov 15, 2022
Hi, I am new here and I want to contribute.
So basically what's left to be done is just to take @Kshitij68 commit, test locally and make a PR out of it - or am I missing something?
thomasjpfan commentedon Nov 30, 2022
@oswinoswin The most acceptable solution is the one suggested in #21876 (comment). I do not think it is a simple task for someone new. I recommend #24862 as a good first issue.