-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms.py
52 lines (43 loc) · 1.81 KB
/
sms.py
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
import os
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.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import classification_report, accuracy_score
# Set working directory
os.chdir(r'D:\Desktop\vscode\projects\python\aspirenex\sms span model')
# Try reading the file with different encodings and inspect the columns
try:
df = pd.read_csv("spam.csv", encoding='utf-8')
except UnicodeDecodeError:
df = pd.read_csv('spam.csv', encoding='ISO-8859-1')
# Display the first few rows and the column names
print(df.head())
print(df.columns)
# Assuming the columns are ['v1', 'v2', ...] where 'v1' is label and 'v2' is the message
# Adjust the column selection based on the actual column names
df = df[['v1', 'v2']]
df.columns = ['label', 'message']
# Convert labels to binary
df['label'] = df['label'].map({'ham': 0, 'spam': 1})
# Split the data
X_train, X_test, y_train, y_test = train_test_split(df['message'], df['label'], test_size=0.2, random_state=42)
# Feature extraction using TF-IDF
vectorizer = TfidfVectorizer()
X_train_tfidf = vectorizer.fit_transform(X_train)
X_test_tfidf = vectorizer.transform(X_test)
# Train and evaluate different models
models = {
"Naive Bayes": MultinomialNB(),
"Logistic Regression": LogisticRegression(max_iter=1000),
"Support Vector Machine": SVC(kernel='linear', probability=True)
}
for name, model in models.items():
model.fit(X_train_tfidf, y_train)
predictions = model.predict(X_test_tfidf)
print(f"{name}:\n")
print(f"Accuracy: {accuracy_score(y_test, predictions)}")
print(classification_report(y_test, predictions))
print("\n")