-
Notifications
You must be signed in to change notification settings - Fork 1
/
Classification.py
executable file
·297 lines (263 loc) · 9.44 KB
/
Classification.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from sklearn.linear_model import SGDClassifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import pandas as pd
from sklearn.decomposition import TruncatedSVD
from sklearn import preprocessing
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV
from sklearn import metrics
import numpy as np
from sklearn.naive_bayes import MultinomialNB
from sklearn.naive_bayes import BernoulliNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
#k-fold cross validation
from sklearn.cross_validation import KFold
#accuracy
from sklearn.metrics import accuracy_score
#roc
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
from scipy import interp
# my method
from sklearn.ensemble import VotingClassifier
def compute_and_print(num,pipeline,X_train,folds):
stats=[]
fieldnames = ['Statistic Measure', 'Naive Bayes Multinomial', 'Naive Bayes Binomial', 'KNN', 'SVM', 'Random Forest', 'My Method']
#ROC plot - mean
mean_tpr = 0.0
mean_fpr = np.linspace(0, 1, 100)
all_tpr = []
for i, (train_index, test_index) in enumerate(kf):
#10-fold cross validation (9 samples for training, 1 for testing)
X_train1, X_test = X_train[train_index], X_train[test_index]
Y_train1, Y_test = Y_train[train_index], Y_train[test_index]
probas_ = pipeline.fit(X_train1,Y_train1).predict(X_test)
if i == 0:
yar = Y_test
pr = probas_
elif i == 1:
yar1 = Y_test
pr1 = probas_
elif i == 2:
yar2 = Y_test
pr2 = probas_
elif i == 3:
yar3 = Y_test
pr3 = probas_
elif i == 4:
yar4 = Y_test
pr4 = probas_
elif i == 5:
yar5 = Y_test
pr5 = probas_
elif i == 6:
yar6 = Y_test
pr6 = probas_
elif i == 7:
yar7 = Y_test
pr7 = probas_
elif i == 8:
yar8 = Y_test
pr8 = probas_
elif i == 9:
yar9 = Y_test
pr9 = probas_
# Accuracy
stats.append(accuracy_score(Y_test, probas_))
#ROC plot
for j in range(5):
mean_tpr = 0.0
mean_fpr = np.linspace(0, 1, 100)
all_tpr = []
for i in range(10):
# Compute ROC curve and area the curve
if i == 0:
fpr, tpr, thresholds = roc_curve(yar, pr, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
elif i == 1:
fpr, tpr, thresholds = roc_curve(yar1, pr1, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
elif i == 2:
fpr, tpr, thresholds = roc_curve(yar2, pr2, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
elif i == 3:
fpr, tpr, thresholds = roc_curve(yar3, pr3, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
elif i == 4:
fpr, tpr, thresholds = roc_curve(yar4, pr4, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
elif i == 5:
fpr, tpr, thresholds = roc_curve(yar5, pr5, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
elif i == 6:
fpr, tpr, thresholds = roc_curve(yar6, pr6, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
elif i == 7:
fpr, tpr, thresholds = roc_curve(yar7, pr7, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
elif i == 8:
fpr, tpr, thresholds = roc_curve(yar8, pr8, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
elif i == 9:
fpr, tpr, thresholds = roc_curve(yar9, pr9, pos_label = j) # pos_label is for the 5 categories
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.6f)' % (i, roc_auc))
plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck')
mean_tpr /= len(kf)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
stats.append(mean_auc)
plt.plot(mean_fpr, mean_tpr, 'k--',label='Mean ROC (area = %0.2f)' % mean_auc, lw=2)
plt.xlim([0.0, 1.05])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
if num==1:
plt.title('MultinomialNB'+'-'+le.classes_[j])
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.savefig(fieldnames[1]+'_'+le.classes_[j]+'.png',bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.clf()
elif num==2:
plt.title('Binomial'+'-'+le.classes_[j])
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.savefig(fieldnames[2]+'_'+le.classes_[j]+'.png',bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.clf()
elif num==3:
plt.title('KNN'+'-'+le.classes_[j])
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.savefig(fieldnames[3]+'_'+le.classes_[j]+'.png',bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.clf()
elif num==4:
plt.title('SVM'+'-'+le.classes_[j])
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.savefig(fieldnames[4]+'_'+le.classes_[j]+'.png',bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.clf()
elif num==5:
plt.title('RandomForest'+'-'+le.classes_[j])
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.savefig(fieldnames[5]+'_'+le.classes_[j]+'.png',bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.clf()
elif num==6:
plt.title('MyMethod'+'-'+le.classes_[j])
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.savefig(fieldnames[6]+'_'+le.classes_[j]+'.png',bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.clf()
return stats
#Read Data
df=pd.read_csv("train_set.csv",sep="\t")
le = preprocessing.LabelEncoder()
le.fit(df["Category"])
Y_train=le.transform(df["Category"])
X_train1=df['Content']
X_train2=[]
for i in range(len(X_train1)):
X_train2.append(10*df['Title'][i]+df['Content'][i])
X_train=np.array(X_train2)
vectorizer=CountVectorizer(stop_words='english')
transformer=TfidfTransformer()
svd=TruncatedSVD(n_components=200, random_state=42)
stats = ['Accuracy','','','','','','','','','','ROC','','','','']
stats1 = []
stats2 = []
stats3 = []
stats4 = []
stats5 = []
stats6 = []
fieldnames = ['Statistic Measure', 'Naive Bayes Multinomial', 'Naive Bayes Binomial', 'KNN', 'SVM', 'Random Forest', 'My Method']
import csv
csv_out = open('EvaluationMetric_10fold.csv', 'wb')
clwriter = csv.writer(csv_out)
kf = KFold(len(X_train), n_folds=10)
#Multinomial NaiveBayes
clf=MultinomialNB()
pipeline = Pipeline([
('vect', vectorizer),
('tfidf', transformer),
('clf', clf)
])
stats1=compute_and_print(1,pipeline,X_train,kf)
#bernouli
clf=BernoulliNB()
pipeline = Pipeline([
('vect', vectorizer),
('tfidf', transformer),
('svd',svd),
('clf', clf)
])
stats2=compute_and_print(2,pipeline,X_train,kf)
#knn
clf=KNeighborsClassifier(n_jobs=-1)
pipeline = Pipeline([
('vect', vectorizer),
('tfidf', transformer),
('svd',svd),
('clf', clf)
])
stats3=compute_and_print(3,pipeline,X_train,kf)
#svm
clf=svm.SVC()
pipeline = Pipeline([
('vect', vectorizer),
('tfidf', transformer),
('svd',svd),
('clf', clf)
])
stats4=compute_and_print(4,pipeline,X_train,kf)
#randomforest
clf=RandomForestClassifier(n_jobs=-1)
pipeline = Pipeline([
('vect', vectorizer),
('tfidf', transformer),
('svd',svd),
('clf', clf)
])
stats5=compute_and_print(5,pipeline,X_train,kf)
#My method---Voting Classifier
clf1 = BernoulliNB(fit_prior=False)
clf2 = KNeighborsClassifier(weights='distance',n_jobs=-1)
clf3 = RandomForestClassifier(n_estimators=500,n_jobs=-1)
clf = VotingClassifier(estimators=[('bnb',clf1),('knn',clf2),('rf',clf3)], voting='hard')
pipeline = Pipeline([
('vect', vectorizer),
('tfidf', transformer),
('svd',svd),
('clf', clf)
])
stats6=compute_and_print(6,pipeline,X_train,kf)
#save stats to csv file
rows = zip(stats, stats1, stats2, stats3, stats4, stats5, stats6)
clwriter.writerow(fieldnames)
clwriter.writerows(rows)
csv_out.close()