-
Notifications
You must be signed in to change notification settings - Fork 302
/
isolation_forest.py
78 lines (63 loc) · 2.88 KB
/
isolation_forest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#
# Copyright (c) 2023 salesforce.com, inc.
# All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
#
"""
The classic isolation forest model for anomaly detection.
"""
import logging
import numpy as np
import pandas as pd
from sklearn.ensemble import IsolationForest as skl_IsolationForest
from merlion.models.anomaly.base import DetectorConfig, DetectorBase
from merlion.transform.moving_average import DifferenceTransform
from merlion.transform.sequence import TransformSequence
from merlion.transform.resample import Shingle
from merlion.utils import UnivariateTimeSeries, TimeSeries
logger = logging.getLogger(__name__)
class IsolationForestConfig(DetectorConfig):
"""
Configuration class for `IsolationForest`.
"""
_default_transform = TransformSequence([DifferenceTransform(), Shingle(size=2, stride=1)])
def __init__(self, max_n_samples: int = None, n_estimators: int = 100, n_jobs=-1, **kwargs):
"""
:param max_n_samples: Maximum number of samples to allow the isolation
forest to train on. Specify ``None`` to use all samples in the
training data.
:param n_estimators: number of trees in the isolation forest.
"""
self.max_n_samples = 1.0 if max_n_samples is None else max_n_samples
self.n_estimators = n_estimators
self.n_jobs = n_jobs
# Isolation forest's uncalibrated scores are between 0 and 1
kwargs["max_score"] = 1.0
super().__init__(**kwargs)
class IsolationForest(DetectorBase):
"""
The classic isolation forest algorithm, proposed in
`Liu et al. 2008 <https://ieeexplore.ieee.org/document/4781136>`_
"""
config_class = IsolationForestConfig
def __init__(self, config: IsolationForestConfig):
super().__init__(config)
self.model = skl_IsolationForest(
max_samples=config.max_n_samples, n_estimators=config.n_estimators, random_state=0, n_jobs=config.n_jobs
)
@property
def require_even_sampling(self) -> bool:
return False
@property
def require_univariate(self) -> bool:
return False
def _train(self, train_data: pd.DataFrame, train_config=None) -> pd.DataFrame:
times, train_values = train_data.index, train_data.values
self.model.fit(train_values)
train_scores = -self.model.score_samples(train_values)
return pd.DataFrame(train_scores, index=times, columns=["anom_score"])
def _get_anomaly_score(self, time_series: pd.DataFrame, time_series_prev: pd.DataFrame = None) -> pd.DataFrame:
# Return the negative of model's score, since model scores are in [-1, 0), where more negative = more anomalous
scores = -self.model.score_samples(np.array(time_series.values))
return pd.DataFrame(scores, index=time_series.index)