|
import pandas as pd
|
|
import string
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.pipeline import Pipeline
|
|
from sklearn.feature_extraction.text import CountVectorizer
|
|
from sklearn.naive_bayes import MultinomialNB
|
|
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
|
|
from joblib import dump
|
|
import nltk
|
|
from nltk.corpus import stopwords
|
|
from nltk.stem import PorterStemmer
|
|
|
|
|
|
nltk.download('stopwords')
|
|
|
|
|
|
stop_words = set(stopwords.words('english'))
|
|
stemmer = PorterStemmer()
|
|
|
|
def preprocess(text):
|
|
"""Clean and preprocess text for model input."""
|
|
|
|
text = text.lower()
|
|
text = ''.join([char for char in text if char not in string.punctuation])
|
|
|
|
words = text.split()
|
|
return ' '.join([stemmer.stem(word) for word in words if word not in stop_words])
|
|
|
|
|
|
data = pd.read_csv('spam.csv', encoding='latin-1')
|
|
data = data[['v1', 'v2']]
|
|
data.columns = ['label', 'message']
|
|
|
|
|
|
data['label'] = data['label'].map({'ham': 'LABEL_0', 'spam': 'LABEL_1'})
|
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(data['message'], data['label'], test_size=0.2, random_state=42)
|
|
|
|
|
|
model_pipeline = Pipeline([
|
|
('vectorizer', CountVectorizer(preprocessor=preprocess)),
|
|
('classifier', MultinomialNB())
|
|
])
|
|
|
|
|
|
model_pipeline.fit(X_train, y_train)
|
|
|
|
|
|
y_pred = model_pipeline.predict(X_test)
|
|
print('Accuracy:', accuracy_score(y_test, y_pred))
|
|
print('Confusion Matrix:\n', confusion_matrix(y_test, y_pred))
|
|
print('Classification Report:\n', classification_report(y_test, y_pred))
|
|
|
|
|
|
dump(model_pipeline, 'spam_classifier_pipeline.joblib')
|
|
|
|
|
|
|