-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomForest.py
119 lines (96 loc) · 4.23 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
"""
@author: Mtajic
"""
import numpy as np
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.externals.six import StringIO
from sklearn.model_selection import cross_validate
from skimage import io
from skimage.filters import gaussian
from skimage.transform import resize
from IPython.display import Image
from sklearn.ensemble import RandomForestClassifier
class DataSet:
def __init__(self, inputs=None, targets=None):
self.inputs = [] if inputs is None else inputs
self.targets = [] if targets is None else targets
if __name__ == '__main__':
dataset = DataSet()
print('Loading Training Data...')
labels = ['01_palm', '02_l', '03_fist', '04_fist_moved', '05_thumb',
'06_index', '07_ok', '08_palm_moved', '09_c', '10_down', ]
# 75% to train
for i in range(10):
for label in labels:
for j in range(1, 151): # 201):
label.split()
image = io.imread('data/leapGestRecog/0' +
str(i) + '/' +
label + '/frame_0' +
str(i) + '_' +
label[0:2] + '_' +
str(j).zfill(4) + '.png')
img_blurred = gaussian(image, sigma=1.65)
dataset.inputs.append(resize(img_blurred, (60, 160), preserve_range=True).flatten())
dataset.targets.append(label)
# shuffle data
x_train, y_train = shuffle(dataset.inputs, dataset.targets)
# Model Selection
print('Model Selection...')
testScores = {}
myMaxScore = 0
bestFunctionPrameters = ""
clf_cv5 = ""
for i in [6, 7, 8, 9, 10]:
for j in ["gini", "entropy"]:
clf_rft = RandomForestClassifier(criterion=j, n_estimators=100, max_features=240, max_depth=i,
random_state=0) # 15
# perform 5-fold cross validation for each model
scores = cross_validate(clf_rft, x_train, y_train, scoring='precision_macro', cv=5, return_estimator=True)
testScores[j + "_" + str(i)] = scores["test_score"]
myScore = np.max(scores["test_score"], axis=0)
print(j + "_" + str(i), " Score: ", str(myScore))
if myScore > myMaxScore:
clf_rft_cv5 = scores["estimator"][np.argmax(scores["test_score"], axis=0)]
myMaxScore = myScore
bestFunctionPrameters = j + "_" + str(i)
# Training
print('Training...')
bst_rft = clf_rft_cv5
labels = ['01_palm', '02_l', '03_fist', '04_fist_moved', '05_thumb',
'06_index', '07_ok', '08_palm_moved', '09_c', '10_down', ]
print('Loading Testing Data...')
# 25% to test
test_data = DataSet()
for i in range(10):
for label in labels:
for j in range(151, 201): # 1, 201):
label.split()
image = io.imread('data/leapGestRecog/0' +
str(i) + '/' +
label + '/frame_0' +
str(i) + '_' +
label[0:2] + '_' +
str(j).zfill(4) + '.png')
img_blurred = gaussian(image, sigma=1.65)
test_data.inputs.append(resize(img_blurred, (60, 160), preserve_range=True).flatten())
test_data.targets.append(label)
print('Testing...')
predicted = bst_rft.predict(test_data.inputs)
print("\nClassification Report")
print(classification_report(test_data.targets, predicted))
print("\nConfusion Matrix")
print(confusion_matrix(test_data.targets, predicted))
y_train_score_rft = bst_rft.predict(x_train)
# creating the box plot for model-selection
s = testScores.values()
labels = testScores.keys()
fig = plt.figure(figsize=(8, 6))
plt.boxplot([x for x in s], 0, '', 1)
plt.xticks([y + 1 for y in range(len([x for x in s]))], labels, rotation=70)
plt.xlabel('Models(metric and max depth)')
plt.ylabel('Accuracy')
t = plt.title('Box plot of HyperParameters with 5-fold cross validation')
plt.show()