-
Notifications
You must be signed in to change notification settings - Fork 9
/
IsolationForest.scala
57 lines (48 loc) · 1.96 KB
/
IsolationForest.scala
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
// Wei Chen - Isolation Forest
// 2022-03-04
package com.scalaml.algorithm
import com.scalaml.general.MatrixFunc._
class IsolationForest() extends Abnormal {
val algoname: String = "IsolationForest"
val version: String = "0.1"
var trees = Array[IsolationTree]()
var tree_n = 10 // Number of Trees
var sample_n = 10 // Number of Sample Data in a Tree
var maxLayer = 5
override def clear(): Boolean = {
trees = Array[IsolationTree]()
tree_n = 10 // Number of Trees
sample_n = 10 // Number of Sample Data in a Tree
maxLayer = 5
true
}
override def config(paras: Map[String, Any]): Boolean = try {
tree_n = paras.getOrElse("TREE_NUMBER", paras.getOrElse("tree_number", paras.getOrElse("tree_n", 10.0))).asInstanceOf[Double].toInt
sample_n = paras.getOrElse("SAMPLE_NUMBER", paras.getOrElse("sample_number", paras.getOrElse("sample_n", 10.0))).asInstanceOf[Double].toInt
maxLayer = paras.getOrElse("maxLayer", 5.0).asInstanceOf[Double].toInt
true
} catch { case e: Exception =>
Console.err.println(e)
false
}
private def randomSelect(data: Array[Array[Double]], sample_n: Int) =
scala.util.Random.shuffle(data.toList).take(sample_n).toArray
private def addTree(data: Array[Array[Double]]): Boolean = {
val itree = new IsolationTree()
var paras = Map("maxLayer" -> maxLayer.toDouble): Map[String, Any]
val check = itree.config(paras) && itree.train(data)
if(check) trees :+= itree
check
}
override def train(data: Array[Array[Double]]): Boolean = {
val data_n = data.size
if (data_n > sample_n) {
(0 until tree_n).forall(i => addTree(randomSelect(data, sample_n)))
} else addTree(data)
}
override def predict(data: Array[Array[Double]]): Array[Double] = {
matrixaccumulate(trees.map { tree =>
tree.predict(data)
}).map(_ / tree_n)
}
}