Skip to content

[WIP] Fix LocalOutlierFactor exposes methods that it shouldn't - #22312

Closed
purna135 wants to merge 5 commits into
scikit-learn:mainfrom
purna135:doc_LocalOutlierFactor
Closed

purna135 wants to merge 5 commits into
scikit-learn:mainfrom
purna135:doc_LocalOutlierFactor

Conversation

@purna135

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Addresses #21876

What does this implement/fix? Explain your changes.

Added added deprecation cycle to kneighbors and kneighbors_graph method

Any other comments?

waiting for your valuable suggestions

@ogrisel

ogrisel commented Jan 27, 2022 •

Copy link
Copy Markdown
Member

We should not change the KNeighborsMixin base class itself. It's used by many other sub-classes that legitimately need to expose those methods.

@ogrisel

ogrisel commented Jan 27, 2022

Copy link
Copy Markdown
Member

I commented in the original issue: #21876 (comment)

@purna135

Copy link
Copy Markdown
Contributor Author

Thank you @ogrisel

@purna135

Copy link
Copy Markdown
Contributor Author

Hi @ogrisel could you please suggest what should be my approach to solve this

@purna135

Copy link
Copy Markdown
Contributor Author

Hi @ogrisel, I tried to use composition but since there is no fit method inside KNeighborsMixin I am getting the error

sklearn\utils\estimator_checks.py:3802: in check_dataframe_column_names_consistency
    estimator.fit(X, y)
sklearn\neighbors\_lof.py:293: in fit
    self._distances_fit_X_, _neighbors_indices_fit_X_ = self.kn_mixin_obj.kneighbors(
sklearn\neighbors\_base.py:728: in kneighbors
    check_is_fitted(self)


TypeError: <sklearn.neighbors._base.KNeighborsMixin object at 0x00000272F5A00790> is not an estimator instance.

Please guide how to solve this

@thomasjpfan

thomasjpfan commented Jan 30, 2022 •

Copy link
Copy Markdown
Member

I tried to use composition but since there is no fit method inside KNeighborsMixin

KNeighborsMixin is a mixin not a full estimator so it can not be directly initialized.

At a high level the refactor involves:

  1. Refactor LocalOutlierFactor.fit to create a KNeighborsClassifier, fit it and store it as a private attribute in LocalOutlierFactor.
  2. Throughout LocalOutlierFactor use the private attribute to call kneighbors.
  3. Deprecate LocalOutlierFactor.kneighbors

I do not think this is an easy task as it involves some understanding of how to develop scikit-learn estimators and object-oriented programming.

We have not decided if composition is worth doing. Note that even if the refactor is complete, it may not be merged if we think it is harder to maintain.

@purna135

Copy link
Copy Markdown
Contributor Author

Thanks for the clarification @thomasjpfan, since it involves lots of activities I don't think it's suitable for me. So I am closing this pull request. I will try some other simple issues.

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.

3 participants