forked from rpaudel42/TwitterTrendSpam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
215 lines (183 loc) · 9.37 KB
/
classifier.py
File metadata and controls
215 lines (183 loc) · 9.37 KB
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
# ******************************************************************************
# classifier.py
#
# Date Name Description
# ======== ========= ========================================================
# 2/25/19 Paudel Initial version,
# ******************************************************************************
import numpy as np
from datetime import datetime
from sklearn import metrics
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score, roc_auc_score, roc_curve, auc, mean_squared_error, r2_score
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
from sklearn.model_selection import cross_val_predict
import itertools
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn import preprocessing
from sklearn.utils import resample
from imblearn.metrics import geometric_mean_score
class Classifier():
K_FOLD = 5
ALGORITHMS = {
"svm": "svm",
"random_forest": "random_forest"
}
def read_dataset_bene(self, feature_file):
data_file = pd.read_csv(feature_file)
encoded_data = data_file.apply(LabelEncoder().fit_transform)
input_cols = ['n_spam_word', 'hashtag_per_word', 'url_per_word', 'num_word', 'num_char', 'num_digit',
'num_url', 'num_hashtag', 'num_user_mention', 'num_rt', 'is_reply']
return encoded_data[input_cols], encoded_data['is_spam']
def read_dataset_chen(self, feature_file):
data_file = pd.read_csv(feature_file)
encoded_data = data_file.apply(LabelEncoder().fit_transform)
input_cols = ['account_age', 'no_of_follower', 'no_of_following', 'no_user_fav', 'no_list', 'no_tweet',
'num_char', 'num_digit', 'num_url', 'num_hashtag', 'num_user_mention', 'num_rt']
return encoded_data[input_cols], encoded_data['is_spam']
def read_dataset_bene_upsample(self, feature_file, sample_size):
data_file = pd.read_csv(feature_file)
encoded_data = data_file.apply(LabelEncoder().fit_transform)
input_cols = ['n_spam_word', 'hashtag_per_word', 'url_per_word', 'num_word', 'num_char', 'num_digit',
'num_url', 'num_hashtag', 'num_user_mention', 'num_rt', 'is_reply', 'is_spam']
#print encoded_data['mci']
# partition data based on class
encoded_data = encoded_data[input_cols]
normal = encoded_data[encoded_data['is_spam'] == False]
spam = encoded_data[encoded_data['is_spam'] == True]
# down sample the majority class (here no-apply class) to match minority class
minority_upsample = resample(spam,
replace=True, # sample without replacement
n_samples=sample_size, # to match minority class
random_state=123) # reproducible results
# concat minority class as well downsampled majority class, this will be the balanced dataset
upsampled = pd.concat([minority_upsample, normal])
y = upsampled['is_spam']
X = upsampled.drop('is_spam', axis=1)
return X, y
#return encoded_data[input_cols], encoded_data['is_spam']
def read_dataset_chen_upsample(self, feature_file, sample_size):
data_file = pd.read_csv(feature_file)
encoded_data = data_file.apply(LabelEncoder().fit_transform)
input_cols = ['account_age', 'no_of_follower', 'no_of_following', 'no_user_fav', 'no_list', 'no_tweet', 'num_char', 'num_digit', 'num_url', 'num_hashtag', 'num_user_mention', 'num_rt', 'is_spam']
#return encoded_data[input_cols], encoded_data['is_spam']
encoded_data = encoded_data[input_cols]
normal = encoded_data[encoded_data['is_spam'] == False]
spam = encoded_data[encoded_data['is_spam'] == True]
# down sample the majority class (here no-apply class) to match minority class
minority_upsample = resample(spam,
replace=False, # sample without replacement
n_samples=sample_size, # to match minority class
random_state=123) # reproducible results
# concat minority class as well downsampled majority class, this will be the balanced dataset
upsampled = pd.concat([minority_upsample, normal])
y = upsampled['is_spam']
X = upsampled.drop('is_spam', axis=1)
return X, y
def __init__(self, feature_file, flag, sample_size):
if flag == 'B':
if sample_size > 0:
self.X, self.Y = self.read_dataset_bene_upsample(feature_file, sample_size)
else:
self.X, self.Y = self.read_dataset_bene(feature_file)
elif flag == 'C':
if sample_size > 0:
self.X, self.Y = self.read_dataset_chen_upsample(feature_file, sample_size)
else:
self.X, self.Y = self.read_dataset_chen(feature_file)
def check_data_distribution(self):
print("\n------- Mean ------")
print(self.data.mean(axis=0))
print("\n\n------- Standard Deviation -------")
print(self.data.std(axis=0))
dt = preprocessing.scale(self.data)
def svm_classify(self):
"""
Uses Support Vector Machine Classifier
:return: None
"""
svm_classifier = svm.SVC(kernel="rbf", gamma='auto')
before = datetime.now()
svm_output = cross_val_predict(svm_classifier, self.X, self.Y, cv=self.K_FOLD)
after = datetime.now()
runtime = (after - before).total_seconds()
#print("Time: ", runtime)
#print("Support Vector Machine Classifier Report")
#print("===========================================\n")
return self.display_report(svm_output, self.ALGORITHMS["svm"])
def random_forest(self):
random_forest_classifier = RandomForestClassifier(n_estimators=100)
before = datetime.now()
classification = cross_val_predict(random_forest_classifier, self.X, self.Y, cv=self.K_FOLD)
after = datetime.now()
runtime = (after - before).total_seconds()
#print("Time: ", runtime)
#print("Random Forest Classifier Report")
#print("===========================================\n")
return self.display_report(classification, self.ALGORITHMS["random_forest"])
def display_report(self, prediction, algorithm):
"""
Displays the classification report
:param prediction: Prediction of the model
:param algorithm: Algorithm used to generate predictions
:return: None
"""
result = {}
#print("Confusion Matrix: ")
#print("------------------\n")
#self.plot_confusion_matrix(confusion_matrix(self.Y, prediction), algorithm)
# print ("\nClassification Report: ")
# print("-----------------------")
# print("Accuracy: ", metrics.accuracy_score(self.Y, prediction))
# print("Precision: ", metrics.precision_score(self.Y, prediction))
# print("Recall: ", metrics.recall_score(self.Y, prediction))
# print("F1-Score: ", metrics.f1_score(self.Y, prediction))
# #print("G-Mean: ", metrics.)
# print("area under curve (auc): ", metrics.roc_auc_score(self.Y, prediction))
# print(classification_report(self.Y, prediction))
# false_positive_rate, true_positive_rate, thresholds = roc_curve(self.Y, prediction)
# print("(auc): ", auc(false_positive_rate, true_positive_rate))
result['acc'] = metrics.accuracy_score(self.Y, prediction)
result['p'] = metrics.precision_score(self.Y, prediction)
result['r'] = metrics.recall_score(self.Y, prediction)
result['f1'] = metrics.f1_score(self.Y, prediction)
result['g'] = geometric_mean_score(self.Y, prediction)
return result
def plot_confusion_matrix(self, con_mat, algorithm,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
:param con_mat: confusion matrix
:param algorithm: algorithm used
:param normalize: boolean to normalize or not
:param title: title of the graph
:param cmap: map color palate
:return: None
"""
plt.figure()
plt.imshow(con_mat, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(2)
plt.xticks(tick_marks, ["normal", "spam"], rotation=45)
plt.yticks(tick_marks, ["normal", "spam"])
if normalize:
con_mat = con_mat.astype('float') / con_mat.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(con_mat)
thresh = con_mat.max() / 2.
for i, j in itertools.product(range(con_mat.shape[0]), range(con_mat.shape[1])):
plt.text(j, i, con_mat[i, j],
horizontalalignment="center",
color="white" if con_mat[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.savefig("figures/" + algorithm + ".png")