-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_spam.py
More file actions
242 lines (201 loc) · 9.25 KB
/
detect_spam.py
File metadata and controls
242 lines (201 loc) · 9.25 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
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
# -*- coding: utf-8 -*-
"""detect_spam.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19fPMbuVSmJ187L9AwScS7oPU6zcxl1J5
# Detecting spam
Email spam, also known as junk email, is unsolicited bulk messages sent through email. The use of spam has been growing in popularity since the early 1990s and is a problem faced by most email users. Recipients of spam often have had their email addresses obtained by spambots, which are automated programs that crawl the internet looking for email addresses. Spammers use spambots to create email distribution lists. A spammer typically sends an email to millions of email addresses, with the expectation that only a small number will respond or interact with the message.
In Machine Learning, there are many ways to detect spam such: decision-based systems, Bayesian classifiers, support vector machine, neural networks and sample-based methods.
In this notebook, I will build a spam classifier program in python which can tell whether a given message is spam or not. We can do this by using a simple, yet powerful theorem from probability theory called Baye’s Theorem.
Installing and importing required libraries:
"""
!pip install wordcloud
!pip install nltk
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from math import log, sqrt
import pandas as pd
import numpy as np
import re
# %matplotlib inline
"""# Loading the data"""
mails = pd.read_csv('spam.csv', encoding = 'latin-1')
mails.head()
mails.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis = 1, inplace = True)
mails.head()
mails.rename(columns = {'v1': 'labels', 'v2': 'message'}, inplace = True)
mails.head()
mails['labels'].value_counts()
mails['label'] = mails['labels'].map({'ham': 0, 'spam': 1})
mails.head()
mails.drop(['labels'], axis = 1, inplace = True)
mails.head()
totalMails = 4825 + 747
trainIndex, testIndex = list(), list()
for i in range(mails.shape[0]):
if np.random.uniform(0, 1) < 0.75:
trainIndex += [i]
else:
testIndex += [i]
trainData = mails.loc[trainIndex]
testData = mails.loc[testIndex]
trainData['label'].value_counts()
spam_words = ' '.join(list(mails[mails['label'] == 1]['message']))
spam_wc = WordCloud(width = 512,height = 512).generate(spam_words)
plt.figure(figsize = (10, 8), facecolor = 'k')
plt.imshow(spam_wc)
plt.axis('off')
plt.tight_layout(pad = 0)
plt.show()
ham_words = ' '.join(list(mails[mails['label'] == 0]['message']))
ham_wc = WordCloud(width = 512,height = 512).generate(ham_words)
plt.figure(figsize = (10, 8), facecolor = 'k')
plt.imshow(ham_wc)
plt.axis('off')
plt.tight_layout(pad = 0)
plt.show()
trainData.head()
trainData['label'].value_counts()
testData.head()
testData['label'].value_counts()
def process_message(message, lower_case = True, stem = True, stop_words = True, gram = 2):
if lower_case:
message = message.lower()
words = word_tokenize(message)
words = [w for w in words if len(w) > 2]
if gram > 1:
w = []
for i in range(len(words) - gram + 1):
w += [' '.join(words[i:i + gram])]
return w
if stop_words:
sw = stopwords.words('english')
words = [word for word in words if word not in sw]
if stem:
stemmer = PorterStemmer()
words = [stemmer.stem(word) for word in words]
return words
class SpamClassifier(object):
def __init__(self, trainData, method = 'tf-idf'):
self.mails, self.labels = trainData['message'], trainData['label']
self.method = method
def train(self):
self.calc_TF_and_IDF()
if self.method == 'tf-idf':
self.calc_TF_IDF()
else:
self.calc_prob()
def calc_prob(self):
self.prob_spam = dict()
self.prob_ham = dict()
for word in self.tf_spam:
self.prob_spam[word] = (self.tf_spam[word] + 1) / (self.spam_words + \
len(list(self.tf_spam.keys())))
for word in self.tf_ham:
self.prob_ham[word] = (self.tf_ham[word] + 1) / (self.ham_words + \
len(list(self.tf_ham.keys())))
self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails
def calc_TF_and_IDF(self):
noOfMessages = self.mails.shape[0]
self.spam_mails, self.ham_mails = self.labels.value_counts()[1], self.labels.value_counts()[0]
self.total_mails = self.spam_mails + self.ham_mails
self.spam_words = 0
self.ham_words = 0
self.tf_spam = dict()
self.tf_ham = dict()
self.idf_spam = dict()
self.idf_ham = dict()
for i in range(noOfMessages):
message_processed = process_message(self.mails[i])
count = list() #To keep track of whether the word has ocured in the message or not.
#For IDF
for word in message_processed:
if self.labels[i]:
self.tf_spam[word] = self.tf_spam.get(word, 0) + 1
self.spam_words += 1
else:
self.tf_ham[word] = self.tf_ham.get(word, 0) + 1
self.ham_words += 1
if word not in count:
count += [word]
for word in count:
if self.labels[i]:
self.idf_spam[word] = self.idf_spam.get(word, 0) + 1
else:
self.idf_ham[word] = self.idf_ham.get(word, 0) + 1
def calc_TF_IDF(self):
self.prob_spam = dict()
self.prob_ham = dict()
self.sum_tf_idf_spam = 0
self.sum_tf_idf_ham = 0
for word in self.tf_spam:
self.prob_spam[word] = (self.tf_spam[word]) * log((self.spam_mails + self.ham_mails) \
/ (self.idf_spam[word] + self.idf_ham.get(word, 0)))
self.sum_tf_idf_spam += self.prob_spam[word]
for word in self.tf_spam:
self.prob_spam[word] = (self.prob_spam[word] + 1) / (self.sum_tf_idf_spam + len(list(self.prob_spam.keys())))
for word in self.tf_ham:
self.prob_ham[word] = (self.tf_ham[word]) * log((self.spam_mails + self.ham_mails) \
/ (self.idf_spam.get(word, 0) + self.idf_ham[word]))
self.sum_tf_idf_ham += self.prob_ham[word]
for word in self.tf_ham:
self.prob_ham[word] = (self.prob_ham[word] + 1) / (self.sum_tf_idf_ham + len(list(self.prob_ham.keys())))
self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails
def classify(self, processed_message):
pSpam, pHam = 0, 0
for word in processed_message:
if word in self.prob_spam:
pSpam += log(self.prob_spam[word])
else:
if self.method == 'tf-idf':
pSpam -= log(self.sum_tf_idf_spam + len(list(self.prob_spam.keys())))
else:
pSpam -= log(self.spam_words + len(list(self.prob_spam.keys())))
if word in self.prob_ham:
pHam += log(self.prob_ham[word])
else:
if self.method == 'tf-idf':
pHam -= log(self.sum_tf_idf_ham + len(list(self.prob_ham.keys())))
else:
pHam -= log(self.ham_words + len(list(self.prob_ham.keys())))
pSpam += log(self.prob_spam_mail)
pHam += log(self.prob_ham_mail)
return pSpam >= pHam
def predict(self, testData):
result = dict()
for (i, message) in enumerate(testData):
processed_message = process_message(message)
result[i] = int(self.classify(processed_message))
return result
def metrics(labels, predictions):
true_pos, true_neg, false_pos, false_neg = 0, 0, 0, 0
for i in range(len(labels)):
true_pos += int(labels[i] == 1 and predictions[i] == 1)
true_neg += int(labels[i] == 0 and predictions[i] == 0)
false_pos += int(labels[i] == 0 and predictions[i] == 1)
false_neg += int(labels[i] == 1 and predictions[i] == 0)
precision = true_pos / (true_pos + false_pos)
recall = true_pos / (true_pos + false_neg)
Fscore = 2 * precision * recall / (precision + recall)
accuracy = (true_pos + true_neg) / (true_pos + true_neg + false_pos + false_neg)
print("Precision: ", precision)
print("Recall: ", recall)
print("F-score: ", Fscore)
print("Accuracy: ", accuracy)
sc_tf_idf = SpamClassifier(trainData, 'tf-idf')
sc_tf_idf.train()
preds_tf_idf = sc_tf_idf.predict(testData['message'])
metrics(testData['label'], preds_tf_idf)
sc_bow = SpamClassifier(trainData, 'bow')
sc_bow.train()
preds_bow = sc_bow.predict(testData['message'])
metrics(testData['label'], preds_bow)
pm = process_message('I cant pick the phone right now. Pls send a message')
sc_tf_idf.classify(pm)
pm = process_message('Claim your 12 free Months of Netflix!')
sc_tf_idf.classify(pm)