-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_three.py
More file actions
39 lines (24 loc) · 908 Bytes
/
Copy pathProject_three.py
File metadata and controls
39 lines (24 loc) · 908 Bytes
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
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix
data = pd.read_csv("emails.csv")
X = data["text"]
y = data["label"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
vectorizer = TfidfVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)
model = MultinomialNB()
model.fit(X_train_vec, y_train)
y_pred = model.predict(X_test_vec)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
email = input("\nEnter email text: ")
email_vec = vectorizer.transform([email])
prediction = model.predict(email_vec)[0]
print("Result:", prediction.upper())