-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryClassifier.py
340 lines (288 loc) · 12.8 KB
/
BinaryClassifier.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
"""
Filename: BinaryClassifier.py
Author: Akash Desai, Vaibhav Joshi
Description: Implementation of binary classifier using various data mining techniques
"""
import praw
import math
import numpy as np
import pandas as pd
import sklearn
import seaborn as sn
import sklearn.utils as util
import matplotlib.pyplot as plt
from sklearn import metrics
from sklearn.metrics import roc_auc_score
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.model_selection import learning_curve
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
def reddit_test(API_client, API_secret, user_agent, user, passwd):
"""
This method is used to fetch data from the subreddit and perform various
data mining methods on the data.
:param API_client: API_client
:param API_secret: API_secrect
:param user_agent: User Agent
:param user: Username of application
:param passwd: Password of Application
:return:None
"""
reddit = praw.Reddit(client_id=API_client,
client_secret=API_secret,
user_agent=user_agent,
username=user,
password=passwd)
#Make authorized instances of reddit
sub1 = reddit.subreddit('soccer').top(limit=900)
sub2 = reddit.subreddit('britishproblems').top(limit=900)
fields = ["title"] #Fields to be used for data mining methods
all_posts = [] #Stores the list of posts
#Create dataframe and assign value to the fields accordingly
for post in sub1:
to_dict = vars(post)
sub_dict = {field: to_dict[field] for field in fields}
sub_dict["target"] = 0
all_posts.append(sub_dict)
for post in sub2:
to_dict = vars(post)
sub_dict = {field: to_dict[field] for field in fields}
sub_dict["target"] = 1
all_posts.append(sub_dict)
dataframe = pd.DataFrame(all_posts)
cols = list(dataframe.columns)
cols[cols.index('target')], cols[-1] = cols[-1], cols[cols.index('target')]
dataframe = dataframe[cols]
#Creates an instance of TF-IDF vector for feature extraction. Stop words will be ignored.
tfidf_transformer = TfidfVectorizer(stop_words=sklearn.feature_extraction.text.ENGLISH_STOP_WORDS)
#Creates a feature vector for post's title
X_train_title_counts = pd.DataFrame((tfidf_transformer.fit_transform(dataframe["title"].values)).todense())
Y = pd.DataFrame(dataframe["target"].values)
#Splits the data into training set, development set and test set
train_X, train_Y, test_X, test_Y,dev_X,dev_Y = get_training_and_testing_sets(X_train_title_counts, Y)
ROC_List = {}
Accuracy = {}
#Perform various data mining methods on the data
Train_SVC(train_X, train_Y, test_X, test_Y, ROC_List, Accuracy)
Train_NaiveBayes(train_X, train_Y, test_X, test_Y, ROC_List, Accuracy)
Train_Logistic(train_X, train_Y, test_X, test_Y, ROC_List, Accuracy)
Train_RandomForest(train_X, train_Y, test_X, test_Y, ROC_List, Accuracy)
#Plots ROC curve
plot_roc_curve_and_accuracy(ROC_List, Accuracy)
def Train_RandomForest(train_X, train_Y, test_X, test_Y, ROC_List, Accuracy):
"""
Implementation of Random Forest
:param train_X: Training Set Features
:param train_Y: Training Set Label
:param test_X: Test Set Feature
:param test_Y: Test Set Lable
:param ROC_List: List which stores parameters for ROC curve
:param Accuracy: List which stores accuracy
:return: None
"""
rdf = RandomForestClassifier(n_estimators=100)
rdf.fit(train_X, train_Y)
Y_pred = rdf.predict(test_X)
Accuracy["Random Forest"] = metrics.accuracy_score(test_Y, Y_pred)
print("Accuracy of Random Forest: %.2f%%" % (Accuracy["Random Forest"] * 100))
y_pred_proba = rdf.predict_proba(test_X)[:, 1]
fpr, tpr, _ = metrics.roc_curve(test_Y, y_pred_proba)
ROC_List["Random Forest"] = [fpr, tpr]
#Confusion Matrix
cm_rf = confusion_matrix(test_Y, Y_pred)
rf_cm = pd.DataFrame(cm_rf, range(2), range(2))
sn.set(font_scale=1.7)
sn.heatmap(rf_cm, annot=True, annot_kws={"size": 15}, fmt='g', cmap="Greens")
auc = metrics.roc_auc_score(test_Y, y_pred_proba)
plt.plot(fpr, tpr, label="Random Forest AUC=" + str(auc))
plt.legend(loc=4)
plt.title("Random Forest")
plt.show()
def Train_SVC(train_X, train_Y, test_X, test_Y, ROC_List, Accuracy):
"""
Implementation of Support Vector Classifier
:param train_X: Training Set Features
:param train_Y: Training Set Label
:param test_X: Test Set Feature
:param test_Y: Test Set Lable
:param ROC_List: List which stores parameters for ROC curve
:param Accuracy: List which stores accuracy
:return:None
"""
clf = SVC(probability=True,C=5, gamma='auto', kernel='linear')
clf.fit(train_X, train_Y)
y_predict = clf.predict(test_X)
Accuracy["SVM"] = metrics.accuracy_score(test_Y, y_predict)
print("Accuracy of SVM: %.2f%%" % (Accuracy["SVM"] * 100))
y_pred_proba = clf.predict_proba(test_X)[:, 1]
fpr, tpr, _ = metrics.roc_curve(test_Y, y_pred_proba)
ROC_List["SVM"] = [fpr, tpr]
#Confusion Matrix
cm_rf = confusion_matrix(test_Y, y_predict)
rf_cm = pd.DataFrame(cm_rf, range(2), range(2))
sn.set(font_scale=1.7)
sn.heatmap(rf_cm, annot=True, annot_kws={"size": 15}, fmt='g', cmap="Greens")
auc = metrics.roc_auc_score(test_Y, y_pred_proba)
plt.plot(fpr, tpr, label="SVC AUC=" + str(auc))
plt.legend(loc=4)
plt.title("SVC")
plt.show()
def Train_Logistic(train_X, train_Y, test_X, test_Y, ROC_List, Accuracy):
"""
Implementation of Logistic Regression
:param train_X: Training Set Features
:param train_Y: Training Set Label
:param test_X: Test Set Feature
:param test_Y: Test Set Lable
:param ROC_List: List which stores parameters for ROC curve
:param Accuracy: List which stores accuracy
:return:None
"""
logistic = LogisticRegression()
logistic.fit(train_X, train_Y)
y_predict=logistic.predict(test_X)
Accuracy["Logistic"] = logistic.score(test_X, test_Y)
print("Accuracy of Logistic Regression: %.2f%%" % (Accuracy["Logistic"] * 100))
y_pred_proba = logistic.predict_proba(test_X)[:, 1]
fpr, tpr, _ = metrics.roc_curve(test_Y, y_pred_proba)
ROC_List["Logistic Regression"] = [fpr, tpr]
# Confusion Metrix
cm_rf = confusion_matrix(test_Y, y_predict)
rf_cm = pd.DataFrame(cm_rf, range(2), range(2))
sn.set(font_scale=1.7)
sn.heatmap(rf_cm, annot=True, annot_kws={"size": 15}, fmt='g', cmap="Greens")
"""
auc = metrics.roc_auc_score(test_Y, y_pred_proba)
plt.plot(fpr, tpr, label="Logistic AUC=" + str(auc))
plt.legend(loc=4)
plt.title("Logistic Regression")
plt.show()
"""
def Train_NaiveBayes(train_X, train_Y, test_X, test_Y, ROC_List, Accuracy):
"""
Implementation of Naive Bayes
:param train_X: Training Set Features
:param train_Y: Training Set Label
:param test_X: Test Set Feature
:param test_Y: Test Set Lable
:param ROC_List: List which stores parameters for ROC curve
:param Accuracy: List which stores accuracy
:return:None
"""
nb = MultinomialNB().fit(train_X, train_Y)
y_predict = nb.predict(test_X)
Accuracy["Naive Bayes"] = metrics.accuracy_score(test_Y, y_predict)
print("Accuracy of Naive Bayes: %.2f%%" % (Accuracy["Naive Bayes"] * 100))
y_pred_proba = nb.predict_proba(test_X)[:, 1]
fpr, tpr, _ = metrics.roc_curve(test_Y, y_pred_proba)
ROC_List["Naive Bayes"] = [fpr, tpr]
# Confusion Metrix
cm_rf = confusion_matrix(test_Y, y_predict)
rf_cm = pd.DataFrame(cm_rf, range(2), range(2))
sn.set(font_scale=1.7)
sn.heatmap(rf_cm, annot=True, annot_kws={"size": 15}, fmt='g', cmap="Greens")
auc = metrics.roc_auc_score(test_Y, y_pred_proba)
plt.plot(fpr, tpr, label="NB AUC=" + str(auc))
plt.legend(loc=4)
plt.title("Naive Bayes")
plt.show()
def get_training_and_testing_sets(data, Y):
data = pd.concat([data, Y], axis=1)
training_split = 0.5
development_split = 0.25
test_split = 0.5
split_index_1 = math.floor(len(data) * training_split)
split_index_2 = math.floor(len(data) * test_split)
sub1 = data.iloc[:split_index_1]
sub2 = data.iloc[split_index_1: split_index_2 + split_index_1]
training_1 = sub1.head(len(sub1) // 2)
training_2 = sub2.head(len(sub2) // 2)
development_split1 = data.iloc[split_index_1 // 2: split_index_1 // 2 + split_index_1 // 4]
development_split2 = data.iloc[
split_index_2 + split_index_2 // 2: split_index_2 + split_index_2 // 2 + split_index_2 // 4]
development_split_final = development_split1.append(development_split2, ignore_index=True)
test1 = sub1.tail(len(sub1) // 4)
test2 = sub2.tail(len(sub2) // 4)
training_final = training_1.append(training_2, ignore_index=True)
test_final = test1.append(test2, ignore_index=True)
training_final = util.shuffle(training_final)
training_final = training_final.reset_index(drop=True)
development_split_final = util.shuffle(development_split_final)
development_split_final = development_split_final.reset_index(drop=True)
test_final = util.shuffle(test_final)
test_final = test_final.reset_index(drop=True)
x, y = training_final.shape
train_X = training_final.iloc[:, 0:y - 1]
train_Y = training_final.iloc[:, y - 1]
x, y = test_final.shape
test_X = test_final.iloc[:, 0:y - 1]
test_Y = test_final.iloc[:, y - 1]
x, y = development_split_final.shape
dev_X = development_split_final.iloc[:, 0:y - 1]
dev_Y = development_split_final.iloc[:, y - 1]
return train_X, train_Y, test_X, test_Y, dev_X, dev_Y
def plot_roc_curve_and_accuracy(fpr_tpr_all, Accuracy):
algo_list = []
acc_list = []
for x in Accuracy.keys():
acc_list.append(Accuracy[x])
algo_list.append(x)
y_pos = np.arange(len(acc_list))
plt.bar(y_pos, acc_list, align='center', alpha=0.5)
plt.xticks(y_pos, algo_list)
plt.title('Accuracy achieved - r/soccer and r/britishproblems')
plt.show()
plt.close()
keys = []
for x in fpr_tpr_all.keys():
keys.append(x)
i = 0
for x in fpr_tpr_all.values():
plt.plot(x[0], x[1], lw=1, label=keys[i])
plt.plot([0, 1], [0, 1], color='darkblue', linestyle='--')
plt.legend(loc="lower right")
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
i += 1
plt.show()
def curve(train_X, train_Y):
# rdf= RandomForestClassifier(n_estimators=100).fit(train_X,train_Y)
logreg = LogisticRegression().fit(train_X, train_Y)
print(logreg)
svm = SVC(gamma=0.001, kernel='linear', probability=True).fit(train_X, train_Y)
train_sizes, train_scores, test_scores = learning_curve(svm,
train_X,
train_Y,
# Number of folds in cross-validation
cv=5,
# Evaluation metric
scoring='accuracy',
# Use all computer cores
n_jobs=-1,
train_sizes=[100, 200, 500, 700])
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)
# Create means and standard deviations of test set scores
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)
# Draw lines
plt.plot(train_sizes, train_mean, '--', color="#111111", label="Training score")
plt.plot(train_sizes, test_mean, color="#111111", label="Cross-validation score")
# Draw bands
# Create plot
plt.title("Learning Curve")
plt.xlabel("Training Set Size"), plt.ylabel("Accuracy Score"), plt.legend(loc="best")
plt.tight_layout()
plt.show()
def main():
API_client = 'JmV8nK1GpgmTdA'
API_secret = 'Ka8kIzZZKiX2HBA_bm9HR4aY_-k'
user_agent = 'FIS Project'
user = 'vj_34'
passwd = 'Myreddit123'
reddit_test(API_client, API_secret, user_agent, user, passwd)
if __name__ == '__main__':
main()