-
Notifications
You must be signed in to change notification settings - Fork 2
/
HW3-part2.py
249 lines (186 loc) · 6.83 KB
/
HW3-part2.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 10 17:41:40 2017
@author: Dovla
"""
from sklearn.datasets import load_files
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.corpus import stopwords
from nltk.stem.snowball import EnglishStemmer
from nltk import word_tokenize
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn import metrics
import pprint as pp
from sklearn.naive_bayes import MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
stemmer = EnglishStemmer()
def stemming_tokenizer(text):
stemmed_text = [stemmer.stem(word) for word in word_tokenize(text, language='english')]
return stemmed_text
data_folder_training_set = "./Training"
data_folder_test_set = "./Test"
training_dataset = load_files(data_folder_training_set)
test_dataset = load_files(data_folder_test_set)
print(training_dataset.target_names)
X_train, X_test_DUMMY_to_ignore, Y_train, Y_test_DUMMY_to_ignore = train_test_split(training_dataset.data,
training_dataset.target,
test_size=0.0)
target_names = training_dataset.target_names
X_train_DUMMY_to_ignore, X_test, Y_train_DUMMY_to_ignore, Y_test = train_test_split(test_dataset.data,
test_dataset.target,
train_size=0.0)
target_names = training_dataset.target_names
print(Y_train.shape)
print(Y_test.shape)
print(target_names)
vectorizer = TfidfVectorizer(strip_accents= None,
preprocessor = None,
)
#nbc = MultinomialNB()
knnC = KNeighborsClassifier()
pipeline = Pipeline([
('vect', vectorizer),
('knnC', knnC),
])
parameters = {
'vect__tokenizer': [None, stemming_tokenizer],
'vect__ngram_range': [(1, 1), (1, 2),],
#'nbc__alpha': [.001,.01, 1.0, 10.],
'knnC__n_neighbors':[3,5,10],
'knnC__leaf_size':[30,50],
}
grid_search = GridSearchCV(pipeline,
parameters,
scoring=metrics.make_scorer(metrics.matthews_corrcoef),
cv=10,
n_jobs=2,
verbose=10)
grid_search.fit(X_train, Y_train)
Y_predicted = grid_search.predict(X_test)
output_classification_report = metrics.classification_report(
Y_test,
Y_predicted,
target_names=target_names)
print(output_classification_report)
confusion_matrix = metrics.confusion_matrix(Y_test, Y_predicted)
print("Confusion Matrix: True-Classes X Predicted-Classes")
print(confusion_matrix)
print("Matthews Corr Coef")
pp.pprint(metrics.matthews_corrcoef(Y_test, Y_predicted))
pp.pprint(grid_search.best_params_)
print("Accuracy")
pp.pprint(metrics.accuracy_score(Y_test, Y_predicted))
###############################################################
from sklearn import linear_model
data_folder_training_set1 = "./Training"
data_folder_test_set1 = "./Test"
training_dataset1 = load_files(data_folder_training_set1)
test_dataset1 = load_files(data_folder_test_set1)
print(training_dataset1.target_names)
X_train1, X_test_DUMMY_to_ignore1, Y_train1, Y_test_DUMMY_to_ignore1 = train_test_split(training_dataset1.data,
training_dataset1.target,
test_size=0.0)
target_names1 = training_dataset1.target_names
X_train_DUMMY_to_ignore1, X_test1, Y_train_DUMMY_to_ignore1, Y_test1 = train_test_split(test_dataset1.data,
test_dataset1.target,
train_size=0.0)
target_names1 = training_dataset1.target_names
print(Y_train1.shape)
print(Y_test1.shape)
print(target_names1)
vectorizer1 = TfidfVectorizer(strip_accents= None,
preprocessor = None,
)
lmC = linear_model.SGDClassifier(alpha=0.0001, average=False, class_weight=None, epsilon=0.1,
eta0=0.0, fit_intercept=True, l1_ratio=0.15,
learning_rate='optimal', loss='hinge', n_iter=5, n_jobs=2,
power_t=0.5, random_state=None, shuffle=True,)
pipeline1 = Pipeline([
('vect', vectorizer1),
('lmC', lmC),
])
parameters1 = {
'vect__tokenizer': [None, stemming_tokenizer],
'vect__ngram_range': [(1, 1), (1, 2),],
'lmC__loss':['hinge','log','squared_loss'],
'lmC__penalty':['l2','l1','elasticnet']
}
grid_search1 = GridSearchCV(pipeline1,
parameters1,
scoring=metrics.make_scorer(metrics.matthews_corrcoef),
cv=10,
n_jobs=2,
verbose=10)
grid_search1.fit(X_train1, Y_train1)
Y_predicted1 = grid_search1.predict(X_test1)
output_classification_report1 = metrics.classification_report(
Y_test1,
Y_predicted1,
target_names=target_names1)
print(output_classification_report1)
confusion_matrix1 = metrics.confusion_matrix(Y_test1, Y_predicted1)
print("Confusion Matrix: True-Classes X Predicted-Classes")
print(confusion_matrix1)
print("Matthews Corr Coef")
pp.pprint(metrics.matthews_corrcoef(Y_test1, Y_predicted1))
pp.pprint(grid_search1.best_params_)
print("Accuracy")
pp.pprint(metrics.accuracy_score(Y_test1, Y_predicted1))
####################################################################
from sklearn.svm import SVC
data_folder_training_set2 = "./Training"
data_folder_test_set2 = "./Test"
training_dataset2 = load_files(data_folder_training_set2)
test_dataset2 = load_files(data_folder_test_set2)
print(training_dataset2.target_names)
X_train2, X_test_DUMMY_to_ignore2, Y_train2, Y_test_DUMMY_to_ignore2 = train_test_split(training_dataset2.data,
training_dataset2.target,
test_size=0.0)
target_names2 = training_dataset2.target_names
X_train_DUMMY_to_ignore2, X_test2, Y_train_DUMMY_to_ignore2, Y_test2 = train_test_split(test_dataset2.data,
test_dataset2.target,
train_size=0.0)
target_names2 = training_dataset2.target_names
print(Y_train2.shape)
print(Y_test2.shape)
print(target_names2)
vectorizer2 = TfidfVectorizer(strip_accents= None,
preprocessor = None,
)
svcC = SVC()
pipeline2 = Pipeline([
('vect', vectorizer2),
('svcC', svcC),
])
parameters2 = {
'vect__tokenizer': [None, stemming_tokenizer],
'vect__ngram_range': [(1, 1), (1, 2),],
'svcC__C':[0.5,1,2],
'svcC__gamma':[1,2],
'svcC__kernel':['rbf','linear'],
}
grid_search2 = GridSearchCV(pipeline2,
parameters2,
scoring=metrics.make_scorer(metrics.matthews_corrcoef),
cv=10,
n_jobs=2,
verbose=10)
grid_search2.fit(X_train2, Y_train2)
Y_predicted2 = grid_search2.predict(X_test2)
output_classification_report2 = metrics.classification_report(
Y_test2,
Y_predicted2,
target_names=target_names2)
print(output_classification_report2)
confusion_matrix2 = metrics.confusion_matrix(Y_test2, Y_predicted2)
print("Confusion Matrix: True-Classes X Predicted-Classes")
print(confusion_matrix2)
print("Matthews Corr Coef")
pp.pprint(metrics.matthews_corrcoef(Y_test2, Y_predicted2))
print("Accuracy")
pp.pprint(metrics.accuracy_score(Y_test2, Y_predicted2))
print("Best Parameters:")
pp.pprint(grid_search2.best_params_)