-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpamMail.py
193 lines (91 loc) · 3.01 KB
/
SpamMail.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# In[3]:
# loading the data from csv file to a pandas Dataframe
raw_mail_data = pd.read_csv('C:/Users/abhis/Downloads/mail_data.csv')
# In[4]:
print(raw_mail_data)
# In[5]:
# replace the null values with a null string
mail_data = raw_mail_data.where((pd.notnull(raw_mail_data)),'')
# In[6]:
# printing the first 5 rows of the dataframe
mail_data.head()
# In[7]:
# checking the number of rows and columns in the dataframe
mail_data.shape
# In[8]:
# label spam mail as 0; ham mail as 1;
#Label Encoding
mail_data.loc[mail_data['Category'] == 'spam', 'Category',] = 0
mail_data.loc[mail_data['Category'] == 'ham', 'Category',] = 1
#spam - 0
#ham - 1
# In[9]:
# separating the data as texts and label
X = mail_data['Message']
Y = mail_data['Category']
# In[10]:
print(X)
# In[11]:
print(Y)
# In[12]:
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=3)
# In[13]:
print(X.shape)
print(X_train.shape)
print(X_test.shape)
# In[14]:
#Feature Extraction
# transform the text data to feature vectors that can be used as input to the Logistic regression
feature_extraction = TfidfVectorizer(min_df = 1, stop_words='english', lowercase='True')
X_train_features = feature_extraction.fit_transform(X_train)
X_test_features = feature_extraction.transform(X_test)
# convert Y_train and Y_test values as integers
Y_train = Y_train.astype('int')
Y_test = Y_test.astype('int')
# In[15]:
print(X_train)
# In[16]:
print(X_train_features)
# In[17]:
#Training the Model
#Logistic Regression
# In[18]:
model = LogisticRegression()
# In[19]:
# training the Logistic Regression model with the training data
model.fit(X_train_features, Y_train)
# In[20]:
# prediction on training data
#Evaluating the trained model
prediction_on_training_data = model.predict(X_train_features)
accuracy_on_training_data = accuracy_score(Y_train, prediction_on_training_data)
# In[21]:
print('Accuracy on training data : ', accuracy_on_training_data)
# In[22]:
# prediction on test data
prediction_on_test_data = model.predict(X_test_features)
accuracy_on_test_data = accuracy_score(Y_test, prediction_on_test_data)
# In[23]:
print('Accuracy on test data : ', accuracy_on_test_data)
# In[24]:
#Building a Predictive System
input_mail = ["I've been searching for the right words to thank you for this breather. I promise i wont take your help for granted and will fulfil my promise. You have been wonderful and a blessing at all times"]
# convert text to feature vectors
input_data_features = feature_extraction.transform(input_mail)
# making prediction
prediction = model.predict(input_data_features)
print(prediction)
if (prediction[0]==1):
print('Ham mail')
else:
print('Spam mail')
# In[ ]: