# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Naive Bayes Classifier
#
# A project to classify spam and not spam messages in the formof texts using NB classifier. The data set here is taken from udacity/kaggle. Its easily available online. Our main aim here is to identify which words are mostly frequently used in spam messages and from that we would get the individual probabilty of each word and compute if the message is spam or not given that the word occurs, P(message is spam/the 'word').

# +
import pandas as pd

# Import the data using read_table
df = pd.read_table('SMSSpamCollection'
                  ,sep = "\t"
                  ,header = None
                  ,names = ['label','message'])

df.head()
# -

# Dimensions of the dataframe
df.shape

# +
#Replacing the labels to 0 and 1 as sklearn doesn't deal with characters but only numbers.
df['label'] = df.replace({'ham':0,'spam':1})

df.head()
# -

# ### Different Steps to implement
#
# 1) Convert all strings to lower case form
#
# 2) Remove all punctuations and stopwords
#
# 3) Tokenization - Split a sentence into individual words using a delimiter
#
# 4) Count frequencies of each word and store it in a document term matrix
#
#  DTM - each row represents each message and each column is a word, each cell will be the frequency of that word(col) in that message(row)

# +
from sklearn.feature_extraction.text import CountVectorizer

count_vector = CountVectorizer()
count_vector
# -

# ##### Split our data set into training and testing dataset

# +
from sklearn.cross_validation import train_test_split

X_train, X_test, Y_train, Y_test = train_test_split(df['message'],
                                                    df['label'],
                                                   random_state = 1)
# -

# Mandatory stuff
print('Number of rows in the total set: {}'.format(df.shape[0]))
print('Number of rows in the training set: {}'.format(Y_train.shape[0]))
print('Number of rows in the test set: {} '.format(Y_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, in 'training data' we are learning a vocabulary dictionary for the training
# data and then transforming the data into a DTM; Here we are not fitting the testing data into the CountVectorizer(), we are 
# only transforming the data into a DTM
testing_data = count_vector.transform(X_test)
# -

import numpy as np
print (np.matrix(training_data))

training_data.todense()

# ## Implement Naive Bayes 
#
# Converting the format and datatype

# Change the format of the test and train dataset for Y
Y_train = np.asarray(Y_train, dtype="|S6")
Y_test = np.asarray(Y_test, dtype="|S4")

# +
# Import the Multinomial Naive Bayes function
from sklearn.naive_bayes import MultinomialNB
naive_bayes = MultinomialNB()

naive_bayes.fit(training_data, Y_train)
# -

predictions = naive_bayes.predict(testing_data)

predictions

# ### Evaluating the model

# +
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

print('Accuracy score: ', format(accuracy_score(Y_test, predictions)))


StarcoderdataJupyterScriptsDedupFiltered