-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevashish_Tripathi_classifications.py
404 lines (320 loc) · 15 KB
/
Devashish_Tripathi_classifications.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
Author: Devashish Tripathi
Description:Code which contains classes to perform classification in respective stages
"""
from time import time
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV, StratifiedKFold
from sklearn.pipeline import Pipeline
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.naive_bayes import GaussianNB, MultinomialNB
from sklearn.preprocessing import MaxAbsScaler, MinMaxScaler, StandardScaler
from sklearn.feature_selection import SelectKBest, chi2, mutual_info_classif, SequentialFeatureSelector
from sklearn.decomposition import PCA
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score, confusion_matrix, classification_report, make_scorer, ConfusionMatrixDisplay
"""Class to evaluate the various classifier using GridSearch without any other modifications"""
class classification_methods:
def __init__(self, classifier, features, labels):
self.classifier = classifier
self.features = features
self.labels = labels
"""Function to store models and params to consider"""
def create_classifier(self):
model = None
params = None
# Decision Tree
if self.classifier == "DecTree":
print("Decision Tree Classification")
model = DecisionTreeClassifier(
random_state=1, class_weight='balanced')
params = {'model__criterion': ['gini', 'entropy', 'log_loss'],
'model__splitter': ['best', 'random'],
'model__ccp_alpha': [0, 0.1, 0.05, 1, 0.001],
'model__max_features': ['auto', 'sqrt', 'log2']}
# Logisitic Regression
elif self.classifier == "LogRes":
print("Logisitic Regression Classification")
model = LogisticRegression(random_state=1, class_weight='balanced')
params = {'model__solver': [
'lbfgs', 'liblinear', 'saga', 'newton-cg']}
# Random Forest
elif self.classifier == "RanFor":
print("Random Forest Classification")
model = RandomForestClassifier(random_state=1,)
params = {'model__n_estimators': [10, 100, 200],
'model__criterion': ['gini', 'entropy', 'log_loss'],
'model__max_features': ['sqrt', 'log2', None],
'model__class_weight': ['balanced', 'balanced_subsample']
}
# SVC
elif self.classifier == "SVC":
print("Support Vector Machine Classification")
model = SVC(random_state=1, class_weight='balanced')
params = {"model__C": [1, 0.5, 0.01, 5],
"model__kernel": ['rbf', 'poly', 'sigmoid'],
}
elif self.classifier == "LSVC":
print("Linear SVM Classification")
model = LinearSVC(random_state=1, class_weight='balanced')
params = {"model__C": [1, 0.5, 0.01, 5],
}
# Gaussian Naive Bayes
elif self.classifier == "GNBay":
print("Gaussian Naive Bayes Classification")
model = GaussianNB()
params = {}
# Multinomial Naive Bayes
elif self.classifier == "MNBay":
print("Multinomial Naive Bayes Classification")
model = MultinomialNB()
params = {'model__alpha': [1, 0.5, 0.05, 0]}
elif self.classifier == "KNN":
print("K Nearest Neighbors Classification")
model = KNeighborsClassifier()
params = {"model__n_neighbors": [5, 300, 500],
"model__weights": ['uniform', 'distance'],
"model__p": [1, 2, 3]}
# Adaboost Classifier
elif self.classifier == "AdaB":
print("Applying Adaboost classifier")
model = AdaBoostClassifier(random_state=1)
be_dt = DecisionTreeClassifier(ccp_alpha=0.001, criterion='gini',
max_features='sqrt', splitter='random', random_state=1)
be_lr = LogisticRegression(solver='newton-cg', random_state=1)
be_rf = RandomForestClassifier(class_weight='balanced_subsample', criterion='entropy',
max_features=None, n_estimators=200, random_state=1)
be_svc = SVC(C=5, kernel='poly', random_state=1)
be_mnb = MultinomialNB(alpha=1)
be_gnb = GaussianNB()
be_knn = KNeighborsClassifier(
n_neighbors=1, p=1, weights='uniform')
params = {'model__base_estimator': [be_dt, be_gnb, be_knn, be_lr, be_svc, be_mnb, be_rf],
'model__n_estimators': [50, 100, 200]}
# GradBoost Classifier
elif self.classifier == "GradB":
print("Applying Gradboost classifier")
model = GradientBoostingClassifier(random_state=1)
params = {'model__loss': ['log_loss', 'exponential'],
'model__criterion': ['friedman_mse', 'squared_error'],
'model__ccp_alpha': [0, 1, 0.05, 5],
'model__n_estimators': [100, 200, 500]}
else:
print("Unknown Classifier...")
exit(0)
return model, params
"""Function to evaluate model"""
def eval_model(self):
X = self.features
y = self.labels
n_splits = 5
print("-"*70)
print(f'Running {self.classifier} for {n_splits} splits.')
print("-"*70)
run = 1
SKF = StratifiedKFold(n_splits=n_splits, random_state=1, shuffle=True)
for train_idx, test_idx in SKF.split(X, y):
X_train = X.iloc[train_idx]
y_train = y.iloc[train_idx]
X_test = X.iloc[test_idx]
y_test = y.iloc[test_idx]
print()
print("Run: ", run)
print()
model, params = self.create_classifier()
pipe = Pipeline([('model', model)])
scorer = make_scorer(f1_score, pos_label=1)
grid = GridSearchCV(pipe, params, cv=5, scoring=scorer)
start = time()
grid.fit(X_train, y_train)
print("Best params for this run:", grid.best_params_)
end = time()
y_train_pred = grid.predict(X_train)
y_test_pred = grid.predict(X_test)
print("Train data evaluation::")
cm = confusion_matrix(y_train, y_train_pred)
print(cm)
d1 = ConfusionMatrixDisplay(cm)
d1.plot()
plt.show()
print(classification_report(y_train, y_train_pred))
print("Accuracy:", accuracy_score(y_train, y_train_pred))
print("Test data evaluation::")
cm = confusion_matrix(y_test, y_test_pred)
print(cm)
d2 = ConfusionMatrixDisplay(cm)
d2.plot()
plt.show()
print(classification_report(y_test, y_test_pred))
print(cm)
print("Accuracy:", accuracy_score(y_test, y_test_pred))
tot_time = end-start
print("Time taken in the run:", tot_time)
run += 1
"""Class to select models based on weights, scalings etc."""
class classification_selection:
def __init__(self, classifier, features, labels):
self.classifier = classifier
self.features = features
self.labels = labels
def create_classifier(self):
model = None
# Decision Tree
if self.classifier == "DecTree":
print("Decision Tree Classification")
model = DecisionTreeClassifier(random_state=1, class_weight='balanced',
ccp_alpha=0.001, criterion='gini',
max_features='sqrt', splitter='random')
# Logisitic Regression
elif self.classifier == "LogRes":
print("Logisitic Regression Classification")
model = LogisticRegression(
random_state=1, class_weight='balanced', solver='newton-cg')
elif self.classifier == "MNBay":
print("Multinomial Naive Bayes Classification")
model = MultinomialNB()
params = {'model__alpha': [1, 0.5, 0.05, 0]}
# Adaboost Classifier
elif self.classifier == "AdaB":
print("Applying Adaboost classifier")
be_lr = LogisticRegression(
random_state=1, class_weight='balanced', solver='newton-cg')
model = AdaBoostClassifier(
random_state=1, base_estimator=be_lr, n_estimators=100)
# GradBoost Classifier
elif self.classifier == "GradB":
print("Applying Gradboost classifier")
model = GradientBoostingClassifier(random_state=1, ccp_alpha=0,
criterion='friedman_mse', loss='log_loss', n_estimators=500)
else:
print("Unknown Classifier...")
exit(0)
return model
"""Function to evaluate classifier"""
def eval_classifier(self):
X = self.features
y = self.labels
n_splits = 5
print("-"*70)
print(f'Running {self.classifier} for {n_splits} splits')
print("-"*70)
run = 1
SKF = StratifiedKFold(n_splits=n_splits, random_state=1, shuffle=True)
for train_idx, test_idx in SKF.split(X, y):
X_train = X.iloc[train_idx]
y_train = y.iloc[train_idx]
X_test = X.iloc[test_idx]
y_test = y.iloc[test_idx]
print()
print("Run: ", run)
print()
model = self.create_classifier()
pipe = Pipeline(
[('scaler', None), ('feature_sel', None), ('model', model)])
params = [
{'scaler': [None, MaxAbsScaler(), MinMaxScaler(), StandardScaler()],
'feature_sel': [None]},
{'scaler': [None, MaxAbsScaler(), MinMaxScaler(), StandardScaler()],
'feature_sel': [SelectKBest(mutual_info_classif)],
'feature_sel__k': [5, 10, 20, 30]},
{'scaler': [None, MaxAbsScaler(), MinMaxScaler(), StandardScaler()],
'feature_sel': [SelectKBest(chi2)],
'feature_sel__k': [5]},
{'scaler': [None, MaxAbsScaler(), MinMaxScaler(), StandardScaler()],
'feature_sel': [SequentialFeatureSelector(estimator=model)],
'feature_sel__direction': ['forward', 'backward'],
'feature_sel__n_features_to_select': ['auto']},
{'scaler': [None, MaxAbsScaler(), MinMaxScaler(), StandardScaler()],
'feature_sel': [PCA(random_state=1)],
'feature_sel__n_components': [5, 10, 20, 30]}
]
scorer = make_scorer(f1_score, pos_label=1)
grid = GridSearchCV(pipe, params, cv=5, scoring=scorer)
start = time()
grid.fit(X_train, y_train)
print("Best params for this run:", grid.best_params_)
end = time()
y_train_pred = grid.best_estimator_.predict(X_train)
y_test_pred = grid.best_estimator_.predict(X_test)
print("Train data evaluation::")
cm = confusion_matrix(y_train, y_train_pred)
print(cm)
# d1=ConfusionMatrixDisplay(cm)
# d1.plot()
# plt.show()
print(classification_report(y_train, y_train_pred))
print("Accuracy:", accuracy_score(y_train, y_train_pred))
print("Test data evaluation::")
cm = confusion_matrix(y_test, y_test_pred)
print(cm)
# d2=ConfusionMatrixDisplay(cm)
# d2.plot()
# plt.show()
print(classification_report(y_test, y_test_pred))
print("Accuracy:", accuracy_score(y_test, y_test_pred))
tot_time = end-start
print("Time taken in the run:", tot_time)
run += 1
"""Class to create the model based on the final classifier and get labels for the test data"""
class final_classifier:
def __init__(self, features, labels, test_data=None):
self.features = features
self.labels = labels
self.test_data = test_data
self.model = None
"""Creating the final model"""
def final_eval(self):
X = self.features
y = self.labels
be = LogisticRegression(
solver='newton-cg', class_weight='balanced', random_state=1)
ada_model = AdaBoostClassifier(
base_estimator=be, n_estimators=200, random_state=1)
n_splits = 5
run = 1
SKF = StratifiedKFold(n_splits=n_splits, random_state=1, shuffle=True)
for train_idx, test_idx in SKF.split(X, y):
X_train = X.iloc[train_idx]
y_train = y.iloc[train_idx]
X_test = X.iloc[test_idx]
y_test = y.iloc[test_idx]
print("-"*70)
print("Run: ", run)
print("-"*70)
start = time()
ada_model.fit(X_train, y_train)
end = time()
y_train_pred = ada_model.predict(X_train)
y_test_pred = ada_model.predict(X_test)
print("Train data evaluation::")
cm = confusion_matrix(y_train, y_train_pred)
d1 = ConfusionMatrixDisplay(cm)
d1.plot()
# plt.show()
print(cm)
print(classification_report(y_train, y_train_pred))
print("Accuracy:", accuracy_score(y_train, y_train_pred))
print("Test data evaluation::")
cm = confusion_matrix(y_test, y_test_pred)
d2 = ConfusionMatrixDisplay(cm)
d2.plot()
# plt.show()
print(cm)
print(classification_report(y_test, y_test_pred))
print("Accuracy:", accuracy_score(y_test, y_test_pred))
tot_time = end-start
print("Time taken for model fitting in the run: %.3f seconds" % tot_time)
run += 1
self.model = ada_model
"""Function for evaluating and Storing the final results on the test data"""
def eval_test(self, df_test):
ada_model = self.model
y = ada_model.predict(df_test)
print(len(y))
print(y)
with open('Devashish_Tripathi_testlabels.txt', 'w') as file:
for out in y:
file.write(f'{out}\n')