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

import pandas as pd
file = open('SMSSpamCollection','r')

data = pd.read_table('SMSSpamCollection',sep='\t',names=['label', 'sms_message'])

data.head(5)

# +
'''
for i in range(len(data)):
    if data.label[i] =='ham':
        data.label[i] =0
    else:
        data.label[i]=1
'''


#data[label]=data.label.map({'ham':0,'spam':1})
data['label']=data.label.map({'ham':0,'spam':1})
print(data.shape)       
# -

data.head(4)
#data.ix[:,0]

documents = ['Hello, how are you!',
             'Win money, win from home.',
             'Call me now.',
             'Hello, Call hello you tomorrow?']
lowercase_documents =[]
for i in documents:
    i=i.lower()
    lowercase_documents.append(i)
print(lowercase_documents)

'''
import string
without_punctuation_documents =[]
for i in lowercase_documents:
    i = i.translate(str.maketrans('', '', string.punctuation))
    without_punctuation_documents.append(i)
print(without_punctuation_documents)
'''

without_punctuation_documents=[]
import string
for i in lowercase_documents:
    for j in i:
        i=''.join(c for c in i if c not in(string.punctuation))
    without_punctuation_documents.append(i)
print(without_punctuation_documents)

tokens_list =[]
for i in without_punctuation_documents:
    tokens_list.append(i.split())
print(tokens_list)

# +
final_list_of_words=[j for i in tokens_list for j in i]
from collections import Counter
count = Counter()
for i in final_list_of_words:
    count[i] +=1
print(dict(count))
    

# -

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(data['sms_message'],data['label'],random_state = 1)
print("Number of rows in the total set:{} ".format(data.shape[0]))
print("Number of rows in the training set:{}".format(X_train.shape[0]))
print("Number of rows in the testing set:{}".format(X_test.shape[0]))
X_train[1]

from sklearn.feature_extraction.text import CountVectorizer
count_vector = CountVectorizer()
training_data = count_vector.fit_transform(X_train)
testing_data = count_vector.transform(X_test)
testing_data

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)))





StarcoderdataJupyterScriptsDedupFiltered