-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmd.python
More file actions
73 lines (54 loc) · 2.57 KB
/
smd.python
File metadata and controls
73 lines (54 loc) · 2.57 KB
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
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
# loading the data from a CSV file to a pandas DataFrame
raw_mail_data = pd.read_csv('/content/mail_data.csv')
# replace the null values with a null string
mail_data = raw_mail_data.where((pd.notnull(raw_mail_data)), '')
# printing the first 5 rows of the dataframe
print(mail_data.head())
# checking the number of rows and columns in the dataframe
print(mail_data.shape)
# label spam mail as 0; ham mail as 1;
mail_data.loc[mail_data['Category'] == 'spam', 'Category'] = 0
mail_data.loc[mail_data['Category'] == 'ham', 'Category'] = 1
# separating the data as texts and label
X = mail_data['Message']
Y = mail_data['Category']
# splitting the data into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=3)
# transform the text data to feature vectors that can be used as input to Logistic Regression
feature_extraction = TfidfVectorizer(min_df=1, lowercase=True, stop_words="english")
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)
# print the shapes of the feature vectors
print(X_train_features.shape)
print(X_test_features.shape)
# initialize the Logistic Regression model
model = LogisticRegression()
# training the Logistic Regression model with the training data
model.fit(X_train_features, Y_train)
# prediction on training data
prediction_on_training_data = model.predict(X_train_features)
accuracy_on_training_data = accuracy_score(Y_train, prediction_on_training_data)
# 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)
print('Accuracy on training data:', accuracy_on_training_data)
print('Accuracy on test data:', accuracy_on_test_data)
# input mail for prediction
input_mail = ["I've been searching for the right words to thank you for this breather. I promise I won't take your help for granted and will fulfill 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)
# make prediction
prediction = model.predict(input_data_features)
if prediction[0] == 1:
print('Ham mail')
else:
print('Spam mail')