-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_spam_ham_detection.py
90 lines (42 loc) · 1.07 KB
/
email_spam_ham_detection.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
# In[2]:
df = pd.read_csv('spam_ham_dataset.csv')
# In[3]:
df
# In[4]:
df.drop(['Unnamed: 0', 'label'], axis=1)
# In[12]:
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline
# In[18]:
model = Pipeline([
('vectorizer', CountVectorizer()),
('nb', MultinomialNB())
])
# In[19]:
X = df.text
y= df.label_num
# In[20]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.25, random_state=12)
# In[21]:
model.fit(X_train, y_train)
# In[29]:
model.score(X_test, y_test)
# In[40]:
def Check_Email_Spam_Ham(email):
pred = model.predict(email)
for p in pred:
if p == '1':
print('This email is a Spam')
else:
print('This email is not a Spam')
email_text = input('Please Enter the Mail Message : ')
text = []
text.append(email_text)
Check_Email_Spam_Ham(text)