-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailSpamDetection.py
More file actions
67 lines (48 loc) · 1.77 KB
/
EmailSpamDetection.py
File metadata and controls
67 lines (48 loc) · 1.77 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
import pandas as pd
import pickle
import re
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC
data = pd.read_csv('datasets/email.csv')
def clean_text(text):
text = text.lower()
text = re.sub(r"[^a-zA-Z0-9 ]", "", text)
return text
data["Message"] = data["Message"].apply(clean_text)
X = data['Message']
y = data['Category']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
vectorizer = TfidfVectorizer(
lowercase=True,
stop_words="english",
ngram_range=(1,2)
)
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)
logistic_model = LogisticRegression()
nbmodel = MultinomialNB()
svmmodel = LinearSVC()
models = {
"Logistic Regression": logistic_model,
"Naive Bayes": nbmodel,
"SVM Model": svmmodel
}
for name, model in models.items():
print("\n", name)
model.fit(X_train_vec, y_train)
y_pred = model.predict(X_test_vec)
print("Accuracy:", accuracy_score(y_test, y_pred)*100)
print("Classification Report\n")
print(classification_report(y_test, y_pred))
cmf = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cmf)
pickle.dump(vectorizer, open("vectorizer.pkl", "wb"))
pickle.dump(logistic_model, open("logistic_model.pkl", "wb"))
pickle.dump(nbmodel, open("nb_model.pkl", "wb"))
pickle.dump(svmmodel, open("svm_model.pkl", "wb"))
print("\nModels and vectorizer saved successfully.")