-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtestNLP.py
417 lines (360 loc) · 20.1 KB
/
testNLP.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
##############################
# Created on: January 2019 #
# #
# #
# Author: Youssef Kishk #
# Rimon Adel #
# Adel Atef #
# Sandra sherif #
# #
# #
# #
# #
##############################
import re
import pandas as pd
import pickle
from pprint import pprint
from nltk.corpus import stopwords
from nltk.stem.wordnet import WordNetLemmatizer
from nltk import word_tokenize
from sklearn import neighbors
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, RandomForestRegressor
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RandomizedSearchCV
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=UserWarning)
dataset = pd.read_csv('offenseval-training-v1.tsv', delimiter='\t')
mustBeRemovedList = ["@USER", "url"]
# #################################################################################################################
def remove_userTag():
datasetwithoutUserTag = []
for line in dataset['tweet']:
finalListOfWords = []
tweets = []
words = line.split()
for word in words:
if word not in mustBeRemovedList:
finalListOfWords.append(word)
tweets = " ".join(finalListOfWords)
datasetwithoutUserTag.append(tweets)
return datasetwithoutUserTag
# #################################################################################################################
# #################################################################################################################
noise_list = set(stopwords.words("english"))
# noise detection
def remove_noise(input_text):
words = word_tokenize(input_text)
noise_free_words = list()
i = 0;
for word in words:
if word.lower() not in noise_list:
noise_free_words.append(word)
i += 1
noise_free_text = " ".join(noise_free_words)
return noise_free_text
# #################################################################################################################
# #################################################################################################################
def lemetize_words(input_text):
words = word_tokenize(input_text)
new_words = []
lem = WordNetLemmatizer()
for word in words:
word = lem.lemmatize(word, "v")
new_words.append(word)
new_text = " ".join(new_words)
return new_text
# #################################################################################################################
# #################################################################################################################
def cleaning():
corpus = []
datasetwithoutUserTag = remove_userTag()
for line in datasetwithoutUserTag:
review = re.sub('[^a-zA-Z]', ' ', line)
review = review.lower()
# remove non segnificant words
review = remove_noise(review)
review = lemetize_words(review)
corpus.append(review)
return corpus
# #################################################################################################################
# #################################################################################################################
def bagOfWordsCreation(corpus):
cv = CountVectorizer(max_features=12000)
bagOfWords = cv.fit_transform(corpus).toarray()
rowsValues = []
for line in dataset['subtask_a']:
if line == "OFF":
rowsValues.append(1)
else:
rowsValues.append(0)
return (bagOfWords, rowsValues)
# #################################################################################################################
# #################################################################################################################
def classifiers(classifier):
# fitting classifer to the training set
classifier_to_save = classifier.fit(bagOfWords_train, rowsValues_train)
# predict the test set resulty
rowsValues_pred = classifier.predict(bagOfWords_train)
# confusion matrix
cm = confusion_matrix(rowsValues_train, rowsValues_pred)
print('confusuion matrix train before tunning\n', cm)
accuracyTrain = (cm[0][0] + cm[1][1]) / len(rowsValues_train)
rowsValues_pred = classifier.predict(bagOfWords_test)
cm = confusion_matrix(rowsValues_test, rowsValues_pred)
print('confusuion matrix test before tunning\n', cm)
accuracyTest = (cm[0][0] + cm[1][1]) / len(rowsValues_test)
return accuracyTrain, accuracyTest, classifier_to_save
# #################################################################################################################
# #################################################################################################################
def save_classifier(classifier_name, classifier_s):
save_classifier = open(classifier_name + ".pickle", "wb")
pickle.dump(classifier_s, save_classifier)
save_classifier.close()
return
# #################################################################################################################
# #################################################################################################################
def use_saved_classifierBeforeTunning(classifier_name):
classifier_f = open(classifier_name + ".pickle", "rb")
classifier = pickle.load(classifier_f)
classifier_f.close()
# predict the test set resulty
rowsValues_pred = classifier.predict(bagOfWords_train)
# confusion matrix
cm = confusion_matrix(rowsValues_train, rowsValues_pred)
print('confusuion matrix train before tunning\n', cm)
accuracyTrain = (cm[0][0] + cm[1][1]) / len(rowsValues_train)
rowsValues_pred = classifier.predict(bagOfWords_test)
cm = confusion_matrix(rowsValues_test, rowsValues_pred)
print('confusuion matrix test before tunning\n', cm)
accuracyTest = (cm[0][0] + cm[1][1]) / len(rowsValues_test)
return accuracyTrain, accuracyTest
# #################################################################################################################
# #################################################################################################################
def use_saved_classifierAfterTunning(classifier_name):
classifier_f = open(classifier_name + ".pickle", "rb")
classifier = pickle.load(classifier_f)
classifier_f.close()
# predict the test set resulty
rowsValues_pred = classifier.predict(bagOfWords_train)
# confusion matrix
cm = confusion_matrix(rowsValues_train, rowsValues_pred)
print('confusuion matrix train after tunning\n', cm)
accuracyTrain = (cm[0][0] + cm[1][1]) / len(rowsValues_train)
rowsValues_pred = classifier.predict(bagOfWords_test)
cm = confusion_matrix(rowsValues_test, rowsValues_pred)
print('confusuion matrix test after tunning\n', cm)
accuracyTest = (cm[0][0] + cm[1][1]) / len(rowsValues_test)
return accuracyTrain, accuracyTest
# #################################################################################################################
# #################################################################################################################
def create_parameter_grid_randomForest():
# Number of trees in random forest
n_estimators = [int(x) for x in pd.np.linspace(start=200, stop=2000, num=10)]
# Number of features to consider at every split
max_features = ['auto', 'sqrt']
# Maximum number of levels in tree
max_depth = [int(x) for x in pd.np.linspace(10, 110, num=11)]
max_depth.append(None)
# Minimum number of samples required to split a node
min_samples_split = [2, 5, 10]
# Minimum number of samples required at each leaf node
min_samples_leaf = [1, 2, 4]
# Create the random grid
random_grid = {'n_estimators': n_estimators,
'max_features': max_features,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
}
print('random_grid')
pprint(random_grid)
return random_grid
# #################################################################################################################
# #################################################################################################################
def create_parameter_grid_KNNClassifier():
# Number of neighbors to use
n_neighbors = [int(x) for x in pd.np.linspace(start=3, stop=300, num=10)]
# Create the random grid
random_grid = {'n_neighbors': n_neighbors,
}
print('random_grid')
pprint(random_grid)
return random_grid
# #################################################################################################################
# #################################################################################################################
def create_parameter_grid_DecisionTreeClassifier():
# The function to measure the quality of a split. Supported criteria are “gini” for the Gini impurity
# and “entropy” for the information gain.
criterion = ['gini', 'entropy']
# Maximum number of levels in tree
max_depth = [int(x) for x in pd.np.linspace(10, 150, num=11)]
max_depth.append(None)
# Create the random grid
random_grid = {'criterion': criterion,
'max_depth': max_depth,
}
print('random_grid')
pprint(random_grid)
return random_grid
# #################################################################################################################
# #################################################################################################################
def create_parameter_grid_LogisticRegression():
# Inverse of regularization strength; must be a positive float.
# Like in support vector machines, smaller values specify stronger regularization
C = [float(x) for x in pd.np.linspace(start=0.1, stop=5.0)]
# Create the random grid
random_grid = {'C': C,
}
print('random_grid')
pprint(random_grid)
return random_grid
# #################################################################################################################
# #################################################################################################################
def random_search_training(randomGrid,classifier):
# Use the random grid to search for best hyperparameters
# First create the base model to tune
rf = classifier
# Random search of parameters, using 3 fold cross validation,
# search across 100 different combinations, and use all available cores
rf_random = RandomizedSearchCV(estimator=rf, param_distributions=randomGrid, n_iter=30, cv=3, verbose=0,
random_state=42, n_jobs=-1, refit=True)
# Fit the random search model
rf_random.fit(bagOfWords_train, rowsValues_train)
print('rf_random.best_params')
var = rf_random.best_params_
print(var)
best_random = rf_random.best_estimator_
return best_random
# #################################################################################################################
# #################################################################################################################
def evaluate(classifier):
rowsValues_pred = classifier.predict(bagOfWords_test)
cm = confusion_matrix(rowsValues_test, rowsValues_pred)
print('confusuion matrix test\n', cm)
accuracyTest = (cm[0][0] + cm[1][1]) / len(rowsValues_test)
return accuracyTest
# #################################################################################################################
def saveDecicsionTreeClassiferBeforeAndAfterTunning():
#Find accuracy of training and validation data before tunning
accuracyTrain, accuracyTest, classifier_s = classifiers(DecisionTreeClassifier(max_depth=30))
save_classifier("decisionTreeClassifier", classifier_s)
#tunning process
random_Grid_DecisionTree = create_parameter_grid_DecisionTreeClassifier()
DecisionTree_classifier_s_t = random_search_training(random_Grid_DecisionTree,DecisionTreeClassifier())
save_classifier("decisionTreeClassifierTuned", DecisionTree_classifier_s_t)
return
# #################################################################################################################
def saveLogisticRegClassiferBeforeAndAfterTunning():
#Find accuracy of training and validation data before tunning
accuracyTrain, accuracyTest, classifier_s = classifiers(LogisticRegression())
save_classifier("logisticRegression", classifier_s)
#tunning process
random_Grid_logistic = create_parameter_grid_LogisticRegression()
logistic_classifier_s_t = random_search_training(random_Grid_logistic,LogisticRegression())
save_classifier("LogisticRegressionTuned", logistic_classifier_s_t)
return
# #################################################################################################################
def saveKNNClassiferBeforeAndAfterTunning():
#Find accuracy of training and validation data before tunning
knn = neighbors.KNeighborsClassifier(n_neighbors=5, weights='distance', algorithm='brute', leaf_size=30, p=2,
metric='cosine', metric_params=None, n_jobs=1)
accuracyTrain, accuracyTest, classifier_s = classifiers(knn)
save_classifier("KNN", classifier_s)
#tunning process
random_Grid_knn = create_parameter_grid_KNNClassifier()
knn_classifier_s_t = random_search_training(random_Grid_knn,neighbors.KNeighborsClassifier())
save_classifier("KNNTuned", knn_classifier_s_t)
return
# #################################################################################################################
def saveRandomForestClassiferBeforeAndAfterTunning():
#build classifier
accuracyTrain, accuracyTest, classifier_s = classifiers(RandomForestClassifier())
save_classifier("randomForest", classifier_s)
# classifier_tuned = RandomForestClassifier(n_estimators = 1200, min_samples_split= 2, min_samples_leaf= 4, max_features= 'sqrt' , max_depth = None)
# accuracyTrainTuned1, accuracyTestTuned1, classifier_s_t = classifiers(classifier_tuned)
random_Grid = create_parameter_grid_randomForest()
var= random_search_training(random_Grid,RandomForestClassifier())
save_classifier("randomForestTuned", var)
return
# #################################################################################################################
#End of functions
# #################################################################################################################
#Code Starts Running From here by calling set of the above implemented functions
corpus = cleaning()
bagOfWords, rowsValues = bagOfWordsCreation(corpus)
# splitting data into training and testing data
bagOfWords_train, bagOfWords_test, rowsValues_train, rowsValues_test = train_test_split(bagOfWords, rowsValues,
test_size=0.2, random_state=0)
# #################################################################################################################
#Random Forst Classifer
print('\nRandom Forest')
#saveRandomForestClassiferBeforeAndAfterTunning()
#use saved classifier to predicit training and test sets
accuracyTrain2, accuracyTest2= use_saved_classifierBeforeTunning("randomForest")
#use saved tuned classifier to predicit training and test sets
accuracyTrainTuned2, accuracyTestTuned2 = use_saved_classifierAfterTunning("randomForestTuned")
print('accuracy Train after saving base classifier = ', accuracyTrain2)
print('accuracy Test after saving base classifier = ', accuracyTest2)
print('accuracyTrainTuned after saving tuned classifier = ', accuracyTrainTuned2)
print('accuracyTestTuned after saving tuned classifier = ', accuracyTestTuned2)
print('\n********************************************************\n')
# #################################################################################################################
# #################################################################################################################
#Naive Base Classifer
print('Naive Base')
#accuracyTrain, accuracyTest, classifier_s = classifiers(MultinomialNB())
#save_classifier("naiveBase", classifier_s)
accuracyTrain2, accuracyTest2 = use_saved_classifierBeforeTunning("naiveBase")
print('accuracy Train2 = ', accuracyTrain2)
print('accuracy Test2 = ', accuracyTest2)
print('\n********************************************************\n')
# #################################################################################################################
# #################################################################################################################
#Decision Tree Classifer
print('DecisionTreeClassifier')
#saveDecicsionTreeClassiferBeforeAndAfterTunning()
#use saved before tunning classifier to predicit training and test sets
accuracyTrain2, accuracyTest2 = use_saved_classifierBeforeTunning("decisionTreeClassifier")
#use saved tuned classifier to predicit training and test sets
accuracyTrainTuned2, accuracyTestTuned2 = use_saved_classifierAfterTunning("decisionTreeClassifierTuned")
print('accuracy Train after saving base classifier = ', accuracyTrain2)
print('accuracy Test after saving base classifier = ', accuracyTest2)
print('accuracyTrainTuned after saving tuned classifier = ', accuracyTrainTuned2)
print('accuracyTestTuned after saving tuned classifier = ', accuracyTestTuned2)
print('\n********************************************************\n')
# #################################################################################################################
# #################################################################################################################
#Logistic Regrission Classifier
print('LogisticRegression')
#saveLogisticRegClassiferBeforeAndAfterTunning()
#use saved before tunning classifier to predicit training and test sets
accuracyTrain2, accuracyTest2 = use_saved_classifierBeforeTunning("logisticRegression")
#use saved tuned classifier to predicit training and test sets
accuracyTrainTuned2, accuracyTestTuned2 = use_saved_classifierAfterTunning("LogisticRegressionTuned")
print('accuracy Train after saving base classifier = ', accuracyTrain2)
print('accuracy Test after saving base classifier = ', accuracyTest2)
print('accuracyTrainTuned after saving tuned classifier = ', accuracyTrainTuned2)
print('accuracyTestTuned after saving tuned classifier = ', accuracyTestTuned2)
print('\n********************************************************\n')
# #################################################################################################################
# #################################################################################################################
#KNN Classifer
print('KNN')
#saveKNNClassiferBeforeAndAfterTunning()
#use saved before tunning classifier to predicit training and test sets
accuracyTrain2, accuracyTest2 = use_saved_classifierBeforeTunning("KNN")
#use saved tuned classifier to predicit training and test sets
accuracyTrainTuned2, accuracyTestTuned2 = use_saved_classifierAfterTunning("KNNTuned")
print('accuracy Train after saving base classifier = ', accuracyTrain2)
print('accuracy Test after saving base classifier = ', accuracyTest2)
print('accuracyTrainTuned after saving tuned classifier = ', accuracyTrainTuned2)
print('accuracyTestTuned after saving tuned classifier = ', accuracyTestTuned2)
print('\n********************************************************\n')
# #################################################################################################################