Skip to content

[MRG] Fix LocalOutlierFactor's output for data with duplicated samples #28773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 30, 2024
Merged
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
9 changes: 8 additions & 1 deletion doc/whats_new/v1.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Changelog
- |API| Deprecates `copy_X` in :class:`linear_model.TheilSenRegressor` as the parameter
has no effect. `copy_X` will be removed in 1.8.
:pr:`29105` by :user:`Adam Li <adam2392>`.

:mod:`sklearn.metrics`
......................

Expand All @@ -103,6 +103,13 @@ Changelog
estimator without re-fitting it.
:pr:`29067` by :user:`Guillaume Lemaitre <glemaitre>`.

:mod:`sklearn.neighbors`
........................

- |Fix| :class:`neighbors.LocalOutlierFactor` raises a warning in the `fit` method
when duplicate values in the training data lead to inaccurate outlier detection.
:pr:`28773` by :user:`Henrique Caroço <HenriqueProj>`.

Thanks to everyone who has contributed to the maintenance and improvement of
the project since version 1.5, including:

Expand Down
8 changes: 8 additions & 0 deletions sklearn/neighbors/_lof.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ def fit(self, X, y=None):
self.negative_outlier_factor_, 100.0 * self.contamination
)

# Verify if negative_outlier_factor_ values are within acceptable range.
# Novelty must also be false to detect outliers
if np.min(self.negative_outlier_factor_) < -1e7 and not self.novelty:
warnings.warn(
"Duplicate values are leading to incorrect results. "
"Increase the number of neighbors for more accurate results."
)

return self

def _check_novelty_predict(self):
Expand Down
34 changes: 34 additions & 0 deletions sklearn/neighbors/tests/test_lof.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,37 @@ def test_lof_dtype_equivalence(algorithm, novelty, contamination):
y_pred_32 = getattr(lof_32, method)(X_32)
y_pred_64 = getattr(lof_64, method)(X_64)
assert_allclose(y_pred_32, y_pred_64, atol=0.0002)


def test_lof_duplicate_samples():
"""
Check that LocalOutlierFactor raises a warning when duplicate values
in the training data cause inaccurate results.

Non-regression test for:
https://github.com/scikit-learn/scikit-learn/issues/27839
"""

rng = np.random.default_rng(0)

x = rng.permutation(
np.hstack(
[
[0.1] * 1000, # constant values
np.linspace(0.1, 0.3, num=3000),
rng.random(500) * 100, # the clear outliers
]
)
)
X = x.reshape(-1, 1)

error_msg = (
"Duplicate values are leading to incorrect results. "
"Increase the number of neighbors for more accurate results."
)

lof = neighbors.LocalOutlierFactor(n_neighbors=5, contamination=0.1)

# Catch the warning
with pytest.warns(UserWarning, match=re.escape(error_msg)):
lof.fit_predict(X)
Loading