import os, pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
# Step 1: Load dataset
os.chdir(os.path.dirname(__file__)) # sets working dir to script’s folder
data = pd.read_csv("email_dataset.csv")
data = data.dropna(subset=['Text']) #Drop rows with NaN in Text
# Columns: 'Text' (email content), 'Class' (1 = phishing, 0 = legit)
# Step 2: Vectorize text
vectorizer = TfidfVectorizer(stop_words='english', max_features=5000)
X = vectorizer.fit_transform(data['Text'])
y = data['Class']
# Step 3: Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Step 4: Train models
log_reg = LogisticRegression(max_iter=1000).fit(X_train, y_train)
nb = MultinomialNB().fit(X_train, y_train)
svm = LinearSVC().fit(X_train, y_train)
rf = RandomForestClassifier(n_estimators=100).fit(X_train, y_train)
# Step 5: Evaluate
print("Logistic Regression Results:")
print(classification_report(y_test, log_reg.predict(X_test)))
print("Naive Bayes Results:")
print(classification_report(y_test, nb.predict(X_test)))
print("SVM Results:")
print(classification_report(y_test, svm.predict(X_test)))
print("Random Forest Results:")
print(classification_report(y_test, rf.predict(X_test)))
data = pd.read_csv("email_dataset.csv")
vectorizer = TfidfVectorizer(stop_words='english', max_features=5000)
X = vectorizer.fit_transform(data['Text'])
y = data['Class']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
log_reg = LogisticRegression(max_iter=1000).fit(X_train, y_train)
nb = MultinomialNB().fit(X_train, y_train)
svm = LinearSVC().fit(X_train, y_train)
rf = RandomForestClassifier(n_estimators=100).fit(X_train, y_train)print(classification_report(y_test, log_reg.predict(X_test)))
Logistic Regression Results:
precision recall f1-score support
0 0.97 1.00 0.98 1336
1 1.00 0.96 0.98 1050
accuracy 0.98 2386
macro avg 0.98 0.98 0.98 2386
weighted avg 0.98 0.98 0.98 2386
Naive Bayes Results:
precision recall f1-score support
0 0.98 0.99 0.98 1336
1 0.99 0.97 0.98 1050
accuracy 0.98 2386
macro avg 0.98 0.98 0.98 2386
weighted avg 0.98 0.98 0.98 2386
SVM Results:
precision recall f1-score support
0 0.98 1.00 0.99 1336
1 1.00 0.97 0.98 1050
accuracy 0.99 2386
macro avg 0.99 0.98 0.99 2386
weighted avg 0.99 0.99 0.99 2386
Random Forest Results:
precision recall f1-score support
0 0.98 1.00 0.99 1336
1 1.00 0.97 0.98 1050
accuracy 0.99 2386
macro avg 0.99 0.98 0.99 2386
weighted avg 0.99 0.99 0.99 2386
No Comment yet!