<jupyter_start><jupyter_text>SPAM CLASSIFIERThis exercise is mostly about applying text preprocessing than actual spam classfication. We will be using Multinomial Naive Bayes for Classification. ( mostly cause of less data)Why Stemming? The tradeoff is not worth it. NB is not<jupyter_code>import pandas as pd

df = pd.read_csv("data/SMSSpamData/SMSSpamCollection", sep = '\t', names = ['Label', 'Message'])
df.head()
import re
import nltk 
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer

stemmer = PorterStemmer()
processed_messages = []

for i in range(len(df)):
    processed_message = re.sub('[^a-zA-z]', ' ', df['Message'][i])
    processed_message = processed_message.lower()
    processed_message = processed_message.split()
    
    processed_message = [stemmer.stem(word) for word in processed_message if not word in stopwords.words('english')]
    processed_message = ' '.join(processed_message)
    
    processed_messages.append(processed_message)

df['Processed Message'] = processed_messages
df.head()
from sklearn.feature_extraction.text import CountVectorizer

count_vectorizer = CountVectorizer()
X = count_vectorizer.fit_transform(processed_messages).toarray()

y = pd.get_dummies(df['Label'])
y.drop(columns=['ham'], inplace = True)
y = y['spam'].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
from sklearn.naive_bayes import MultinomialNB

model = MultinomialNB()
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
from sklearn.metrics import classification_report
report = classification_report(y_test, y_pred)
print(report)<jupyter_output>precision    recall  f1-score   support

           0       0.99      0.99      0.99       959
           1       0.91      0.95      0.93       156

    accuracy                           0.98      1115
   macro avg       0.95      0.97      0.96      1115
weighted avg       0.98      0.98      0.98      1115

0.9802690582959641
{'fraction_non_alphanumeric': 0.0946012877662209, 'fraction_numerical': 0.0391282813273898, 'mean_word_length': 3.6013667425968108, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '439417', 'license': 'MIT', 'n_tokens_mistral': 707, 'n_tokens_neox': 647, 'n_words': 166, 'path': 'natural-language-processing/nlp-projects/sms-spam-classification/spam_classifier.ipynb', 'repo_name': 'naagarjunsa/data-science-portfolio', 'size': '2019'}