-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspam_email.py
86 lines (58 loc) · 2.23 KB
/
spam_email.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
# -*- coding: utf-8 -*-
"""Spam_email.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ldciQy_w-gW9VeTuUWEh5sLEQWMwUVjH
"""
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
raw_mail_data = pd.read_csv('/content/email.csv')
print (raw_mail_data)
mail_data=raw_mail_data.where((pd.notnull(raw_mail_data)), '')
mail_data.head(5)
mail_data.shape
#Label Encding spam as 0 and ham as 1
mail_data.loc[mail_data['Category']== 'spam', 'Category',]=0
mail_data.loc[mail_data['Category']== 'ham', 'Category',]=1
# separating text and label
x=mail_data['Message']
y=mail_data['Category']
x
#splitting data into training data and test data
x_train,x_test,y_train,y_test=train_test_split(x,y,train_size=0.2,random_state=3)
print(x.shape)
print(x_test.shape)
#feature extraction
#transform the text data to feature vectors for 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 to integers
y_train = y_train.astype('int')
y_test=y_test.astype('int')
print(x_train_features)
#training the model
#logical regression
model = LogisticRegression()
#training the logistic regression model
model.fit(x_train_features,y_train)
#evaluating the trained model
#prediction on trained model
prediction_data=model.predict(x_train_features)
accuracy_data=accuracy_score(y_train,prediction_data)
print('Accuracy on training data :',accuracy_data)
#prediction on test model
prediction_test=model.predict(x_test_features)
accuracy_test=accuracy_score(y_test,prediction_test)
print('Accuracy on training data :',accuracy_test)
#building a predictive system
input_mail=["Even my brother is not like to speak with me. They treat me like aids patent."]
#convert text to feature vectors
input_data_features=feature_extraction.transform(input_mail)
#making predictions
prediction=model.predict(input_data_features)
print(prediction)