import pandas as pd from sklearn.model_selection import train_test_split from sklearn.feature_extraction.text import CountVectorizer from sklearn.naive_bayes import MultinomialNB from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/00228/smsspamcollection.zip" df = pd.read_csv(url, compression='zip', sep='\t', names=['label', 'message'])
df['label'] = df['label'].map({'ham': 0, 'spam': 1}) # Convert labels to binary (0: ham, 1: spam)
X_train, X_test, y_train, y_test = train_test_split(df['message'], df['label'], test_size=0.2, random_state=42)
vectorizer = CountVectorizer() X_train_matrix = vectorizer.fit_transform(X_train) X_test_matrix = vectorizer.transform(X_test)
classifier = MultinomialNB() classifier.fit(X_train_matrix, y_train)
predictions = classifier.predict(X_test_matrix)
accuracy = accuracy_score(y_test, predictions) conf_matrix = confusion_matrix(y_test, predictions) classification_rep = classification_report(y_test, predictions)
print(f"Accuracy: {accuracy}") print("Confusion Matrix:") print(conf_matrix) print("Classification Report:") print(classification_rep)