<jupyter_start><jupyter_text>Import the Dataset<jupyter_code>import pandas as pd

messages = pd.read_csv('SMSSpamCollection', sep='\t',
                           names=["label", "message"])<jupyter_output><empty_output><jupyter_text>Importing Dependencies<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import re
import nltk
import numpy as np
nltk.download('stopwords')
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import accuracy_score
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
ps = PorterStemmer()<jupyter_output>[nltk_data] Downloading package stopwords to
[nltk_data]     C:\Users\Akshay\AppData\Roaming\nltk_data...
[nltk_data]   Package stopwords is already up-to-date!<jupyter_text>Data preprocessing<jupyter_code>messages.head()
messages.isnull().sum()
len(messages)<jupyter_output><empty_output><jupyter_text>Histogram visualisation for ham and spam<jupyter_code>plt.figure(figsize=(8,5))
sns.countplot(x='label', data=messages)
plt.xlabel('message')
plt.ylabel('Count')
plt.show()<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code>corpus = []
for i in range(0, len(messages)):
    review = re.sub('[^a-zA-Z]', ' ', messages['message'][i])
    review = review.lower()
    review = review.split()
    
    review = [ps.stem(word) for word in review if not word in stopwords.words('english')]
    review = ' '.join(review)
    corpus.append(review)<jupyter_output><empty_output><jupyter_text>Creating the Bag of Words model<jupyter_code>cv = CountVectorizer(max_features=2500)
X = cv.fit_transform(corpus).toarray()<jupyter_output><empty_output><jupyter_text>Extracting dependent variable from the dataset<jupyter_code>y = pd.get_dummies(messages['label'])
y = y.iloc[:, 1].values
y<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0)<jupyter_output><empty_output><jupyter_text>Accuracy<jupyter_code>best_accuracy = 0.0
alpha_val = 0.0
for i in np.arange(0.0,1.1,0.1):
    temp_classifier = MultinomialNB(alpha=i)
    temp_classifier.fit(X_train, y_train)
    temp_y_pred = temp_classifier.predict(X_test)
    score = accuracy_score(y_test, temp_y_pred)
    print("Accuracy score for alpha={} is: {}%".format(round(i,1), round(score*100,2)))
    if score>best_accuracy:
        best_accuracy = score
        alpha_val = i
print('***************************************************')
print('The best accuracy is {}% with alpha value as {}'.format(round(best_accuracy*100, 2), round(alpha_val,1)))
acc_s = accuracy_score(y_test, y_pred)*100
print("Accuracy Score {} %".format(round(acc_s,2)))<jupyter_output>Accuracy Score 98.57 %<jupyter_text>Naive Bayes<jupyter_code>classifier = MultinomialNB(alpha=0.1)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
y_pred<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>def predict_spam(sample_message):
    sample_message = re.sub(pattern='[^a-zA-Z]',repl=' ', string = sample_message)
    sample_message = sample_message.lower()
    sample_message_words = sample_message.split()
    sample_message_words = [word for word in sample_message_words if not word in set(stopwords.words('english'))]
    ps = PorterStemmer()
    final_message = [ps.stem(word) for word in sample_message_words]
    final_message = ' '.join(final_message)
    temp = cv.transform([final_message]).toarray()
    return classifier.predict(temp)
result = ['SPAM!','HAM']
msg = "Hi! kindly submit your assignment"

if predict_spam(msg):
    print(result[0])
else:
    print(result[1])
msg = 'Click to invest now:'
if predict_spam(msg):
    print(result[0])
else:
    print(result[1])<jupyter_output>SPAM!
{'fraction_non_alphanumeric': 0.14126490409538622, 'fraction_numerical': 0.011923276308968377, 'mean_word_length': 6.507782101167315, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '12593', 'license': 'Unlicense', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1374, 'n_words': 245, 'path': 'Sms_Spam_Classifier (1).ipynb', 'repo_name': 'akshaybaisla2/SmsSpamClassifier', 'size': '3858'}