-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_classifiers.py
executable file
·180 lines (142 loc) · 4.5 KB
/
test_classifiers.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
#!/usr/bin/python
import sys, os, time
from sklearn import decomposition
import numpy as np
from sklearn import datasets
from skimage.feature import hog
from sklearn.utils import shuffle
from sklearn import metrics
import sys
from numpy import genfromtxt
import pickle
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
import matplotlib.pyplot as plt
extnsn = "_grey_ftr.csv"
# extnsn = "_clr_ftr.csv"
Rsol = 30
f = open('HOG'+str(Rsol)+extnsn+'.pickle')
X_train_hog, y_train, X_test_hog, y_test = pickle.load(f)
f.close()
f = open('plain'+str(Rsol)+extnsn+'.pickle')
X_train, y_train, X_test, y_test = pickle.load(f)
f.close()
def dt_results():
print "--------------DecisionTreeClassifier-----------------"
rang = [None, 10, 20, 50, 100, 200, 400]
print "--------------With HOG-----------------"
ans = []
print "maxDepth Accuracy"
for i in rang:
clf = DecisionTreeClassifier(max_depth=i)
clf.fit(X_train_hog, y_train)
mean_accuracy = clf.score(X_test_hog, y_test)
print i, " ", mean_accuracy
ans.append('('+str(i)+", "+str(mean_accuracy)+')')
print ans
print "\n--------------Without HOG-----------------"
ans = []
print "maxDepth Accuracy"
for i in rang:
clf = DecisionTreeClassifier(max_depth=i)
clf.fit(X_train, y_train)
mean_accuracy = clf.score(X_test, y_test)
print i, " ", mean_accuracy
ans.append('('+str(i)+", "+str(mean_accuracy)+')')
print ans
def RF_results():
print "--------------RandomForestClassifier-----------------"
rang = [None, 10, 20, 50, 100, 200, 400]
print "--------------With HOG-----------------"
ans = []
print "n_estimators Accuracy"
for i in rang:
clf = RandomForestClassifier(n_estimators=i)
clf.fit(X_train_hog, y_train)
mean_accuracy = clf.score(X_test_hog, y_test)
print i, " ", mean_accuracy
ans.append('('+str(i)+", "+str(mean_accuracy)+')')
print ans
print "\n--------------Without HOG-----------------"
ans = []
print "n_estimators Accuracy"
for i in rang:
clf = RandomForestClassifier(n_estimators=i)
clf.fit(X_train, y_train)
mean_accuracy = clf.score(X_test, y_test)
print i, " ", mean_accuracy
ans.append('('+str(i)+", "+str(mean_accuracy)+')')
print ans
def AB_results(): # AdaBoostClassifier
print "--------------AdaBoostClassifier-----------------"
rang = [60, 80]
# print "--------------With HOG-----------------"
# ans = []
# print "n_estimators Accuracy"
# for i in rang:
# clf = AdaBoostClassifier(n_estimators=i)
# clf.fit(X_train_hog, y_train)
# mean_accuracy = clf.score(X_test_hog, y_test)
# print i, " ", mean_accuracy
# ans.append('('+str(i)+", "+str(mean_accuracy)+')')
# print ans
# plt.plot(rang, ans, linewidth=2.0)
# plt.xlabel("n_estimators")
# plt.ylabel("mean_accuracy")
# plt.savefig("temp_hog.png")
print "\n--------------Without HOG-----------------"
ans = []
print "n_estimators Accuracy"
for i in rang:
clf = AdaBoostClassifier(n_estimators=i)
clf.fit(X_train, y_train)
mean_accuracy = clf.score(X_test, y_test)
print i, " ", mean_accuracy
ans.append('('+str(i)+", "+str(mean_accuracy)+')')
print ans
plt.plot(rang, ans, linewidth=2.0)
plt.xlabel("n_estimators")
plt.ylabel("mean_accuracy")
plt.savefig("temp_plain.png")
def SVC_results():
print "--------------LinearSVC-----------------"
rang = [1]
print "--------------With HOG-----------------"
ans = []
print "n_estimators Accuracy"
for i in rang:
clf = LinearSVC()
clf.fit(X_train_hog, y_train)
y_pred = clf.predict(X_test_hog)
print "confusion matrix: \n", metrics.confusion_matrix(y_test, y_pred), "\n"
mean_accuracy = clf.score(X_test_hog, y_test)
print i, " ", mean_accuracy
ans.append('('+str(i)+", "+str(mean_accuracy)+')')
print ans
# plt.plot(rang, ans, linewidth=2.0)
# plt.xlabel("n_estimators")
# plt.ylabel("mean_accuracy")
# plt.savefig("temp_hog.png")
# print "\n--------------Without HOG-----------------"
# ans = []
# print "n_estimators Accuracy"
# for i in rang:
# clf = LinearSVC()
# clf.fit(X_train, y_train)
# mean_accuracy = clf.score(X_test, y_test)
# print i, " ", mean_accuracy
# ans.append('('+str(i)+", "+str(mean_accuracy)+')')
# print ans
# plt.plot(rang, ans, linewidth=2.0)
# plt.xlabel("n_estimators")
# plt.ylabel("mean_accuracy")
# plt.savefig("temp_plain.png")
# dt_results()
# RF_results()
# AB_results()
SVC_results()