-
Notifications
You must be signed in to change notification settings - Fork 2
/
building_models.py
443 lines (389 loc) · 16.1 KB
/
building_models.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
from sklearn.metrics import (
confusion_matrix,
roc_auc_score,
recall_score,
precision_score,
)
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import make_scorer, accuracy_score, f1_score
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn import linear_model
from sklearn import naive_bayes
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from textblob import Word
from nltk.stem import PorterStemmer
from textblob import TextBlob
from nltk.corpus import stopwords
from pathConfig import PATH_CONFIG # path config file imported
import pandas as pd
import numpy as np
pathData = "data/AnnotatedData3.csv" # ubunutu/linux
# pathData = PATH_CONFIG['pathData'] #windows
def handle_negation(final_df):
out_df = pd.DataFrame()
count_tweet = 0
for text in final_df['text']:
temp_text = ""
li_text = text.split()
for word in li_text:
count = 0
lower_word = word.lower()
if lower_word == "didn't" or lower_word == "not" or lower_word == "no" or lower_word == "never"\
or lower_word == "don't":
temp = count + 1
temp_text = temp_text + word + " "
for i in range(temp, len(li_text)):
if li_text[i] in [",", "?", "!", "."]:
temp_text = " "+temp_text + li_text[i] + " "
break
else:
temp_text = temp_text + "NOT_" + li_text[i]+" "
else:
temp_text = temp_text + word + " "
# print(temp_text)
out_df.at[count_tweet, 'text'] = temp_text
out_df.at[count_tweet, 'class'] = final_df.iloc[count_tweet]['class']
count_tweet += 1
return out_df
def space(final_df):
new_df = pd.DataFrame()
count_tweets = 0
for text in final_df['text']:
temp = ""
for char in text:
if char in [",", ".", "!", "?", ":", ";"]:
temp = temp + ' ' + char
else:
temp = temp + char
# print(temp)
new_df.at[count_tweets, 'text'] = temp
new_df.at[count_tweets, 'class'] = final_df.iloc[count_tweets]['class']
count_tweets += 1
# print("new_df")
# print(new_df)
return new_df
def extract(path):
fd = open(path, encoding="utf-8", errors="replace")
df = pd.read_csv(fd)
defined = df["class"] != ("undefined")
# #output dataframe without undeined
df2 = df[defined]
defined1 = df2["class"] != "Undefined"
df4 = df2[defined1]
# replace no PI with no
df3 = df4.replace("No PI", "no")
# replace PI with yes
final = df3.replace("PI", "yes")
replace_yes = final.replace("Yes", "yes")
final_df = replace_yes.replace("No", "no")
return final_df, df
def report_results(model, X, y):
pred_proba = model.predict_proba(X)[:, 1]
pred = model.predict(X)
auc = roc_auc_score(y, pred_proba)
acc = accuracy_score(y, pred)
f1 = f1_score(y, pred)
prec = precision_score(y, pred)
rec = recall_score(y, pred)
tn, fp, fn, tp = confusion_matrix(y, pred).ravel()
TrueNeg = tn / (tn + fp)
result = {
"auc": auc,
"f1": f1,
"acc": acc,
"precision": prec,
"recall": rec,
"TN": tn,
"FP": fp,
"FN": fn,
"TP": tp,
"True Negative rate": TrueNeg,
}
return result
final_data_frame, data_frame_undefined = extract(pathData)
print(final_data_frame.head())
print()
# ---------------------------------------------------------------------
# SHUFFLING THE DATA FRAME
final_data_frame.reindex(np.random.permutation(final_data_frame.index))
print("shuffled data frame")
print(final_data_frame.head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# LOWERCASE
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join(x.lower() for x in x.split())
)
print("lowercase all text")
print(final_data_frame["text"].head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# NEGATION HANDLING
final_data_frame = space(final_data_frame)
final_data_frame = handle_negation(final_data_frame)
print("handle negation")
print(final_data_frame["text"].head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# REMOVE PUNC
final_data_frame["text"] = final_data_frame["text"].str.replace("[^\w\s]", "")
print("removed punctuation")
print(final_data_frame["text"].head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# STOPWORDS REMOVAL
stop = stopwords.words("english")
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join(x for x in x.split() if x not in stop)
)
print("removed stoped words")
print(final_data_frame["text"].head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# COMMON WORD REMOVAL
freq = pd.Series(
" ".join(final_data_frame["text"]).split()).value_counts()[:2]
print(freq)
freq = list(freq.index)
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join(x for x in x.split() if x not in freq)
)
print("removed comman words")
print(final_data_frame["text"].head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# RARE WORDS REMOVAL
rare = pd.Series(
" ".join(final_data_frame["text"]).split()).value_counts()[-10:]
print(rare)
rare = list(rare.index)
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join(x for x in x.split() if x not in rare)
)
print("removed rare words")
print(final_data_frame["text"].head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# SPELLING CORRECTION
final_data_frame["text"][:5].apply(lambda x: str(TextBlob(x).correct()))
print("fixed spellings")
print(final_data_frame["text"].head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# STEMMING
st = PorterStemmer()
final_data_frame["text"][:5].apply(
lambda x: " ".join([st.stem(word) for word in x.split()])
)
print("applied stemming")
print(final_data_frame["text"].head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# LEMMATIZATION
# Lemmatization is a more effective option than stemming
# because it converts the word into its root word,
# rather than just stripping the suffices.
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join([Word(word).lemmatize() for word in x.split()])
)
print("applied lemmatization")
print(final_data_frame["text"].head())
print()
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# BUILDING THE CORPUS
corpus = []
for text in final_data_frame["text"]:
corpus.append(text)
print("corpus")
# print(corpus)
print()
# ---------------------------------------------------------------------
# -------------------------------------------------------------------------
# CHANGE CLASS VALUES FROM YES/NO TO 0/1
final_data_frame.rename(columns={"class": "class_label"}, inplace=True)
Class_Label = {"yes": 1, "no": 0}
final_data_frame.class_label = [
Class_Label[item] for item in final_data_frame.class_label
]
final_data_frame.rename(columns={"class_label": "class"}, inplace=True)
print("rename values of class column")
print(final_data_frame.head())
print()
# -------------------------------------------------------------------------
# --------------------------------------------------------------------------
# TF
# Transforms text into a sparse matrix of n-gram counts.
count_vectorizer = CountVectorizer()
count_vectorized_data = count_vectorizer.fit_transform(corpus)
# --------------------------------------------------------------------------
# --------------------------------------------------------------------------
# IDF
# Performs the TF-IDF transformation from a provided matrix of counts.
tfidf_vectorizer = TfidfVectorizer()
tfidf_vectorized_data = tfidf_vectorizer.fit_transform(corpus)
# --------------------------------------------------------------------------
# --------------------------------------------------------------------------
# chose document vector
# vectorized_data = count_vectorized_data
vectorized_data = tfidf_vectorized_data
# --------------------------------------------------------------------------
# --------------------------------------------------------------------------
# SPLITING THE DATA
X_train, X_test, Y_train, Y_test = train_test_split(
vectorized_data, final_data_frame["class"], test_size=0.3, random_state=0
)
# --------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Applying SVM
SVM = svm.SVC(probability=True, C=1.0, kernel="linear", degree=3, gamma="auto")
SVM.fit(X_train, Y_train)
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Applying Naive Bayes
Naive = naive_bayes.MultinomialNB()
Naive.fit(X_train, Y_train)
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Applying Logistic Regression
logisticReg = linear_model.LogisticRegression(C=1.0)
logisticReg.fit(X_train, Y_train)
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Applying Decision Tree
dtc = DecisionTreeClassifier(min_samples_split=7, random_state=252)
dtc.fit(X_train, Y_train)
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Applying Neural Network
neural_network = MLPClassifier(
solver="lbfgs", alpha=1e-5, hidden_layer_sizes=(15, 15, 5), random_state=1
)
neural_network.fit(X_train, Y_train)
# -------------------------------------------------------------------------
# --------------------------------------------------------------------------
# statitics
# -------------------------------------------------------------------------
# statitics for SVM
stats = report_results(SVM, X_test, Y_test)
print("-------------------------------------------------------------------------")
print("statitics for SVM")
print(stats)
print("-------------------------------------------------------------------------")
print()
# statitics for NaiveBayes
stats = report_results(Naive, X_test, Y_test)
print("-------------------------------------------------------------------------")
print("statitics for NaiveBayes")
print(stats)
print("-------------------------------------------------------------------------")
print()
# statitics for LogisticRegression
stats = report_results(logisticReg, X_test, Y_test)
print("-------------------------------------------------------------------------")
print("statitics for Logistic Regression")
print(stats)
print("-------------------------------------------------------------------------")
print()
# statitics for DECISION TREE
stats = report_results(dtc, X_test, Y_test)
print("-------------------------------------------------------------------------")
print("statitics for decision tree")
print(stats)
print("-------------------------------------------------------------------------")
print()
# statistics for neural network
stats = report_results(neural_network, X_test, Y_test)
print("-------------------------------------------------------------------------")
print("statitics for Neural Network")
print(stats)
print("-------------------------------------------------------------------------")
print("")
# --------------------------------------------------------------------------
def output_to_results(pathData):
final_data_frame, data_frame_undefined = extract(pathData)
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join(x.lower() for x in x.split())
)
final_data_frame["text"] = final_data_frame["text"].str.replace(
"[^\w\s]", "")
stop = stopwords.words("english")
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join(x for x in x.split() if x not in stop)
)
freq = pd.Series(
" ".join(final_data_frame["text"]).split()).value_counts()[:10]
freq = list(freq.index)
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join(x for x in x.split() if x not in freq)
)
rare = pd.Series(
" ".join(final_data_frame["text"]).split()).value_counts()[-10:]
rare = list(rare.index)
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join(x for x in x.split() if x not in rare)
)
final_data_frame["text"][:5].apply(lambda x: str(TextBlob(x).correct()))
st = PorterStemmer()
final_data_frame["text"][:5].apply(
lambda x: " ".join([st.stem(word) for word in x.split()])
)
final_data_frame["text"] = final_data_frame["text"].apply(
lambda x: " ".join([Word(word).lemmatize() for word in x.split()])
)
corpus = []
for text in final_data_frame["text"]:
corpus.append(text)
final_data_frame.rename(columns={"class": "class_label"}, inplace=True)
Class_Label = {"yes": 1, "no": 0}
final_data_frame.class_label = [
Class_Label[item] for item in final_data_frame.class_label
]
final_data_frame.rename(columns={"class_label": "class"}, inplace=True)
count_vectorizer = CountVectorizer()
count_vectorized_data = count_vectorizer.fit_transform(corpus)
tfidf_vectorizer = TfidfVectorizer()
tfidf_vectorized_data = tfidf_vectorizer.fit_transform(corpus)
vectorized_data = tfidf_vectorized_data
X_train, X_test, Y_train, Y_test = train_test_split(
vectorized_data, final_data_frame["class"], test_size=0.3, random_state=0
)
SVM = svm.SVC(probability=True, C=1.0,
kernel="linear", degree=3, gamma="auto")
SVM.fit(X_train, Y_train)
Naive = naive_bayes.MultinomialNB()
Naive.fit(X_train, Y_train)
logisticReg = linear_model.LogisticRegression(C=1.0)
logisticReg.fit(X_train, Y_train)
dtc = DecisionTreeClassifier(min_samples_split=7, random_state=252)
dtc.fit(X_train, Y_train)
neural_network = MLPClassifier(
solver="lbfgs", alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1
)
neural_network.fit(X_train, Y_train)
stats_SVM = report_results(SVM, X_test, Y_test)
stats_Naive = report_results(Naive, X_test, Y_test)
stats_logistic = report_results(logisticReg, X_test, Y_test)
stats_dtc = report_results(dtc, X_test, Y_test)
stats_neural = report_results(neural_network, X_test, Y_test)
stats = []
stats.append(stats_SVM)
stats.append(stats_Naive)
stats.append(stats_logistic)
stats.append(stats_dtc)
stats.append(stats_neural)
return stats
# output_to_results("data/AnnotatedData3.csv")