Skip to content

LocalOutlierFactor exposes methods that it shouldn't #21876

Open

Description

@kno10

Describe the issue linked to the documentation

The LocalOutlierFactor class (and hence documentation) exposes methods that it probably shouldn't:

https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor.kneighbors
https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor.kneighbors_graph

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.

Activity

  1. glemaitre commented on Dec 4, 2021

    @glemaitre
    Member

    pinging @ngoix @agramfort Was there any use case for exposing those methods?

  2. agramfort commented on Dec 5, 2021

    @agramfort
    Member

    Agreed it shouldn’t be public methods

  3. glemaitre commented on Dec 5, 2021

    @glemaitre
    Member

    OK so we should make a deprecation and go ahead with the proposal of @kno10

  4. purna135 commented on Jan 17, 2022

    @purna135
    Contributor

    Is this issue still open?
    If yes may I work on this?
    Thanks

  5. glemaitre commented on Jan 24, 2022

    @glemaitre
    Member

    @purnachandramansingh The issue is still open and you can go forward deprecating the methods.

  6. purna135 commented on Jan 24, 2022

    @purna135
    Contributor

    Thanks, @glemaitre, It's my pleasure

  7. purna135 commented on Jan 27, 2022

    @purna135
    Contributor

    Hi @glemaitre, I am a little bit confused about this case, could you please guide me on how to proceed.
    Thanks

  8. glemaitre commented on Jan 27, 2022

    @glemaitre
    Member

    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.

    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.

  9. purna135 commented on Jan 27, 2022

    @purna135
    Contributor

    Thanks @glemaitre

  10. purna135 commented on Jan 27, 2022

    @purna135
    Contributor

    Hi @glemaitre, I have added deprecation cycle to kneighbors and kneighbors_graph method which generated the HTML file as below
    image

    image

    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
    

    I am making a PR please suggest

  11. ogrisel commented on Jan 27, 2022

    @ogrisel
    Member

    As mentioned in the summary, one needs to inherit from NeighborsBase and not KNeigborsMixin.

    This is not what @kno10 suggested. He suggested to use composition instead of inheritance:

    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.

  12. kno10 commented on Jan 27, 2022

    @kno10
    ContributorAuthor

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

  13. Kshitij68 commented on Sep 11, 2022

    @Kshitij68
    Contributor

    Going through comments in here and in #22312, here are my findings:-

    Proposed 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.html

    However, this functionality is currently not compatible with automsummary feature of sphinx that is used. See here -> sphinx-doc/sphinx#8700

    I think waiting for upstream fixes and then using this feature should be the way to go.

  14. kno10 commented on Sep 12, 2022

    @kno10
    ContributorAuthor

    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.

  15. agramfort commented on Sep 12, 2022

    @agramfort
    Member

    then the solution to me is to introduce a new mixin class that LOF would not inherit from.

  16. added
    Easy
    good first issue
    on Nov 4, 2022
  17. oswinoswin commented on Nov 15, 2022

    @oswinoswin

    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?

  18. thomasjpfan commented on Nov 30, 2022

    @thomasjpfan
    Member

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

  19. added
    Moderate
    and removed
    Easy
    good first issue
    on Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    APIModeratemodule:neighbors

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions