<jupyter_start><jupyter_text>Spam Classifier based on Bayesian Theorem  Data Preprocessing<jupyter_code>!wget https://archive.ics.uci.edu/ml/machine-learning-databases/00228/smsspamcollection.zip --no-check-certificate
!unzip smsspamcollection.zip
import pandas as pd

df=pd.read_table('SMSSpamCollection',names=['label','sms_message'])

df.head()<jupyter_output><empty_output><jupyter_text>Converting to numerical values<jupyter_code>df['label']=df.label.map({'ham':0, 'spam':1})

df.shape<jupyter_output><empty_output><jupyter_text><jupyter_code>from sklearn.model_selection  import train_test_split

X_train, X_test, y_train, y_test = train_test_split(df['sms_message'], 
                                                    df['label'], 
                                                    random_state=1)

print('Number of rows in the total set: {}'.format(df.shape[0]))
print('Number of rows in the training set: {}'.format(X_train.shape[0]))
print('Number of rows in the test set: {}'.format(X_test.shape[0]))
# Instantiate the CountVectorizer method
count_vector = CountVectorizer()

# Fit the training data and then return the matrix
training_data = count_vector.fit_transform(X_train)

# Transform testing data and return the matrix. Note we are not fitting the testing data into the CountVectorizer()
testing_data = count_vector.transform(X_test)<jupyter_output><empty_output><jupyter_text>Bayes Theorem  So how does bayes theorem works!!It a relation between what is we know to what we need to infere. That is from a dataset we make a model in which knows the occurance rate of particular words in a spam message. With that probability we find the probabilty of a message being spam with the words in it. Naive Bayes ApproachNaive approach means feature is independent. Meaning there probabilites collectively can be multipled version of individual occurances.  Spam classifier From the dataset we make a document-word-frequency-model in which the preprocessed messages are fed into the model and a model of probability of occurances is made. Then from these data a predicting model in which given message is spam or not is made words of that make that document vocab.Used a multinomial because our data is discrete state. If it was continous we could have used Gaussian Naive Bayes .<jupyter_code>from sklearn.naive_bayes import MultinomialNB
naive_bayes = MultinomialNB()
naive_bayes.fit(training_data,y_train)
predictions = naive_bayes.predict(testing_data)
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
print('Accuracy score: ', format(accuracy_score(y_test,predictions)))
print('Precision score: ', format(precision_score(y_test,predictions)))
print('Recall score: ', format(recall_score(y_test,predictions)))
print('F1 score: ', format(f1_score(y_test,predictions)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08383023500526131, 'fraction_numerical': 0.004910557699052964, 'mean_word_length': 5.351893095768374, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '346034', 'license': 'MIT', 'n_tokens_mistral': 854, 'n_tokens_neox': 815, 'n_words': 271, 'path': 'spam_classifier.ipynb', 'repo_name': 'akhilmhdh/spam-classifier-bayes', 'size': '2851'}