-
Notifications
You must be signed in to change notification settings - Fork 55
/
Isolation_Forest.py
93 lines (69 loc) · 2.25 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from sklearn.ensemble import IsolationForest
class Isolation_Forest:
"""
Isolation Forest or iForest builds an ensemble of iTrees for a given data set, then anomalies are those instances which have short average path lengths on the iTrees.
Parameters
----------
params : list
A list containing three parameters: random_state, n_jobs, and contamination.
Attributes
----------
random_state : int
The random seed used for reproducibility.
n_jobs : int
The number of CPU cores to use for parallelism.
contamination : float
The expected proportion of anomalies in the dataset.
Examples
--------
>>> from Isolation_Forest import Isolation_Forest
>>> PARAMS = [random_state, n_jobs, contamination]
>>> model = Isolation_Forest(PARAMS)
>>> model.fit(X_train)
>>> predictions = model.predict(test_data)
"""
def __init__(self, params):
self.params = params
self.random_state = self.params[0]
self.n_jobs = self.params[1]
self.contamination = self.params[2]
def _Random(self, seed_value):
import os
os.environ["PYTHONHASHSEED"] = str(seed_value)
import random
random.seed(seed_value)
import numpy as np
np.random.seed(seed_value)
import tensorflow as tf
tf.random.set_seed(seed_value)
def _build_model(self):
self._Random(0)
model = IsolationForest(
random_state=self.random_state,
n_jobs=self.n_jobs,
contamination=self.contamination,
)
return model
def fit(self, X):
"""
Train the Isolation Forest model on the provided data.
Parameters
----------
X : numpy.ndarray
Input data for training the model.
"""
self.model = self._build_model()
self.model.fit(X)
def predict(self, data):
"""
Generate predictions using the trained Isolation Forest model.
Parameters
----------
data : numpy.ndarray
Input data for generating predictions.
Returns
-------
numpy.ndarray
Predicted output data.
"""
return self.model.predict(data)