-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomForest.py
225 lines (189 loc) · 5.41 KB
/
randomForest.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import pickle
from math import sqrt
from random import randrange
from random import seed
import numpy as np
import sklearn.metrics as metrics
def testSplit(index, val, data):
l, r = list(), list()
for i in data:
if i[index] < val:
l.append(i)
else:
r.append(i)
return l, r
def ginIndex(grps, classes):
noOfTimes = float(sum([len(grp) for grp in grps]))
ginVal = 0.0
for grp in grps:
size = float(len(grp))
if size == 0:
continue
score = 0.0
for i in classes:
poi = [row[-1] for row in grp].count(i) / size
score += poi * poi
ginVal += (1.0 - score) * (size / noOfTimes)
return ginVal
def to_terminal(g):
result = [row[-1] for row in g]
return max(set(result), key = result.count)
def doSplit(data, noOfFeatues):
ind, val, score, grps = 999, 999, 999, None
classVal = list(set(row[-1] for row in data))
features = list()
while len(features) < noOfFeatues:
index = randrange(len(data[0]) - 1)
if index in features:
continue
elif index not in features:
features.append(index)
for index in features:
for row in data:
groups = testSplit(index, row[index], data)
ginVal = ginIndex(groups, classVal)
if ginVal < score:
ind, val, score, grps = index, row[index], ginVal, groups
return {'index': ind, 'value': val, 'groups': grps}
def split(node, maxDepth, minSize, noOfFeatues, depth):
l, r = node['groups']
del(node['groups'])
if not l or not r:
node['left'] = node['right'] = to_terminal(l + r)
return
if depth >= maxDepth:
node['left'], node['right'] = to_terminal(l), to_terminal(r)
return
if len(l) > minSize:
node['left'] = doSplit(l, noOfFeatues)
split(node['left'], maxDepth, minSize, noOfFeatues, depth+1)
else:
node['left'] = to_terminal(l)
if len(r) > minSize:
node['right'] = doSplit(r, noOfFeatues)
split(node['right'], maxDepth, minSize, noOfFeatues, depth+1)
else:
node['right'] = to_terminal(r)
def subsample(data, ratio):
noOfSamp = round(len(data) * ratio)
sample = list()
while len(sample) < noOfSamp:
index = randrange(len(data))
sample.append(data[index])
return sample
def buildTree(train, maxDepth, minSize, noOfFeatues):
root = doSplit(train, noOfFeatues)
split(root, maxDepth, minSize, noOfFeatues, 1)
return root
def crossValSplit(dataset, nFolds):
split = list()
copy = list(dataset)
foldSize = int(len(dataset) / nFolds)
for i in range(nFolds):
fold = list()
while len(fold) < foldSize:
index = randrange(len(copy))
fold.append(copy.pop(index))
split.append(fold)
return split
# def acc_score(real, predVal):
# corr = 0
# for i in range(len(real)):
# if real[i] == predVal[i]:
# corr += 1
# return corr / float(len(real)) * 100.0
def runAlgo(dataset, algo, nFolds, *args):
scores = list()
folds = crossValSplit(dataset, nFolds)
for fold in folds:
testSet = list()
trainSet = list(folds)
trainSet.remove(fold)
trainSet = sum(trainSet, [])
for row in fold:
temp = list(row)
testSet.append(temp)
temp[-1] = None
predVal = algo(trainSet, testSet, *args)
real = [row[-1] for row in fold]
# acc = acc_score(real, predVal)
acc = metrics.accuracy_score(real,predVal)
scores.append(acc)
return scores
def predict(node, row):
if row[node['index']] >= node['value']:
if isinstance(node['right'], dict):
return predict(node['right'], row)
else:
return node['right']
else:
if isinstance(node['left'], dict):
return predict(node['left'], row)
else:
return node['left']
def bagging_pred(trees, row):
predVal = [predict(tree, row) for tree in trees]
return max(set(predVal), key = predVal.count)
def subsample(dataset, ratio):
sample = list()
noOfSamp = round(len(dataset) * ratio)
while len(sample) < noOfSamp:
index = randrange(len(dataset))
sample.append(dataset[index])
return sample
def randomForest(train, test, maxDepth, minSize, sampleSize, n_trees, noOfFeatues):
trees = list()
for i in range(n_trees):
# sample = list()
# n_samp = round(len(dataset) * sampleSize)
# while len(sample) < n_samp:
# index = randrange(len(dataset))
# sample.append(dataset[index])
sample = subsample(train, sampleSize)
tree = buildTree(sample, maxDepth, minSize, noOfFeatues)
trees.append(tree)
predVal = [bagging_pred(trees, row) for row in test]
return predVal
def safeLoad(filename):
return pickle.load(open(filename, 'rb'))
X = np.array(safeLoad('trainvec.pkl'))
Y = safeLoad('labels.pkl')
testX = np.array(safeLoad('testvec.pkl'))
testY = np.array(safeLoad('testlabel.pkl'))
feature = []
some_num = 25
for i in X:
i = np.transpose(np.reshape(i,(i.shape[0]/some_num,some_num)))
cov = np.cov(i)
cov = cov[0:8][:]
cov = cov.flat[:]
arr = [len(cov) + some_num]
arr[0:some_num] = np.mean(i,axis=1)
arr[some_num:some_num+len(cov)] = cov
feature.append(arr)
X = feature
feature = []
for i in testX:
i = np.transpose(np.reshape(i,(i.shape[0]/some_num,some_num)))
cov = np.cov(i)
cov = cov[0:8][:]
cov = cov.flat[:]
arr = [len(cov) + some_num]
arr[0:some_num] = np.mean(i,axis=1)
arr[some_num:some_num+len(cov)] = cov
feature.append(arr)
testX = feature
seed(2)
dataset = X
for i, item in enumerate(dataset):
item.append(Y[i])
maxDepth = 10
minSize = 1
nFolds = 5
sampleSize = 1.0
noOfFeatues = int(sqrt(len(dataset[0])-1))
for i in [50]:
scores = runAlgo(dataset, randomForest, nFolds, maxDepth, minSize, sampleSize, i, noOfFeatues)
print ('no of Trees: %d' % i)
print ('Scores: %s' % scores)
print('Mean Accuracy: %.3f%%' % (sum(scores * 100)/float(len(scores))))