antitheft159
commited on
Commit
•
aa98e36
1
Parent(s):
9654148
Upload epurethim_159.py
Browse files- epurethim_159.py +40 -0
epurethim_159.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""epurethim.159
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1UmqJMfDY_e89v6dh4maq9aobuj5vz79k
|
8 |
+
"""
|
9 |
+
|
10 |
+
import pandas as pd
|
11 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
12 |
+
from sklearn.model_selection import train_test_split
|
13 |
+
from sklearn.naive_bayes import MultinomialNB
|
14 |
+
from sklearn.metrics import accuracy_score
|
15 |
+
|
16 |
+
dataset = pd.read_csv('/content/emails.csv')
|
17 |
+
|
18 |
+
dataset.head()
|
19 |
+
|
20 |
+
vectorizer = CountVectorizer()
|
21 |
+
X = vectorizer.fit_transform(dataset['text'])
|
22 |
+
|
23 |
+
X_train, X_test, y_train, y_test = train_test_split(X, dataset['spam'], test_size=0.2)
|
24 |
+
|
25 |
+
model = MultinomialNB()
|
26 |
+
model.fit(X_train, y_train)
|
27 |
+
|
28 |
+
yPred = model.predict(X_test)
|
29 |
+
|
30 |
+
accuracy = accuracy_score(y_test, yPred)
|
31 |
+
print(accuracy)
|
32 |
+
|
33 |
+
def predictMessage(message):
|
34 |
+
messageVector = vectorizer.transform([message])
|
35 |
+
prediction = model.predict(messageVector)
|
36 |
+
return 'Spam' if prediction[0] == 1 else 'Ham'
|
37 |
+
|
38 |
+
userMessage = input('Enter text to predict:')
|
39 |
+
prediction = predictMessage(userMessage)
|
40 |
+
print(f'The message is {prediction}')
|