-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.go
44 lines (33 loc) · 878 Bytes
/
module.go
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
package anomaly
import (
"go.k6.io/k6/js/modules"
)
func init() {
modules.Register("k6/x/anomaly", new(Anomaly))
}
type Anomaly struct{}
func (*Anomaly) Lof(data []float64, threshold float64) []LOFResult {
var anomalies []LOFResult
if threshold == 0.0 {
threshold = 1
}
lofResults := LocalOutlierFactor(data)
median := GetMedianFromLofResults(lofResults)
stdDev := CalculateStandardDeviation(lofResults)
threshold = median - (threshold * stdDev)
for _, result := range lofResults {
if result.LofScore < threshold {
anomalies = append(anomalies, result)
}
}
return anomalies
}
func (*Anomaly) OneClassSvm(trainData []float64, testData []float64, gammaValue float64) []float64 {
if gammaValue == 0.0 {
gammaValue = 50.0
}
svm := NewOneClassSVM(gammaValue, rbfKernel)
svm.Fit(trainData)
anomalies := svm.Predict(testData)
return anomalies
}