Skip to content

Commit

Permalink
solve identical samples issue in lof
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxHalford committed Oct 3, 2023
1 parent 7956876 commit 4b949c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 4 additions & 6 deletions river/anomaly/lof.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ def calc_local_reach_dist(
Calculate local reachability distance of affected points.
"""
for i in set_index:
local_reach_dist[i] = len(neighborhoods[i]) / sum(
[reach_dist[i][j] for j in neighborhoods[i]]
)
denominator = sum(reach_dist[i][j] for j in neighborhoods[i])
local_reach_dist[i] = len(neighborhoods[i]) / denominator if denominator else 0
return local_reach_dist


Expand All @@ -136,9 +135,8 @@ def calc_lof(set_index: set, neighborhoods: dict, local_reach: dict, lof: dict):
Calculate local outlier factor (LOF) of affected points.
"""
for i in set_index:
lof[i] = sum([local_reach[j] for j in neighborhoods[i]]) / (
len(neighborhoods[i]) * local_reach[i]
)
denominator = len(neighborhoods[i]) * local_reach[i]
lof[i] = sum(local_reach[j] for j in neighborhoods[i]) / denominator if denominator else 0
return lof


Expand Down
7 changes: 7 additions & 0 deletions river/anomaly/test_ilof.py → river/anomaly/test_lof.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ def test_batch_lof_scores():
assert np.allclose(
ilof_scores_river_batch, lof_scores_sklearn_batch, rtol=1e-02, atol=1e-02
)


def test_issue_1328():
lof = anomaly.LocalOutlierFactor()
X = [{"a": 1, "b": 1}, {"a": 1, "b": 1}]
for x in X:
lof.learn_one(x)

0 comments on commit 4b949c6

Please sign in to comment.