-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation.py
More file actions
172 lines (125 loc) · 4.77 KB
/
implementation.py
File metadata and controls
172 lines (125 loc) · 4.77 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
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# prepation******************************************************************
def select_feature(data):
return np.random.choice(data.columns)
def select_value(data,feat):
mini = data[feat].min()
maxi = data[feat].max()
return (maxi-mini)*np.random.random()+mini
def split_data(data, split_column, split_value):
data_below = data[data[split_column] < split_value]
data_above = data[data[split_column] >= split_value]
return data_below, data_above
def classify_data(data):
data = np.array(data)
label_column = data[:, -1]
unique_classes, counts_unique_classes = np.unique(label_column, return_counts=True)
# 找到每一个数值中最大的,其实也可以用unique_classes[0]
index = counts_unique_classes.argmax()
classification = unique_classes[index]
return classification
# key functions**************************************************************
def isolation_tree(data, counter=0, max_depth=50):
# End Loop if max depth or isolated
if (counter >= max_depth) or data.shape[0] <= 1:
classification = classify_data(data)
return classification
else:
# Select feature
split_column = select_feature(data)
# Select value
split_value = select_value(data, split_column)
# Split data
data_left, data_right = split_data(data, split_column, split_value)
# instantiate sub-tree
question = "{} <= {}".format(split_column, split_value)
sub_tree = {question: []}
# Recursive part
left_answer = isolation_tree(data_left , counter+1, max_depth=max_depth)
right_answer = isolation_tree(data_right, counter+1, max_depth=max_depth)
if left_answer == right_answer:
sub_tree = left_answer
else:
# quesiton相当于这一层的root,分别放left和right
sub_tree[question].append(left_answer)
sub_tree[question].append(right_answer)
return sub_tree
def isolation_forest(df, n_trees=5, max_depth=5, subspace=256):
forest = []
max_depth = np.ceil(np.log2(subspace))
for i in range(n_trees):
# Sample the subspace
if subspace <= 1:
df = df.sample(frac = subspace)
else:
df = df.sample(n = subspace)
tree = isolation_tree(df, 0, max_depth)
forest.append(tree)
return forest
def pathLength(example, iTree, path=0):
# Initialize question
question = list(iTree.keys())[0]
feature_name, _, value = question.split()
# ask question
if example[feature_name].values <= float(value):
answer = iTree[question][0]
else:
answer = iTree[question][1]
# base case,叶结点了
if not isinstance(answer, dict):
return path+1
#T_size = len(list(iTree.keys())[0])
#return np.e + 2*(np.log(T_size-1)+0.5772156649) -2*(T_size-1)/T_size
# recursive part
else:
residual_tree = answer
return pathLength(example, residual_tree, path+1)
# Evaluate one instance
def evaluate_instance(instance,forest):
paths = []
for tree in forest:
paths.append(pathLength(instance,tree))
return paths
def predict(dataset,forest,ratio = 0.1):
normal = []
abnormal = []
pred = []
columns = dataset.columns.values
dataset = np.array(dataset)
for i in dataset:
instance = pd.DataFrame(i.reshape(-1, 2), columns=columns)
length = evaluate_instance(instance, iForest)
pred.append(np.mean(length))
sorted = np.sort(pred)
split = sorted[int(len(sorted) * ratio)]
for i in range(len(pred)):
if pred[i] < split:
abnormal.append(dataset[i])
else:
normal.append(dataset[i])
return normal,abnormal
mean = [0, 0]
cov = [[1, 0], [0, 1]]
Nobjs = 2000
x, y = np.random.multivariate_normal(mean, cov, Nobjs).T
#Add manual outlier
x[0]=3.3; y[0]=3.3
dataset = np.array([x, y]).T
dataset = pd.DataFrame(dataset, columns=['feat1', 'feat2'])
iForest = isolation_forest(dataset, n_trees=30, max_depth=100, subspace=256)
normal,abnormal = predict(dataset,iForest,0.15)
# visualization
plt.figure(figsize=(7,7))
axes = plt.subplot(111)
axes.scatter(x,y, s=10, color='blue')
plt.show()
axes = plt.subplot(111)
normal = np.array(normal)
abnormal = np.array(abnormal)
type1 = axes.scatter(normal[ : , 0], normal[ : , 1], s=10, color='blue')
type2 = axes.scatter(abnormal[ : , 0], abnormal[ : , 1], s=12, color='red')
axes.legend((type1, type2), ("norm", "anomaly") , prop={'size':12})
plt.title("Isolution Forest")
plt.show()