-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_all(test_datasetB_NP).py
178 lines (163 loc) · 6.12 KB
/
test_all(test_datasetB_NP).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
from sklearn.model_selection import cross_val_score
#Used for storing and loading the trained classifier
from sklearn.externals import joblib
import numpy
from MachineSpecificSettings import Settings
import scipy.io
from DataSetLoaderLib import DataSetLoader
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report
from sklearn import metrics
from sklearn.ensemble import VotingClassifier
sizes=['10','50','100','150','200','250']
methods=['MRMR','JMI','JMIM']
for method in methods:
for size in sizes:
print size
print method
import time
d = DataSetLoader();
X_train= d.LoadDataSet("B_train");
y_train = d.LoadDataSetClasses("B_train");
print X_train.shape
print y_train.shape
X_test= d.LoadDataSet("B_test");
y_test = d.LoadDataSetClasses("B_test");
print X_test.shape
print y_test.shape
#chaipee will fix it later on
y_train=numpy.transpose(y_train)
y_test=numpy.transpose(y_test)
print y_train.shape
targets=list(y_train)
test_targets=list(y_test)
y_train=[]
y_test=[]
for i in targets:
#print i
y_train.append(int(i))
for i in test_targets:
y_test.append(int(i))
#print len(y_train)
indices= joblib.load('datasetB_pickles/datasetB'+size+'-'+method+'.joblib.pkl')
X_train=X_train[:,indices]
X_test=X_test[:,indices]
print "MLP logistic sgd"
clf_mlp = MLPClassifier(activation='logistic',solver='sgd')
print "Training classifier"
start_time=time.time()
clf_mlp.fit(X_train,y_train)
end_time=time.time()-start_time
print end_time
print "Evaluation time"
start_time=time.time()
predictions=clf_mlp.predict(X_test)
end_time=time.time()-start_time
print end_time
print(classification_report(y_test, predictions))
joblib.dump(clf_mlp,'datasetB_results/'+'MLP_logistic_sgd_'+method+'-'+size+'.joblib.pkl')
from sklearn.ensemble import AdaBoostClassifier
print "AdaBoostClassifier"
clf_ada = AdaBoostClassifier()
print "Training classifier"
start_time=time.time()
clf_ada.fit(X_train,y_train)
end_time=time.time()-start_time
print end_time
print "Evaluation time"
start_time=time.time()
predictions=clf_ada.predict(X_test)
end_time=time.time()-start_time
print end_time
print(classification_report(y_test, predictions))
joblib.dump(clf_ada,'datasetB_results/'+'AdaBoostClassifier_'+method+'-'+size+'.joblib.pkl')
from sklearn import tree
print "DT classifier"
clf_tree = tree.DecisionTreeClassifier()
print "Training classifier"
start_time=time.time()
clf_tree.fit(X_train,y_train)
end_time=time.time()-start_time
print end_time
print "Evaluation time"
start_time=time.time()
predictions=clf_tree.predict(X_test)
end_time=time.time()-start_time
print end_time
print(classification_report(y_test, predictions))
joblib.dump(clf_tree,'datasetB_results/'+'DT_classifier_'+method+'-'+size+'.joblib.pkl')
from sklearn.ensemble import ExtraTreesClassifier
print "Extra tree classifier"
clf_extra = ExtraTreesClassifier()
print "Training classifier"
start_time=time.time()
clf_extra.fit(X_train,y_train)
end_time=time.time()-start_time
print end_time
print "Evaluation time"
start_time=time.time()
predictions=clf_extra.predict(X_test)
end_time=time.time()-start_time
print end_time
print(classification_report(y_test, predictions))
joblib.dump(clf_extra,'datasetB_results/'+'Extra_tree_classifier_'+method+'-'+size+'.joblib.pkl')
from sklearn.ensemble import RandomForestClassifier
print "Random Forest"
clf_random = RandomForestClassifier()
print "Training classifier"
start_time=time.time()
clf_random.fit(X_train,y_train)
end_time=time.time()-start_time
print end_time
print "Evaluation time"
start_time=time.time()
predictions=clf_random.predict(X_test)
end_time=time.time()-start_time
print end_time
print(classification_report(y_test, predictions))
joblib.dump(clf_random,'datasetB_results/'+'Random_Forest_'+method+'-'+size+'.joblib.pkl')
from sklearn import svm
print "SVM SVC"
clf_svm = svm.SVC(probability=True)
print "Training classifier"
start_time=time.time()
clf_svm.fit(X_train,y_train)
end_time=time.time()-start_time
print end_time
print "Evaluation time"
start_time=time.time()
predictions=clf_svm.predict(X_test)
end_time=time.time()-start_time
print end_time
print(classification_report(y_test, predictions))
joblib.dump(clf_svm,'datasetB_results/'+'SVM_SVC_'+method+'-'+size+'.joblib.pkl')
#import sendemail as EMAIL
#EMAIL.SendEmail('Classifier trained','Trained for '+method+' on a feature set size of '+size)
print "Ensemble hard classifier"
eclf1 = VotingClassifier(estimators=[('svm', clf_svm), ('rf', clf_random), ('et', clf_extra), ('tree', clf_tree),('mlp', clf_mlp),('ada', clf_ada)], voting='hard')
print "Training classifier"
start_time=time.time()
eclf1 .fit(X_train,y_train)
end_time=time.time()-start_time
print end_time
print "Evaluation time"
start_time=time.time()
predictions=eclf1 .predict(X_test)
end_time=time.time()-start_time
print end_time
print(classification_report(y_test, predictions))
joblib.dump(clf_svm,'datasetB_results/'+'Ensemble_hard_'+method+'-'+size+'.joblib.pkl')
print "Ensemble soft classifier"
eclf1 = VotingClassifier(estimators=[('svm', clf_svm), ('rf', clf_random), ('et', clf_extra), ('tree', clf_tree),('mlp', clf_mlp),('ada', clf_ada)], voting='soft')
print "Training classifier"
start_time=time.time()
eclf1 .fit(X_train,y_train)
end_time=time.time()-start_time
print end_time
print "Evaluation time"
start_time=time.time()
predictions=eclf1 .predict(X_test)
end_time=time.time()-start_time
print end_time
print(classification_report(y_test, predictions))
joblib.dump(clf_svm,'datasetB_results/'+'Ensemble_soft_'+method+'-'+size+'.joblib.pkl')