-
Notifications
You must be signed in to change notification settings - Fork 0
/
iso_forest.py
184 lines (155 loc) · 5.2 KB
/
iso_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"isolated forest functions"
__author__ = 'Matias Carrasco Kind'
import numpy as np
import random as rn
import os
import warnings
from version import __version__
try:
import igraph as ig
except:
warnings.warn("No igraph interface for plotting trees")
def c_factor(n) :
return 2.0*(np.log(n-1)+0.5772156649) - (2.0*(n-1.)/(n*1.0))
class iForest(object):
def __init__(self,X, ntrees, sample_size, limit=None):
self.ntrees = ntrees
self.X = X
self.nobjs = len(X)
self.sample = sample_size
self.Trees = []
self.limit = limit
if limit is None:
self.limit = int(np.ceil(np.log2(self.sample)))
self.c = c_factor(self.sample)
for i in range(self.ntrees):
ix = rn.sample(range(self.nobjs), self.sample)
X_p = X[ix]
self.Trees.append(iTree(X_p, 0, self.limit))
def compute_paths(self, X_in = None):
if X_in is None:
X_in = self.X
S = np.zeros(len(X_in))
for i in range(len(X_in)):
h_temp = 0
for j in range(self.ntrees):
h_temp += PathFactor(X_in[i],self.Trees[j]).path*1.0
Eh = h_temp/self.ntrees
S[i] = 2.0**(-Eh/self.c)
return S
def compute_paths_single(self, x):
S = np.zeros(self.ntrees)
for j in range(self.ntrees):
path = PathFactor(x,self.Trees[j]).path*1.0
S[j] = 2.0**(-1.0*path/self.c)
return S
class Node(object):
def __init__(self, X, q, p, e, left, right, node_type = '' ):
self.e = e
self.size = len(X)
self.X = X # to be removed
self.q = q
self.p = p
self.left = left
self.right = right
self.ntype = node_type
class iTree(object):
"""
Unique entries for X
"""
def __init__(self,X,e,l):
self.e = e # depth
self.X = X #save data for now
self.size = len(X) # n objects
self.Q = np.arange(np.shape(X)[1], dtype='int') # n dimensions
self.l = l # depth limit
self.p = None
self.q = None
self.exnodes = 0
self.root = self.make_tree(X,e,l)
def make_tree(self,X,e,l):
self.e = e
if e >= l or len(X) <= 1:
left = None
right = None
self.exnodes += 1
return Node(X, self.q, self.p, e, left, right, node_type = 'exNode' )
else:
self.q = rn.choice(self.Q)
mini = X[:,self.q].min()
maxi = X[:,self.q].max()
if mini==maxi:
left = None
right = None
self.exnodes += 1
return Node(X, self.q, self.p, e, left, right, node_type = 'exNode' )
self.p = rn.uniform(mini,maxi)
w = np.where(X[:,self.q] < self.p,True,False)
return Node(X, self.q, self.p, e,\
left=self.make_tree(X[w],e+1,l),\
right=self.make_tree(X[~w],e+1,l),\
node_type = 'inNode' )
def get_node(self, path):
node = self.root
for p in path:
if p == 'L' : node = node.left
if p == 'R' : node = node.right
return node
class PathFactor(object):
def __init__(self,x,itree):
self.path_list=[]
self.x = x
self.e = 0
self.path = self.find_path(itree.root)
def find_path(self,T):
if T.ntype == 'exNode':
if T.size == 1: return self.e
else:
self.e = self.e + c_factor(T.size)
return self.e
else:
a = T.q
self.e += 1
if self.x[a] < T.p:
self.path_list.append('L')
return self.find_path(T.left)
else:
self.path_list.append('R')
return self.find_path(T.right)
def all_branches(node, current=[], branches = None):
current = current[:node.e]
if branches is None: branches = []
if node.ntype == 'inNode':
current.append('L')
all_branches(node.left, current=current, branches=branches)
current = current[:-1]
current.append('R')
all_branches(node.right, current=current, branches=branches)
else:
branches.append(current)
return branches
def branch2num(branch, init_root=0):
num = [init_root]
for b in branch:
if b == 'L':
num.append(num[-1] * 2 + 1)
if b == 'R':
num.append(num[-1] * 2 + 2)
return num
def gen_graph(branches, g = None, init_root = 0, pre = ''):
num_branches = [branch2num(i, init_root) for i in branches]
all_nodes = [j for branch in num_branches for j in branch]
all_nodes = np.unique(all_nodes)
all_nodes = all_nodes.tolist()
if g is None:
g=ig.Graph()
for k in all_nodes : g.add_vertex(pre+str(k))
t=[]
for j in range(len(branches)):
branch = branch2num(branches[j], init_root)
for i in range(len(branch)-1):
pair = [branch[i],branch[i+1]]
if pair not in t:
t.append(pair)
g.add_edge(pre+str(branch[i]),pre+str(branch[i+1]))
return g,max(all_nodes)