-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtrcf_model.py
More file actions
58 lines (51 loc) · 2.07 KB
/
Copy pathtrcf_model.py
File metadata and controls
58 lines (51 loc) · 2.07 KB
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
# Java imports
from typing import List, Optional, Tuple, Any
import numpy as np
import logging
from com.amazon.randomcutforest.parkservices import ThresholdedRandomCutForest
from com.amazon.randomcutforest.config import Precision
from com.amazon.randomcutforest.parkservices import AnomalyDescriptor
from com.amazon.randomcutforest.config import TransformMethod
import jpype
class TRandomCutForestModel:
"""
Random Cut Forest Python Binding around the AWS Random Cut Forest Official Java version:
https://github.com/aws/random-cut-forest-by-aws
"""
def __init__(self, rcf_dimensions, shingle_size, num_trees: int = 30, output_after: int=256, anomaly_rate=0.005,
z_factor=2.5, score_differencing=0.5, ignore_delta_threshold=0, sample_size=256):
self.forest = (ThresholdedRandomCutForest
.builder()
.dimensions(rcf_dimensions)
.sampleSize(sample_size)
.numberOfTrees(num_trees)
.timeDecay(0.0001)
.initialAcceptFraction(output_after*1.0/sample_size)
.parallelExecutionEnabled(True)
.compact(True)
.precision(Precision.FLOAT_32)
.boundingBoxCacheFraction(1)
.shingleSize(shingle_size)
.anomalyRate(anomaly_rate)
.outputAfter(output_after)
.internalShinglingEnabled(True)
.transformMethod(TransformMethod.NORMALIZE)
.alertOnce(True)
.autoAdjust(True)
.build())
self.forest.setZfactor(z_factor)
def process(self, point: List[float]) -> AnomalyDescriptor:
"""
a single call that prepreprocesses data, compute score/grade and updates
state.
Parameters
----------
point: List[float]
A data point with shingle size
Returns
-------
AnomalyDescriptor
Encapsulate detailed information about anomalies detected by RCF model. This class stores various attributes
related to an anomaly, such as confidence levels, attribution scores, and expected values.
"""
return self.forest.process(point, 0)