-
Notifications
You must be signed in to change notification settings - Fork 0
/
Classifier.py
544 lines (451 loc) · 26.8 KB
/
Classifier.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# MACHINE LEARNING PROJECT
# Author : Anindya Chakrabarty
from Input import Input ,MongoDB, Report
class Utility:
def makeDir(self,parentDirectory, dirName):
import os
new_dir = os.path.join(parentDirectory, dirName+'\\')
if not os.path.isdir(new_dir):
os.makedirs(new_dir)
return new_dir
def SetLogger(self):
import logging
logger = logging.getLogger("Classifier")
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('LogFile.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
return logger
def stopwatchStart(self):
import time
self.start_=time.perf_counter()
def stopwatchStop(self):
import time
self.finish_=time.perf_counter()
def showTime(self):
import time
import numpy as np
print(f'This operation has finished in {np.round(self.finish_-self.start_,2)} second(s)')
class NaturalLanguageProcessor:
def __init__(self,input):
import pandas as pd
import os
import numpy as np
import logging
import warnings
warnings.filterwarnings('ignore')
self.input_=input
self.util_=Utility()
self.logger_=self.util_.SetLogger()
self.dataset_=input.readMongoData()
self.Header_=list(self.dataset_.columns)
self.Header_.remove('Date')
self.dataset_=self.dataset_[self.Header_]
print(self.dataset_.head())
self.dependentVariableName_=input.dependentVariableName_
self.dataFileName_=input.collectionName_
self.parentDirectory_ = os.path.dirname(os.getcwd())
self.exploratoryDataAnalysisDir_= self.util_.makeDir(self.parentDirectory_,"Natural Language Processing")
def joinNews(self):
import pandas as pd
self.dataset_["Combined_News"]=0
print("**************Collating Headlines *********************")
for row in range(len(self.dataset_.index)):
self.dataset_["Combined_News"][row]= " ".join(str(x) for x in self.dataset_.iloc[row,2:len(self.dataset_.columns)])
self.data_=self.dataset_[[self.dependentVariableName_,"Combined_News"]]
def cleanNews(self):
import re
import nltk
from nltk.corpus import stopwords
self.data_["Combined_News"] = self.data_["Combined_News"].map(lambda x : ' '.join(re.sub("[^a-zA-Z]"," ",x).split()))
self.data_["Combined_News"] = self.data_["Combined_News"].map(lambda x: x.lower())
self.data_["Combined_News"] = self.data_["Combined_News"].map(lambda x : ' '.join([w for w in x.split() if w not in stopwords.words('english')]))
def lemmatizeData(self):
import nltk
from nltk.stem import WordNetLemmatizer
lemmer = WordNetLemmatizer()
self.data_["Combined_News"] = self.data_["Combined_News"].map(lambda x : ' '.join([lemmer.lemmatize(w) for w in x.split()]))
def bagOfWord(self,max_features=1000):
print("**************Performing Bag of Words *********************")
from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd
cv_vectorizer = CountVectorizer(min_df=.015, max_df=.8, max_features=max_features, ngram_range=[1, 3])
cv = cv_vectorizer.fit_transform(data["Combined_News"])
print("Bow-CV :", cv.shape)
dataBow= pd.DataFrame(cv.toarray(), columns=cv_vectorizer.get_feature_names())
dataBow = pd.concat([self.data_, dataBow], axis = 1)
dataBow.drop("Combined_News", axis = 1,inplace = True)
self.data_=dataBow
print(self.data_.head())
print(self.data_.tail())
def TFIDF(self):
from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd
print("**************Performing TFIDF *********************")
tfidf_vectorizer = TfidfVectorizer(min_df=.02, max_df=.7, ngram_range=[1,3])
tfidf = tfidf_vectorizer.fit_transform(self.data_["Combined_News"])
print("TF:IDF :", tfidf.shape)
dataTfidf = pd.DataFrame(tfidf.toarray(), columns=tfidf_vectorizer.get_feature_names(), index=self.data_.index)
dataTfidf = pd.concat([self.data_, dataTfidf], axis = 1)
dataTfidf.drop("Combined_News", axis = 1,inplace = True)
self.data_=dataTfidf
print(self.data_.head())
print(self.data_.tail())
def splitData(self):
from sklearn.model_selection import train_test_split
from collections import Counter
Y = self.data_[self.input_.dependentVariableName_]
X=self.data_.drop(self.input_.dependentVariableName_,axis=1)
self.X_train_, self.X_test_, self.Y_train_, self.Y_test_ = train_test_split(X, Y, train_size = 0.85, random_state = 21)
print('Original Training Dataset Shape {}'.format(Counter(self.Y_train_)))
print('Original Testing Dataset Shape {}'.format(Counter(self.Y_test_)))
def isImbalence(self,threshold):
imbl=self.data_[self.input_.dependentVariableName_].value_counts()
if (imbl[1]/imbl[0]<threshold or imbl[0]/imbl[1]<threshold):
print(f'We have imbalence dataset with count of 1 in Total Data : {imbl[1]} and count of 0 in Total Data : {imbl[0]}')
else:
print(f'We do not have imbalence dataset with count of 1 in Total Data : {imbl[1]} and count of 0 in Total Data : {imbl[0]}')
return (imbl[1]/imbl[0]<threshold or imbl[0]/imbl[1]<threshold)
def overSampling(self,ratio):
from imblearn.over_sampling import RandomOverSampler
from collections import Counter
os=RandomOverSampler(ratio)
self.X_train_, self.Y_train_ =os.fit_resample(self.X_train_, self.Y_train_)
print('Over Sampled Training Dataset Shape {}'.format(Counter(self.Y_train_)))
def SMOTE(self,k):
from imblearn.over_sampling import SMOTE
from collections import Counter
smote=SMOTE(sampling_strategy='auto', k_neighbors=k, random_state=100)
self.X_train_, self.Y_train_ =smote.fit_resample(self.X_train_, self.Y_train_)
print('SMOTE Training Dataset Shape {}'.format(Counter(self.Y_train_)))
def handlingImbalanceData(self):
if (self.isImbalence(0.5)):
#self.overSampling(1)
self.SMOTE(1)
else:
print("Data set is balanced and hence no changes made")
def run(self):
self.joinNews()
self.cleanNews()
self.lemmatizeData()
self.TFIDF()
self.splitData()
self.handlingImbalanceData()
class Classifier:
def __init__(self,input):
import pandas as pd
import os
import numpy as np
pd.set_option('display.max_columns', None)
self.input_=input
self.bestModels_={}
self.NLP_=NaturalLanguageProcessor(self.input_)
self.NLP_.run()
self.NLP_.logger_.debug("Ending Natural Language Processing")
self.util_=Utility()
self.parentDirectory_ = os.path.dirname(os.getcwd())
self.Model_Dir_= self.util_.makeDir(self.parentDirectory_,"Machine Learning Models")
def getHyperParameters(self):
self.grid_params_NaiveBayesClassifier_ = {'alpha' : [1,2,3]}
self.grid_params_RandomForestClassifier_ = {'n_estimators' : [100,200,300,400,500],'max_depth' : [10, 7, 5, 3],'criterion' : ['entropy', 'gini']}
self.grid_params_XGBClassifier_={'n_estimators' : [100,200,300],'learning_rate' : [1.0, 0.1, 0.05],'max_depth':[2,3,6],'min_child_weight':[1,2]}
self.grid_params_AdaBoostClassifier_={'n_estimators' : [100,200,300],'learning_rate' : [1.0, 0.1, 0.05]}
self.grid_params_GradientBoostingClassifier_={'n_estimators' : [100,200,300],'learning_rate' : [1.0, 0.1, 0.05],'max_depth':[2,3,6]}
self.grid_params_KernelSupportVectorMachine_=[{'kernel': ['rbf','sigmoid','linear'], 'gamma': [1e-2]}]
self.grid_params_LogisticRegression_= {'C' : [0.0001, 0.01, 0.05, 0.2, 1],'penalty' : ['l1', 'l2']}
self.grid_params_ExtraTreesClassifier_={'n_estimators' : [100,200,300,400,500],'max_depth' : [10, 7, 5, 3],'criterion' : ['entropy', 'gini']}
def tuneNaiveBayesClassifier(self):
import numpy as np
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import StratifiedKFold,KFold,GridSearchCV,cross_val_score
print("**************Tuning Naive Bayes Classifier*********************")
self.classifier_ = MultinomialNB()
grid_object = GridSearchCV(estimator =self.classifier_, param_grid = self.grid_params_NaiveBayesClassifier_, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(self.NLP_.X_train_,self.NLP_.Y_train_)
print("Best Parameters : ", grid_object.best_params_)
print("Best_ROC-AUC : ", round(grid_object.best_score_ * 100, 2))
print("Best model : ", grid_object.best_estimator_)
self.bestModels_.update({'Naive Bayes Classifier':grid_object.best_estimator_})
self.Y_pred_ = grid_object.best_estimator_.predict(self.NLP_.X_test_)
self.probs_ = grid_object.best_estimator_.predict_proba(self.NLP_.X_test_)
kfold = KFold(n_splits=10, random_state=25, shuffle=True)
results = cross_val_score(grid_object.best_estimator_, self.NLP_.X_test_, self.NLP_.Y_test_, cv=kfold)
results = results * 100
results = np.round(results,2)
print("Cross Validation Accuracy : ", round(results.mean(), 2))
print("Cross Validation Accuracy in every fold : ", results)
return self.getResult('Tuned Naive Bayes Classifier')
def tuneRandomForestClassifier(self):
import numpy as np
from sklearn.model_selection import StratifiedKFold,KFold,GridSearchCV,cross_val_score
from sklearn.ensemble import RandomForestClassifier
print("**************Tuning Random Forest Classifier*********************")
self.classifier_ = RandomForestClassifier()
grid_object = GridSearchCV(estimator = self.classifier_, param_grid = self.grid_params_RandomForestClassifier_, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(self.NLP_.X_train_, self.NLP_.Y_train_)
print("Best Parameters : ", grid_object.best_params_)
print("Best_ROC-AUC : ", round(grid_object.best_score_ * 100, 2))
print("Best model : ", grid_object.best_estimator_)
self.bestModels_.update({'Random Forest Classifier':grid_object.best_estimator_})
self.Y_pred_ = grid_object.best_estimator_.predict(self.NLP_.X_test_)
self.probs_ = grid_object.best_estimator_.predict_proba(self.NLP_.X_test_)
kfold = KFold(n_splits=10, random_state=25, shuffle=True)
results = cross_val_score(grid_object.best_estimator_, self.NLP_.X_test_, self.NLP_.Y_test_, cv=kfold)
results = results * 100
results = np.round(results,2)
print("Cross Validation Accuracy : ", round(results.mean(), 2))
print("Cross Validation Accuracy in every fold : ", results)
return self.getResult('Tuned Random Forest Classifier')
def tuneXGBClassifier(self):
import numpy as np
from xgboost import XGBClassifier
from sklearn.model_selection import StratifiedKFold,KFold,GridSearchCV,cross_val_score
print("**************Tuning XG Boost Classifier*********************")
self.classifier_=XGBClassifier()
grid_object = GridSearchCV(estimator = self.classifier_, param_grid = self.grid_params_XGBClassifier_, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(self.NLP_.X_train_, self.NLP_.Y_train_)
print("Best Parameters : ", grid_object.best_params_)
print("Best_ROC-AUC : ", round(grid_object.best_score_ * 100, 2))
print("Best model : ", grid_object.best_estimator_)
self.bestModels_.update({'XG Boost Classifier':grid_object.best_estimator_})
self.Y_pred_ = grid_object.best_estimator_.predict(self.NLP_.X_test_)
self.probs_ = grid_object.best_estimator_.predict_proba(self.NLP_.X_test_)
kfold = KFold(n_splits=10, random_state=25, shuffle=True)
results = cross_val_score(grid_object.best_estimator_, self.NLP_.X_test_, self.NLP_.Y_test_, cv=kfold)
results = results * 100
results = np.round(results,2)
print("Cross Validation Accuracy : ", round(results.mean(), 2))
print("Cross Validation Accuracy in every fold : ", results)
return self.getResult('Tuned XG Boost Classifier')
def tuneAdaBoostClassifier(self):
import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import StratifiedKFold,KFold,GridSearchCV,cross_val_score
print("**************Tuning Ada Boost Classifier*********************")
self.classifier_ = AdaBoostClassifier()
grid_object = GridSearchCV(estimator = self.classifier_, param_grid = self.grid_params_AdaBoostClassifier_, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(self.NLP_.X_train_, self.NLP_.Y_train_)
print("Best Parameters : ", grid_object.best_params_)
print("Best_ROC-AUC : ", round(grid_object.best_score_ * 100, 2))
print("Best model : ", grid_object.best_estimator_)
self.bestModels_.update({'Ada Boost Classifier':grid_object.best_estimator_})
self.Y_pred_ = grid_object.best_estimator_.predict(self.NLP_.X_test_)
self.probs_ = grid_object.best_estimator_.predict_proba(self.NLP_.X_test_)
kfold = KFold(n_splits=10, random_state=25, shuffle=True)
results = cross_val_score(grid_object.best_estimator_, self.NLP_.X_test_, self.NLP_.Y_test_, cv=kfold)
results = results * 100
results = np.round(results,2)
print("Cross Validation Accuracy : ", round(results.mean(), 2))
print("Cross Validation Accuracy in every fold : ", results)
return self.getResult('Tuned AdaBoost Classifier')
def tuneGradientBoostingClassifier(self):
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import StratifiedKFold,KFold,GridSearchCV,cross_val_score
print("**************Tuning Grdient Boosting Classifier*********************")
self.classifier_ = GradientBoostingClassifier()
grid_object = GridSearchCV(estimator = self.classifier_, param_grid = self.grid_params_GradientBoostingClassifier_, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(self.NLP_.X_train_, self.NLP_.Y_train_)
print("Best Parameters : ", grid_object.best_params_)
print("Best_ROC-AUC : ", round(grid_object.best_score_ * 100, 2))
print("Best model : ", grid_object.best_estimator_)
self.bestModels_.update({'Grdient Boosting Classifier':grid_object.best_estimator_})
self.Y_pred_ = grid_object.best_estimator_.predict(self.NLP_.X_test_)
self.probs_ = grid_object.best_estimator_.predict_proba(self.NLP_.X_test_)
kfold = KFold(n_splits=10, random_state=25, shuffle=True)
results = cross_val_score(grid_object.best_estimator_, self.NLP_.X_test_, self.NLP_.Y_test_, cv=kfold)
results = results * 100
results = np.round(results,2)
print("Cross Validation Accuracy : ", round(results.mean(), 2))
print("Cross Validation Accuracy in every fold : ", results)
return self.getResult('Tuned Gradient Boosting Classifier')
def tuneKernelSupportVectorMachine(self):
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold,KFold,GridSearchCV,cross_val_score
print("**************Tuning Kernel Support Vector Machine*********************")
self.classifier_=SVC(probability=True)
grid_object = GridSearchCV(estimator = self.classifier_, param_grid = self.grid_params_KernelSupportVectorMachine_, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(self.NLP_.X_train_, self.NLP_.Y_train_)
print("Best Parameters : ", grid_object.best_params_)
print("Best_ROC-AUC : ", round(grid_object.best_score_ * 100, 2))
print("Best model : ", grid_object.best_estimator_)
self.bestModels_.update({'Kernel Support Vector Machine':grid_object.best_estimator_})
self.Y_pred_ = grid_object.best_estimator_.predict(self.NLP_.X_test_)
self.probs_ = grid_object.best_estimator_.predict_proba(self.NLP_.X_test_)
kfold = KFold(n_splits=10, random_state=25, shuffle=True)
results = cross_val_score(grid_object.best_estimator_, self.NLP_.X_test_, self.NLP_.Y_test_, cv=kfold)
results = results * 100
results = np.round(results,2)
print("Cross Validation Accuracy : ", round(results.mean(), 2))
print("Cross Validation Accuracy in every fold : ", results)
return self.getResult('Tuned Support Vector Machine')
def tuneLogisticRegression(self):
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold,KFold,GridSearchCV,cross_val_score
print("**************Tuning Logistic Regression*********************")
self.classifier_=LogisticRegression()
grid_object = GridSearchCV(estimator = self.classifier_, param_grid = self.grid_params_LogisticRegression_, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(self.NLP_.X_train_, self.NLP_.Y_train_)
print("Best Parameters : ", grid_object.best_params_)
print("Best_ROC-AUC : ", round(grid_object.best_score_ * 100, 2))
print("Best model : ", grid_object.best_estimator_)
self.bestModels_.update({'Logistic Regression':grid_object.best_estimator_})
self.Y_pred_ = grid_object.best_estimator_.predict(self.NLP_.X_test_)
self.probs_ = grid_object.best_estimator_.predict_proba(self.NLP_.X_test_)
kfold = KFold(n_splits=10, random_state=25, shuffle=True)
results = cross_val_score(grid_object.best_estimator_, self.NLP_.X_test_, self.NLP_.Y_test_, cv=kfold)
results = results * 100
results = np.round(results,2)
print("Cross Validation Accuracy : ", round(results.mean(), 2))
print("Cross Validation Accuracy in every fold : ", results)
return self.getResult('Tuned Logistic Regression')
def tuneExtraTreesClassifier(self):
import numpy as np
from sklearn.model_selection import StratifiedKFold,KFold,GridSearchCV,cross_val_score
from sklearn.ensemble import ExtraTreesClassifier
print("**************Tuning Extra Trees Classifier*********************")
self.classifier_ = ExtraTreesClassifier()
grid_object = GridSearchCV(estimator = self.classifier_, param_grid = self.grid_params_ExtraTreesClassifier_, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(self.NLP_.X_train_, self.NLP_.Y_train_)
print("Best Parameters : ", grid_object.best_params_)
print("Best_ROC-AUC : ", round(grid_object.best_score_ * 100, 2))
print("Best model : ", grid_object.best_estimator_)
self.bestModels_.update({'Extra Trees Classifier':grid_object.best_estimator_})
self.Y_pred_ = grid_object.best_estimator_.predict(self.NLP_.X_test_)
self.probs_ = grid_object.best_estimator_.predict_proba(self.NLP_.X_test_)
kfold = KFold(n_splits=10, random_state=25, shuffle=True)
results = cross_val_score(grid_object.best_estimator_, self.NLP_.X_test_, self.NLP_.Y_test_, cv=kfold)
results = results * 100
results = np.round(results,2)
print("Cross Validation Accuracy : ", round(results.mean(), 2))
print("Cross Validation Accuracy in every fold : ", results)
return self.getResult('Tuned Extra Trees Classifier')
def compareModel(self):
self.report_=Report()
self.getHyperParameters()
self.NLP_.logger_.debug("Tuning Logistic Regression ")
self.NLP_.util_.stopwatchStart()
lst=self.tuneLogisticRegression()
self.report_.insertResult(lst)
self.NLP_.util_.stopwatchStop()
self.NLP_.util_.showTime()
self.NLP_.logger_.debug("Tuning Extra Trees Classifier ")
self.NLP_.util_.stopwatchStart()
lst=self.tuneExtraTreesClassifier()
self.report_.insertResult(lst)
self.NLP_.util_.stopwatchStop()
self.NLP_.util_.showTime()
self.NLP_.logger_.debug("Tuning Naive Bayes Classifier ")
self.NLP_.util_.stopwatchStart()
lst=self.tuneNaiveBayesClassifier()
self.report_.insertResult(lst)
self.NLP_.util_.stopwatchStop()
self.NLP_.util_.showTime()
self.NLP_.logger_.debug("Tuning Random Forest Classifier ")
self.NLP_.util_.stopwatchStart()
lst=self.tuneRandomForestClassifier()
self.report_.insertResult(lst)
self.NLP_.util_.stopwatchStop()
self.NLP_.util_.showTime()
self.NLP_.logger_.debug("Tuning AdaBoost Classifier ")
self.NLP_.util_.stopwatchStart()
lst=self.tuneAdaBoostClassifier()
self.report_.insertResult(lst)
self.NLP_.util_.stopwatchStop()
self.NLP_.util_.showTime()
self.NLP_.logger_.debug("Tuning Gradient Boosting Classifier ")
self.NLP_.util_.stopwatchStart()
lst=self.tuneGradientBoostingClassifier()
self.report_.insertResult(lst)
self.NLP_.util_.stopwatchStop()
self.NLP_.util_.showTime()
self.NLP_.logger_.debug("Tuning XGBClassifier ")
self.NLP_.util_.stopwatchStart()
lst=self.tuneXGBClassifier()
self.report_.insertResult(lst)
self.NLP_.util_.stopwatchStop()
self.NLP_.util_.showTime()
self.NLP_.logger_.debug("Tuning Support Vector Machine ")
self.NLP_.util_.stopwatchStart()
lst=self.tuneKernelSupportVectorMachine()
self.report_.insertResult(lst)
self.NLP_.util_.stopwatchStop()
self.NLP_.util_.showTime()
self.report_.report_= self.report_.report_.sort_values(["Accuracy"], ascending =False)
print(self.report_.report_)
self.input_.writeMongoData(self.report_.report_,"TunedModelComparisonReport")
self.NLP_.logger_.debug("Ending Model Calibration ")
def compareModel1(self):
self.getHyperParameters()
self.algoCall_={"Tuning Naive Bayes Classifier ":self.tuneNaiveBayesClassifier(),
"Tuning Random Forest Classifier":self.tuneRandomForestClassifier(),
"Tuning AdaBoost Classifier":self.tuneAdaBoostClassifier(),
"Tuning Gradient Boosting Classifier":self.tuneGradientBoostingClassifier(),
"Tuning XGBClassifier":self.tuneXGBClassifier(),
"Tuning Support Vector Machine":self.tuneKernelSupportVectorMachine()}
self.report_=Report()
for key in self.algoCall_:
self.report_.insertResult(self.algoCall_[key])
self.report_.report_= self.report_.report_.sort_values(["Accuracy"], ascending =False)
self.NLP_.logger_.debug("Ending Program. Thanks for your visit ")
def getResult(self,algoName):
from sklearn.metrics import roc_curve, auc, classification_report, confusion_matrix, precision_score, recall_score, accuracy_score, precision_recall_curve
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
report=[algoName]
print("\n", "Confusion Matrix")
cm = confusion_matrix(self.NLP_.Y_test_, self.Y_pred_)
print("\n", cm, "\n")
#sns.heatmap(cm, square=True, annot=True, cbar=False, fmt = 'g', cmap='RdBu',
#xticklabels=['ham', 'spam'], yticklabels=['ham', 'spam'])
#plt.xlabel('true label')
#plt.ylabel('predicted label')
#plt.show()
print("\n", "Classification Report", "\n")
print(classification_report(self.NLP_.Y_test_, self.Y_pred_))
print("Overall Accuracy : ", round(accuracy_score(self.NLP_.Y_test_, self.Y_pred_) * 100, 2))
print("Precision Score : ", round(precision_score(self.NLP_.Y_test_, self.Y_pred_, average='binary') * 100, 2))
print("Recall Score : ", round(recall_score(self.NLP_.Y_test_, self.Y_pred_, average='binary') * 100, 2))
preds = self.probs_[:,1] # this is the probability for 1, column 0 has probability for 0. Prob(0) + Prob(1) = 1
fpr, tpr, threshold = roc_curve(self.NLP_.Y_test_, preds)
roc_auc = auc(fpr, tpr)
print("AUC : ", round(roc_auc * 100, 2), "\n")
report.append(round(accuracy_score(self.NLP_.Y_test_, self.Y_pred_) * 100, 2))
report.append(round(precision_score(self.NLP_.Y_test_, self.Y_pred_, average='binary') * 100, 2))
report.append(round(recall_score(self.NLP_.Y_test_, self.Y_pred_, average='binary') * 100, 2))
report.append(round(roc_auc * 100, 2))
plt.figure()
plt.plot(fpr, tpr, label='Best Model on Test Data (area = %0.2f)' % roc_auc)
plt.plot([0.0, 1.0], [0, 1],'r--')
plt.xlim([-0.1, 1.1])
plt.ylim([-0.1, 1.1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('RoC-AUC on Test Data')
plt.legend(loc="lower right")
#plt.savefig('Log_ROC')
#plt.show()
return report
def predict(self,newData):
self.NLP_.logger_.debug("Starting Model prediction ")
import pandas as pd
import numpy as np
self.predictionReport_=Report()
self.newInput_=Input(self.input_.databaseName_,newData,self.input_.dependentVariableName_)
self.newDataSet_=self.newInput_.readMongoData()
self.NLP_.Header_.remove(self.input_.dependentVariableName_)
self.newData_=self.newDataSet_[self.NLP_.Header_].copy()
self.newData_ = pd.get_dummies(self.newData_,drop_first=False)
self.newData_=self.newData_.reindex(columns=list(self.NLP_.X_train_.columns),fill_value=0)
for key in self.bestModels_:
self.predictionReport_.insertPredictionResults([key,int(self.bestModels_[key].predict(self.newData_)),int(np.round(self.bestModels_[key].predict_proba(self.newData_)[0][0],2)*100),int(np.round(self.bestModels_[key].predict_proba(self.newData_)[0][1],2)*100)])
print(self.predictionReport_.predictionReport_)
self.NLP_.logger_.debug("Ending Model prediction. Good Bye")