Skip to content

Commit

Permalink
Add learn_many to learn multiple instances at a time and update Docst…
Browse files Browse the repository at this point in the history
…ring test to take into account the newly implemented function.
  • Loading branch information
hoanganhngo610 committed Sep 7, 2023
1 parent a5afc23 commit 456cb6e
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions river/anomaly/ilof.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import functools

import pandas as pd

from river import anomaly, utils
from river.neighbors.base import DistanceFunc
from river.utils import VectorDict
Expand Down Expand Up @@ -79,12 +81,14 @@ class LocalOutlierFactor(anomaly.base.AnomalyDetector):
>>> for x, _ in datasets.CreditCard().take(200):
... incremental_lof.learn_one(x)
>>> incremental_lof.learn_many(cc_df[201:401])
>>> ilof_scores = []
>>> for x in cc_df[0][201:206]:
>>> for x in cc_df[0][401:406]:
... ilof_scores.append(incremental_lof.score_one(x))
>>> [round(ilof_score, 3) for ilof_score in ilof_scores]
[1.149, 1.098, 1.158, 1.101, 1.092]
[1.802, 1.937, 1.567, 1.181, 1.28]
References
----------
Expand Down Expand Up @@ -117,6 +121,19 @@ def __init__(
else functools.partial(utils.math.minkowski_distance, p=2)
)

def learn_many(self, x: pd.DataFrame):
"""
Update the model with multiple incoming observations simultaneously.
This function assumes that the observations are stored in the first column of the dataset.
Parameters
----------
x
A Pandas DataFrame including multiple instances to be learned at the same time
"""
x = x[0].tolist()
self.learn(x)

def learn_one(self, x: dict):
"""
Update the model with one incoming observation
Expand Down

0 comments on commit 456cb6e

Please sign in to comment.