diff --git a/river/anomaly/lof.py b/river/anomaly/lof.py index 12b4eea44b..ecb54c5f00 100644 --- a/river/anomaly/lof.py +++ b/river/anomaly/lof.py @@ -221,7 +221,25 @@ class LocalOutlierFactor(anomaly.base.AnomalyDetector): ... scores.append(lof.score_one(x)) >>> [round(score, 3) for score in scores] - [1.802, 1.937, 1.567, 1.181, 1.28] + [1.802, 1.936, 1.566, 1.181, 1.272] + + >>> X = [0.5, 0.45, 0.43, 0.44, 0.445, 0.45, 0.0] + >>> lof = anomaly.LocalOutlierFactor() + + >>> for x in X[:3]: + ... lof.learn_one({'x': x}) # Warming up + + >>> for x in X: + ... features = {'x': x} + ... print(f'Anomaly score for x={x:.3f}: {lof.score_one(features):.3f}') + ... lof.learn_one(features) + Anomaly score for x=0.500: 0.000 + Anomaly score for x=0.450: 0.000 + Anomaly score for x=0.430: 0.000 + Anomaly score for x=0.440: 1.020 + Anomaly score for x=0.445: 1.032 + Anomaly score for x=0.450: 0.000 + Anomaly score for x=0.000: 0.980 References ---------- diff --git a/river/anomaly/test_lof.py b/river/anomaly/test_lof.py index 9703b8cdc9..c4933b0091 100644 --- a/river/anomaly/test_lof.py +++ b/river/anomaly/test_lof.py @@ -80,3 +80,33 @@ def test_issue_1328(): X = [{"a": 1, "b": 1}, {"a": 1, "b": 1}] for x in X: lof.learn_one(x) + + +def test_issue_1331(): + import copy + + from river import anomaly + + lof = anomaly.LocalOutlierFactor() + + X = [{"a": 1, "b": 1}, {"a": 1, "b": 1}] + for x in X: + lof.learn_one(x) + + neighborhoods_ = lof.neighborhoods.copy() + rev_neighborhoods = lof.rev_neighborhoods.copy() + k_dist_ = lof.k_dist.copy() + reach_dist_ = copy.deepcopy(lof.reach_dist) + dist_dict_ = copy.deepcopy(lof.dist_dict) + local_reach_ = lof.local_reach.copy() + lof_ = lof.lof.copy() + + lof.score_one({"a": 0.5, "b": 1}) + + assert neighborhoods_ == lof.neighborhoods + assert rev_neighborhoods == lof.rev_neighborhoods + assert k_dist_ == lof.k_dist + assert reach_dist_ == lof.reach_dist + assert dist_dict_ == lof.dist_dict + assert local_reach_ == lof.local_reach + assert lof_ == lof.lof