-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_bayes_classifier.py
248 lines (185 loc) · 10.3 KB
/
naive_bayes_classifier.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
# -*- coding: utf-8 -*-
"""Naive_Bayes_Classifier.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1JZwLGwBxEjnbv_8UTqEmmbDgFy_7Te2r
<div class="alert alert-block alert-info" >
<h1>Naive Bayes Classifier </h1>
## Build a spam classifier using Naive Bayes
"""
#Headers
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn
"""## Step 1:- Load your data
#### There are three datasets for training: TrainDataset1.csv, TrainDataset2.csv and TrainDataset3.txt. Each dataset contains short messages with the labels (ham or spam). Load the dataset using pandas.
"""
#Load your dataset in this cell
def loadData():
#your code
data1=pd.read_csv(r'TrainDataset1.csv')
data2=pd.read_csv(r'TrainDataset2.csv')
data3 = pd.read_csv('TrainDataset3.txt', delimiter='\t')
return data1,data2,data3
dataset1,dataset2,dataset3=loadData()
print("Dataset 1 contatin :")
print(dataset1)
print("Dataset 2 contain :")
print(dataset2)
print("Dataset 3 contain :")
print(dataset3)
training_data = pd.DataFrame(np.concatenate([dataset1.values, dataset2.values, dataset3.values]), columns=dataset1.columns)
print(training_data)
"""## Step 2:- Preprocess the data
#### Analysing the data, for this you will need to process the text, namely remove punctuation and stopwords, and then create a list of clean text words (Research how to do this) use any libraries that you feel comfortable. Now Combine them into one big data set for the training.
"""
# #Pre-process the data
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk.stem import PorterStemmer
import string
lemma=WordNetLemmatizer()
def preprocess(text):
text=[char for char in text if char not in string.punctuation]
text=''.join(text)
text=[word for word in text.split() if word.lower() not in stopwords.words('english')]
text=[PorterStemmer().stem(words) for words in text]
text=[lemma.lemmatize(word=w,pos='v') for w in text]
return text
"""## Step 3:- Visualise the data
#### Trying to visualize and analyse the data such as before and after pre processing, number of ham/spam etc. Analyse as many verticals you can, feel free to use graphical libraries like seaborn.
"""
# Visualise the data
print("Bar chart:-\n")
ham_count1, spam_count1 = training_data.type.value_counts()
plt.bar(["ham", "spam"], [ham_count1, spam_count1], color=['blue', 'red'])
plt.title("Training dataset(combined data)")
plt.xlabel='length'
plt.ylabel='number of messages'
plt.show()
print("Pie chart:-\n")
ham_count1, spam_count1 = dataset1.type.value_counts()
labels = 'spam', 'ham'
fig1,ax1 = plt.subplots()
ax1.pie([ham_count1,spam_count1], labels=labels,colors=['blue', 'red'],autopct='%1.1f%%')
plt.title("Training dataset(combined data)")
ax1.axis('equal')
plt.show()
"""## Step 4:- Build, train and validate the classifer
### Training on supervised data (labelled data)
#### Use the data in order to build your own Naive Bayes classifier (You can either use existing Naive Bayes from sklearn or build your own). Build the classifier, train it and then validate. Provide your result in confusion matrix (use heatmap from seaborn) along with the classification report from sklearn. Validation accuracy should be around 99%.
"""
# Build, train and validate the classifier,
#your code here
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from collections import Counter
#Considering spam messages as 1 and ham messages as 0
training_data['spam'] = training_data['type'].map( {'spam': 1, 'ham': 0} ).astype(int)
x_train, x_validate, y_train, y_validate = train_test_split(training_data["text"], training_data["spam"], test_size=0.2, random_state=5)
My_pipeline = Pipeline([
('vectorizer',TfidfVectorizer(analyzer=preprocess)), #Vectorizing
('classifier',MultinomialNB()) #NB classifier
])
#Training the classifier
My_pipeline.fit(x_train,y_train)
#Prediction using validation data
predicted_validation_data = My_pipeline.predict(x_validate)
#Calculating accuracy
accuracy = accuracy_score(y_validate, predicted_validation_data)
print("Accuracy:",accuracy*100,"%")
#Constructing the classification report
report = classification_report(y_validate, predicted_validation_data)
print("Classification_report: \n", report)
#Constucting the confusion matrix and visualizing it using heatmap
confusion_matrix_validation_data = confusion_matrix(y_validate, predicted_validation_data)
sns.heatmap(confusion_matrix_validation_data, annot=True)
"""## Step 5:- Test the classifier
### Supervised classification
#### Test your Classifier using the SMSSpamCollection.txt dataset provide a heatmap and classification report. Test accuracy should be around 99%.
"""
# Test the classifier
#your code here
#Loading the data
labelled_test_dataset = pd.read_csv('SMSSpamCollection.txt', delimiter='\t')
labelled_test_dataset.columns = ['type', 'text']
#Considering spam messages as 1 and ham messages as 0
labelled_test_dataset['spam'] = labelled_test_dataset['type'].map( {'spam': 1, 'ham': 0} ).astype(int)
#Prediction of labelled dataset
predicted_labelled_data = My_pipeline.predict(labelled_test_dataset["text"])
#Accuracy of labelled dataset
accuracy_labelled_data = accuracy_score(labelled_test_dataset["spam"], predicted_labelled_data)
print("Accuracy:",accuracy_labelled_data*100,"%")
#Classification report for labelled dataset
report_labelled_data = classification_report(labelled_test_dataset["spam"], predicted_labelled_data)
print("Classification_report: \n", report_labelled_data)
#Constucting the confusion matrix and visualizing it using heatmap
confusion_matrix_labelled_data = confusion_matrix(labelled_test_dataset["spam"], predicted_labelled_data)
sns.heatmap(confusion_matrix_labelled_data, annot=True)
"""### Unsupervised classification
#### Test your Classifier using the TestDataset.csv dataset. This dataset is not labelled so kindly predict the labels and visualise it.
"""
# Test the classifier
#your code here
#Loading the data
unlabelled_test_dataset = pd.read_csv('TestDataset.csv')
unlabelled_test_dataset.columns = ['type']
#Prediction of unlabelled dataset and plotting it
predicted_unlabelled_data = My_pipeline.predict(unlabelled_test_dataset["type"])
spam_count_predicted_unlabelled_data = np.count_nonzero(predicted_unlabelled_data == 1)
ham_count_predicted_unlabelled_data = np.count_nonzero(predicted_unlabelled_data == 0)
print("Bar chart:-\n")
plt.bar(["ham", "spam"], [ham_count_predicted_unlabelled_data,spam_count_predicted_unlabelled_data], color=['blue', 'red'],label=labels)
plt.title("Visualization of unlabelled dataset")
plt.xlabel='length'
plt.ylabel='number of messages'
plt.show()
print("Pie chart:-\n")
labels = 'spam', 'ham'
fig2, ax2 = plt.subplots()
ax2.pie([ham_count_predicted_unlabelled_data,spam_count_predicted_unlabelled_data], labels=labels,colors=['blue', 'red'],autopct='%1.1f%%')
plt.title("Visualization of unlabelled dataset")
ax2.axis('equal')
plt.show()
"""## Step 6:- Cheat the classifier
#### Try to cheat the classifier by adding "good words" to the end of test dataset(TestDataset.csv) e.g:- Oh! no share Market has fallen down by $100,000$ due to Corona outbreak... try mixing up spam and ham words see how the classifier works. Output the results in a good format to validate your work
"""
# Cheat the classifier
#your code here
cheating_classifier_msgs = ['CONGRATULATIONS!! Status of your application for HBRS MAS course...',
'Urgent! Hello bro, our group has planned for a movie in 30 minutes, Be ready ASAP, i will pick you up...',
'Go until jurong point, crazy..Available only... ',
'Happy morning, lets have breakfast at ... after the jog',
'CONGRATULATIONS!! Your Number was selected as the Winner in Power Lottery Competition, Kindly send your Details...',
'As per you request, "oh baby" song has been set as your caller tune',
'Dear Beneficiary, We want to confirm to you that our Bank {HSBC}, USA, has issued an ATM MasterCard Cash of $500,000 USD in mark',
'Hello Prabhudev, Welcome to Blizzard services!You have successfully created the following Blizzard Account:',
'Dear Kaushik, Your SmartStatement was created and linkind to this email ...',
'Dear customer, due to schedule maintainence activity net and mobile banking will not be available from 10-10-2020 ...']
cheating_classifier_msg_values = [0,0,0,0,1,0,1,0,0,0]
data = {'text': cheating_classifier_msgs, 'spam': cheating_classifier_msg_values}
cheating_classifier_dataset = pd.DataFrame(data=data)
predicted_cheating_classifier_dataset = My_pipeline.predict(cheating_classifier_dataset["text"])
accuracy_cheating_classifier_dataset = accuracy_score(cheating_classifier_dataset["spam"], predicted_cheating_classifier_dataset)
print("Accuracy:",accuracy_cheating_classifier_dataset*100,"%")
#Building a text report showing the main classification metrics
report_cheating_classifier_dataset = classification_report(cheating_classifier_dataset["spam"], predicted_cheating_classifier_dataset)
print("Classification_report: \n", report_cheating_classifier_dataset)
#Constucting the confusion matrix and visualizing it
confusion_matrix_cheating_classifier_dataset = confusion_matrix(cheating_classifier_dataset["spam"], predicted_cheating_classifier_dataset)
print("Heatmap of confusion matrix")
sns.heatmap(confusion_matrix_cheating_classifier_dataset, annot=True)
"""### Help
<a href="https://towardsdatascience.com/spam-filtering-using-naive-bayes-98a341224038" target="_top">Spam classification</a><br>
<a href="https://seaborn.pydata.org/generated/seaborn.heatmap.html" target="_top">Seaborn Heatmap</a><br>
<a href="https://scikit-learn.org/stable/modules/naive_bayes.html" target="_top">Sklearn Naive Bayes</a><br>
<a href="https://scikit-learn.org/stable/modules/model_evaluation.html" target="_top">Sklearn Metrics</a><br>
<a href="https://pandas.pydata.org/docs/getting_started/index.html#getting-started" target="_top">Intro to Pandas</a>
"""