-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspam_sms_classification.py
370 lines (271 loc) · 11.7 KB
/
spam_sms_classification.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
# -*- coding: utf-8 -*-
"""Spam SMS Classification.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1AFFjLb5NsyeRQ9WaRzqJoq8QujqS27Om
**Spam SMS Classification using NLP (Natural Language Processing)**
"""
# Importing essential libraries
import numpy as np
import pandas as pd
# Loading the dataset
df = pd.read_csv('/content/Spam SMS Collection.txt', sep='\t', names=['label', 'message'])
"""# **Exploratory Data Analysis (EDA)**
* Exploring NaN values in dataset
* Plotting countplots for Spam vs. Ham
"""
df.shape
df.columns
df.dtypes
df.head()
df.tail()
df.info()
df.describe(include='object')
"""_**Note: No NaN values** in the dataset._"""
# Mapping values for label
df['label'] = df['label'].map({'ham': 0, 'spam': 1})
df.head()
df.tail()
# Commented out IPython magic to ensure Python compatibility.
# Importing essential libraries for visualization
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline
# Countplot for Spam vs. Ham as imbalanced dataset
plt.figure(figsize=(8,8))
g = sns.countplot(x='label', hue='label', data=df)
p = plt.title('Countplot for Spam vs. Ham as imbalanced dataset')
p = plt.xlabel('Is SMS Spam?')
p = plt.ylabel('Count')
"""_**Insight:** From the above countplot, it is evident that the **dataset is imbalanced**._
# **Feature Engineering**
* Handling imbalanced dataset using Oversampling
* Creating new features e.g. word_count, contains_currency_symbol, contains_numbers, etc.
"""
# Handling imbalanced dataset using Oversampling
only_spam = df[df['label']==1]
print('Number of Spam records: {}'.format(only_spam.shape[0]))
print('Number of Ham records: {}'.format(df.shape[0]-only_spam.shape[0]))
count = int((df.shape[0]-only_spam.shape[0])/only_spam.shape[0])
for i in range(0, count-1):
df = pd.concat([df, only_spam])
df.shape
# Countplot for Spam vs. Ham as balanced dataset
plt.figure(figsize=(8,8))
g = sns.countplot(x='label', hue='label', data=df)
p = plt.title('Countplot for Spam vs. Ham as balanced dataset')
p = plt.xlabel('Is SMS Spam?')
p = plt.ylabel('Count')
# Creating new feature word_count
df['word_count'] = df['message'].apply(lambda x: len(x.split()))
df.head()
plt.figure(figsize=(12, 6))
# 1-row, 2-column, go to the first subplot
plt.subplot(1, 2, 1)
g1 = sns.histplot(data=df[df['label']==0], x='word_count', kde=True, stat="density", color='blue')
g1.set(title='Distribution of word_count for Ham messages')
# 1-row, 2-column, go to the second subplot
plt.subplot(1, 2, 2)
g2 = sns.histplot(data=df[df['label']==1], x='word_count', kde=True, stat="density", color='red')
g2.set(title='Distribution of word_count for Spam messages')
plt.tight_layout()
plt.show()
"""_**Insight:** **Spam messages** word_count fall in the range of **15-30 words**, whereas majority of the **Ham messages** fall in the range of **below 25 words**._"""
# Creating feature contains_currency_symbol
def currency(x):
currency_symbols = ['€', '$', '¥', '£', '₹']
for i in currency_symbols:
if i in x:
return 1
return 0
df['contains_currency_symbol'] = df['message'].apply(currency)
df.tail()
# Countplot for contains_currency_symbol
plt.figure(figsize=(8,8))
g = sns.countplot(x='contains_currency_symbol', data=df, hue='label')
p = plt.title('Countplot for contain_currency')
p = plt.xlabel('Does SMS contain currency symbol?')
p = plt.ylabel('Count')
p = plt.legend(labels=['Ham', 'Spam'], loc=9)
"""_**Insight: Almost 1/3 of Spam messages contain currency symbols**, and currency symbols are **rarely used in Ham messages.**_"""
# Creating feature contains_number
def numbers(x):
for i in x:
if ord(i)>=48 and ord(i)<=57:
return 1
return 0
df['contains_number'] = df['message'].apply(numbers)
df.head()
# Countplot for contains_number
plt.figure(figsize=(8,8))
g = sns.countplot(x='contains_number', data=df, hue='label')
p = plt.title('Countplot for contain_numbers')
p = plt.xlabel('Does SMS contain number?')
p = plt.ylabel('Count')
p = plt.legend(labels=['Ham', 'Spam'], loc=9)
"""_**Insight:** It is evident that **most of the Spam messages contain numbers,** and **majority of the Ham messages donot contain numbers.**_
# **Data Cleaning**
* Removing special character and numbers using regular expression
* Converting the entire sms into lower case
* Tokenizing the sms by words
* Removing the stop words
* Lemmatizing the words
* Joining the lemmatized words
* Building a corpus of messages
"""
# Importing essential libraries for performing NLP
import nltk
import re
nltk.download('stopwords')
nltk.download('wordnet')
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
# Cleaning the messages
corpus = []
wnl = WordNetLemmatizer()
for sms_string in list(df.message):
# Cleaning special character from the sms
message = re.sub(pattern='[^a-zA-Z]', repl=' ', string=sms_string)
# Converting the entire sms into lower case
message = message.lower()
# Tokenizing the sms by words
words = message.split()
# Removing the stop words
filtered_words = [word for word in words if word not in set(stopwords.words('english'))]
# Lemmatizing the words
lemmatized_words = [wnl.lemmatize(word) for word in filtered_words]
# Joining the lemmatized words
message = ' '.join(lemmatized_words)
# Building a corpus of messages
corpus.append(message)
corpus[0:3]
# Creating the Bag of Words model
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(max_features=500)
vectors = tfidf.fit_transform(corpus).toarray()
feature_names = tfidf.get_feature_names_out()
# Extracting independent and dependent variables from the dataset
X = pd.DataFrame(vectors, columns=feature_names)
y = df['label']
"""# **Model Building & Evaluation**
_**Metric: F1-Score**_
* Multiomail Naive Bayes: 0.943
* Decision Tree: 0.98
* **Random Forest (Ensemble): 0.994**
* Voting (Multinomial Naive Bayes + Decision Tree): 0.98
"""
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
"""**Using Naive Bayes Model**"""
# Fitting Naive Bayes to the Training set
from sklearn.naive_bayes import MultinomialNB
mnb = MultinomialNB()
cv = cross_val_score(mnb, X, y, scoring='f1', cv=10)
print('--- Average F1-Score for MNB model: {} ---'.format(round(cv.mean(), 3)))
print('Standard Deviation: {}'.format(round(cv.std(), 3)))
# Classification report for MNB model
mnb = MultinomialNB()
mnb.fit(X_train, y_train)
y_pred = mnb.predict(X_test)
print('--- Classification report for MNB model ---')
print(classification_report(y_test, y_pred))
# Confusion matrix of MNB model
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8,5))
axis_labels = ['ham', 'spam']
g = sns.heatmap(data=cm, annot=True, cmap="Blues", xticklabels=axis_labels, yticklabels=axis_labels, fmt='g', cbar_kws={"shrink": 0.5})
p = plt.xlabel('Actual values')
p = plt.ylabel('Predicted values')
p = plt.title('--- Confusion Matrix for Multinomial Naive Bayes model ---')
"""**Using Decision Tree Model**"""
# Fitting Decision Tree to the Training set
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier()
cv = cross_val_score(dt, X, y, scoring='f1', cv=10)
print('--- Average F1-Score for Decision Tree model: {} ---'.format(round(cv.mean(), 3)))
print('Standard Deviation: {}'.format(round(cv.std(), 3)))
# Classification report for Decision Tree model
dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
y_pred = dt.predict(X_test)
print('--- Classification report for Decision Tree model ---')
print(classification_report(y_test, y_pred))
# Confusion matrix of Decision Tree model
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8,5))
axis_labels = ['ham', 'spam']
g = sns.heatmap(data=cm, annot=True, cmap="Blues", xticklabels=axis_labels, yticklabels=axis_labels, fmt='g', cbar_kws={"shrink": 0.5})
p = plt.xlabel('Actual values')
p = plt.ylabel('Predicted values')
p = plt.title('--- Confusion Matrix for Decision Tree model ---')
"""**Using Random Forest Model**"""
# Fitting Random Forest to the Training set
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=10)
cv = cross_val_score(rf, X, y, scoring='f1', cv=10,)
print('--- Average F1-Score for Random Forest model: {} ---'.format(round(cv.mean(), 3)))
print('Standard Deviation: {}'.format(round(cv.std(), 3)))
# Classification report for Random Forest model
rf = RandomForestClassifier(n_estimators=20)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
print('--- Classification report for Random Forest model ---')
print(classification_report(y_test, y_pred))
# Confusion matrix of Random Forest model
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8,5))
axis_labels = ['ham', 'spam']
g = sns.heatmap(data=cm, annot=True, cmap="Blues", xticklabels=axis_labels, yticklabels=axis_labels, fmt='g', cbar_kws={"shrink": 0.5})
p = plt.xlabel('Actual values')
p = plt.ylabel('Predicted values')
p = plt.title('--- Confusion Matrix for Random Forest model ---')
"""_**Note:** Decision Tree & MNB algorithms are selected and fed to Voting algorithm to increase the F1-Score!_
**Using Voting Algorithm** (Decison Tree + Naive Bayes Model)
"""
# Fitting Decision Tree and MNB to VotingClassifier
from sklearn.ensemble import VotingClassifier
vc = VotingClassifier([('decision_tree', dt), ('m_naive_bayes', mnb)], weights=[2,1])
cv = cross_val_score(vc, X, y, cv=10, scoring='f1')
print('--- Average F1-Score for VotingClassifier model: {} ---'.format(round(cv.mean(), 3)))
print('Standard Deviation: {}'.format(round(cv.std(), 3)))
"""_**Note:** Voting algorithm did not out-perform Random Forest algorithm, hence **Random Forest algorithm is selected for predicting the results of this problem statement.**_
# **Making Predictions**
"""
def predict_spam(sample_message):
sample_message = re.sub(pattern='[^a-zA-Z]',repl=' ', string = sample_message)
sample_message = sample_message.lower()
sample_message_words = sample_message.split()
sample_message_words = [word for word in sample_message_words if not word in set(stopwords.words('english'))]
final_message = [wnl.lemmatize(word) for word in sample_message_words]
final_message = ' '.join(final_message)
temp = tfidf.transform([final_message]).toarray()
return rf.predict(temp)
# Prediction 1 - Lottery text message
sample_message = 'IMPORTANT - You could be entitled up to £3,160 in compensation from mis-sold PPI on a credit card or loan. Please reply PPI for info or STOP to opt out.'
if predict_spam(sample_message):
print('Gotcha! This is a SPAM message.')
else:
print('This is a HAM (normal) message.')
# Prediction 2 - Casual text chat
sample_message = 'Came to think of it. I have never got a spam message before.'
if predict_spam(sample_message):
print('Gotcha! This is a SPAM message.')
else:
print('This is a HAM (normal) message.')
# Prediction 3 - Transaction confirmation text message
sample_message = 'Sam, your rent payment for Jan 19 has been received. $1,300 will be drafted from your Wells Fargo Account ******0000 within 24-48 business hours. Thank you!'
if predict_spam(sample_message):
print('Gotcha! This is a SPAM message.')
else:
print('This is a HAM (normal) message.')
# Predicting values 4 - Feedback message
sample_message = 'Tammy, thanks for choosing Carl’s Car Wash for your express polish. We would love to hear your thoughts on the service. Feel free to text back with any feedback. Safe driving!'
if predict_spam(sample_message):
print('Gotcha! This is a SPAM message.')
else:
print('This is a HAM (normal) message.')
import pickle
pickle.dump(tfidf,open('vectorizer.pkl','wb'))
pickle.dump(rf,open('model.pkl','wb'))