-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
160 lines (117 loc) · 4.21 KB
/
main.py
File metadata and controls
160 lines (117 loc) · 4.21 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Suppress warnings
import warnings
warnings.filterwarnings('ignore')
# Import necessary libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score, confusion_matrix, classification_report,
roc_curve, auc
)
# Config (set local CSV path)
# Local (Windows) example:
file_path = r"E:\SMS_Spam_Detection\spam.csv" # <- change if needed
# Choose model: 'svm' (recommended), 'nb' (baseline), or 'logreg'
MODEL_CHOICE = 'svm'
# Load the dataset
# The original CSV has extra unnamed columns; we’ll select v1 (label) and v2 (message)
df = pd.read_csv(file_path, encoding='ISO-8859-1')
df = df[['v1', 'v2']]
df.columns = ['label', 'message']
# Display the first few rows of the dataset
df.head()
# Basic cleaning (non-destructive, keeps theme)
# Strip whitespace and drop empty messages
df['message'] = df['message'].astype(str).str.strip()
df = df[df['message'].str.len() > 0].reset_index(drop=True)
# Check for missing values
df.isnull().sum()
# Plot the distribution of spam and ham messages
sns.countplot(x='label', data=df)
plt.title('Distribution of Spam and Ham Messages')
plt.xlabel('Label')
plt.ylabel('Count')
plt.tight_layout()
plt.show()
# Convert text data to numerical data
USE_TFIDF = True
if USE_TFIDF:
vectorizer = TfidfVectorizer(
lowercase=True,
ngram_range=(1, 2),
min_df=2,
max_df=0.95,
sublinear_tf=True
)
else:
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(df['message'])
y = df['label'].map({'ham': 0, 'spam': 1}).values
# Split 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=42, stratify=y
)
# Train a classifier (better and efficient by default)
if MODEL_CHOICE == 'svm':
# Fast linear SVM (strong baseline for sparse text)
model = LinearSVC() # hinge loss, decision_function available
elif MODEL_CHOICE == 'logreg':
# Also strong; liblinear handles sparse, smaller datasets well
model = LogisticRegression(max_iter=2000, solver='liblinear')
else:
# Baseline Naive Bayes
model = MultinomialNB()
model.fit(X_train, y_train)
# Make predictions on the test data
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred, target_names=['ham', 'spam'])
print(f"Accuracy: {accuracy:.4f}")
print("\nConfusion Matrix:\n", conf_matrix)
print("\nClassification Report:\n", class_report)
# Confusion Matrix plot
plt.figure(figsize=(6, 5))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues',
xticklabels=['ham', 'spam'], yticklabels=['ham', 'spam'])
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.title('Confusion Matrix')
plt.tight_layout()
plt.show()
# ROC Curve and AUC (binary)
# We can build ROC using decision scores (preferred) or probabilities.
# LinearSVC: use decision_function; NB/LogReg: use predict_proba (or decision_function for LogReg).
if hasattr(model, "decision_function"):
y_scores = model.decision_function(X_test)
elif hasattr(model, "predict_proba"):
# use probability of positive class
y_scores = model.predict_proba(X_test)[:, 1]
else:
y_scores = None
if y_scores is not None:
fpr, tpr, _ = roc_curve(y_test, y_scores)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(6, 5))
plt.plot(fpr, tpr, lw=2, label=f'ROC curve (AUC = {roc_auc:.3f})')
plt.plot([0, 1], [0, 1], 'k--', lw=1)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve (Spam vs Ham)')
plt.legend(loc="lower right")
plt.tight_layout()
plt.show()
# Inference helper (optional)
def predict_sms(texts):
x_new = vectorizer.transform(texts)
preds = model.predict(x_new)
return ['spam' if p == 1 else 'ham' for p in preds]