-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
264 lines (176 loc) · 9.44 KB
/
main.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
import pandas as pd
import numpy as np
# used for confusion matrix metric
from sklearn.metrics import confusion_matrix
# used for building and displaying the confusion matrix
import matplotlib.pyplot as plt
# used for displaying the heatmap properties for the confusion matrix
import seaborn as sns
class NaiveBayesEmailClassifier:
def __init__(self, trainingData):
self.trainingDataSet = trainingData
# training spambase class priors
self.trainingSpamClassPrior = self.determineClassPrior(self.trainingDataSet, 1)
self.trainingHamClassPrior = self.determineClassPrior(self.trainingDataSet, 0)
# defaults for each spam and ham mean and std
self.spamsMean = []
self.spamsStd = []
self.hamsMean = []
self.hamsStd = []
self.determineClassFeatureMeansStds(trainingData)
# performance metrics:
self.totalEmailsClassified = 0
self.truePositives = 0
self.trueNegatives = 0
self.falsePositives = 0
self.falseNegatives = 0
self.confusionMatrix = None
# supply a 0 for ham and 1 for spam
def determineClassPrior(self, dataSet, targetClass):
return ((dataSet.iloc[:, -1] == targetClass).sum()) / len(dataSet)
def determineClassFeatureMeansStds(self, trainingData):
# calculates the mean for each feature within the training data
foundSpams = trainingData[trainingData.iloc[:, -1] == 1]
foundHam = trainingData[trainingData.iloc[:, -1] == 0]
# pull the std and mean for each class respectively.
foundSpamsMean = foundSpams.iloc[:, :-1].mean(axis=0)
foundSpamsStd = foundSpams.iloc[:, :-1].std(axis=0)
foundHamsMean = foundHam.iloc[:, :-1].mean(axis=0)
foundHamsStd = foundHam.iloc[:, :-1].std(axis=0)
# replace 0 with a small number for underflow and prevent division by zero
# inplace=True swaps the current df without need for reassignment of the df
foundSpamsStd.replace(0.0, 0.0001, inplace=True)
foundHamsStd.replace(0.0, 0.0001, inplace=True)
self.spamsMean = np.array(foundSpamsMean.to_list())
self.spamsStd = np.array(foundSpamsStd.to_list())
self.hamsMean = np.array(foundHamsMean.to_list())
self.hamsStd = np.array(foundHamsStd.to_list())
def classifyEmails(self, emailFeatures, emailTargets):
predictedValues = []
for email in range(len(emailFeatures)):
self.totalEmailsClassified += 1
emailFeatureVector = emailFeatures[email]
targetClass = emailTargets[email]
# adds a posteriors list
posteriors = []
# classify ham
hamPosterior = np.sum(np.log(self.gaussianNB(emailFeatureVector, self.hamsMean, self.hamsStd)) + np.log(self.trainingHamClassPrior))
posteriors.append(hamPosterior)
# classify spam
spamPosterior = np.sum(np.log(self.gaussianNB(emailFeatureVector, self.spamsMean, self.spamsStd)) + np.log(self.trainingSpamClassPrior))
posteriors.append(spamPosterior)
prediction = np.argmax(posteriors, axis=0)
predictedValues.append(prediction)
if prediction == targetClass:
# classify prediction for accuracy metrics:
# case where the target is classified as spam
# case where the email is correctly classified as spam
if prediction == 1 and targetClass == 1:
self.truePositives += 1
# case where classified as not spam and is non-spam
if prediction == 0 and targetClass == 0:
self.trueNegatives += 1
# case where it wasn't correctly predicted.
else:
# case where the email is falsely classified as spam
if prediction == 1 and targetClass == 0:
self.falsePositives += 1
# case where classified as non spam but is spam email
if prediction == 0 and targetClass == 1:
self.falseNegatives += 1
self.confusionMatrix = confusion_matrix(emailTargets, predictedValues)
def coefficientEFraction(self, classStd):
return 1 / np.sqrt((2 * np.pi) * classStd )
def exponentialFractionTerm(self, featureVector, classMean, classStd):
eps = 1e-4
return (((featureVector - classMean) ** 2) / (2 * (classStd ** 2) + eps))
def gaussianNB(self, emailFeatureVector, classMean, classStd):
result = self.coefficientEFraction(classStd) * (np.exp(-1 * self.exponentialFractionTerm(emailFeatureVector, classMean, classStd)))
return result
def determineAccuracy(self):
print("Accuracy Results:")
print("====================================")
print("Out of " + str(self.totalEmailsClassified) + " emails:")
accuracy = (self.truePositives + self.trueNegatives) / (self.truePositives + self.falsePositives + self.trueNegatives + self.falseNegatives) * 100
print( "Accuracy of : " + str(round(accuracy,2)) + "%")
print("====================================")
def determinePrecision(self):
print("Precision Results:")
print("====================================")
print("Out of " + str(self.totalEmailsClassified) + " emails:")
precision = (self.truePositives) / (self.truePositives + self.falsePositives) * 100
print("Precison of : " + str(round(precision, 2)))
print("====================================")
def determineRecall(self):
print("Recall Results:")
print("====================================")
print("Out of " + str(self.totalEmailsClassified) + " emails:")
recall = (self.truePositives) / (self.truePositives + self.falseNegatives) * 100
print("Recall of : " + str(round(recall, 2)))
print("====================================")
def displayConfusionMatrix(self):
if len(self.confusionMatrix) > 0:
labels = ['Not Spam', 'Spam']
sns.heatmap(self.confusionMatrix, annot=True, fmt='d', cmap='Greens', xticklabels=labels, yticklabels=labels)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
else:
return "run a classification in order to display a confusion matrix"
# build 2300 instances of spam and ham
# split 40% spam 60% ham
def buildTrainingTestData(spamSplit, hamSplit):
spambase = pd.read_csv("spambase.data", header=None)
# get the number of spams and hams required
spamPulls = int(2300 * spamSplit)
hamPulls = int(2300 * hamSplit)
# shuffle original data
spambase = spambase.sample(frac=1)
# Training Data Preparations:
# =======================================================
# pull only the desired number of spams
trainingSpams = spambase[spambase.iloc[:, -1] == 1][:spamPulls]
trainingHams = spambase[spambase.iloc[:, -1] == 0][:hamPulls]
# removes data so test can't pull it into its own DF
# this is done with a boolean mask to remove the corresponding index values from the original df
spambase = spambase[~spambase.index.isin(trainingSpams.index)]
spambase = spambase[~spambase.index.isin(trainingHams.index)]
training_spambase = pd.concat([trainingHams, trainingSpams])
# Testing Data
# ==============================================================
# pull only the desired number of spams
testingSpams = spambase[spambase.iloc[:, -1] == 1][:spamPulls]
testingHams = spambase[spambase.iloc[:, -1] == 0][:hamPulls]
# removes data so test can't pull it into its own DF
# ~ negates the boolean mask to select the rows that are not present in spambase and filter them
spambase = spambase[~spambase.index.isin(testingSpams.index)]
spambase = spambase[~spambase.index.isin(testingHams.index)]
testing_spambase = pd.concat([testingHams, testingSpams])
# return the data from split
return training_spambase, testing_spambase
def prepareTestData(testingData):
# shuffle the test data.
testingData = testingData.sample(frac=1)
# split the testing features from the label.
inputFeatures = np.array(testingData.iloc[:, :-1])
# pull classifications and for comparison.
inputTargets = np.array(testingData.iloc[:, -1].to_list())
return inputFeatures, inputTargets
def main():
trainingSpambaseData, testingSpambaseData = buildTrainingTestData(.40,.60)
emailClassifier = NaiveBayesEmailClassifier(trainingSpambaseData)
# split target and features for testing.
inputFeature, inputTargets = prepareTestData(testingSpambaseData)
emailClassifier.classifyEmails(inputFeature, inputTargets)
emailClassifier.determineAccuracy()
emailClassifier.determinePrecision()
emailClassifier.determineRecall()
emailClassifier.displayConfusionMatrix()
# used for visual correlation inspection of the data.
# determine correlation for the features:
correlationMatrix = testingSpambaseData.corr()
# display.max_columns lets all of the 57 features be displayed.
pd.set_option('display.max_columns', None)
print(correlationMatrix.head())
main()