<jupyter_start><jupyter_text>This program detects if an email/message is spam (1) or not (0)Import Libraries<jupyter_code>#import libraries
import numpy as np
import pandas as pd
import nltk
from nltk.corpus import stopwords
import string<jupyter_output><empty_output><jupyter_text>Load the data<jupyter_code>#Load the data
from google.colab import files
uploaded = files.upload()<jupyter_output><empty_output><jupyter_text>Read the CSV file<jupyter_code>#Read the CSV file
df = pd.read_csv('emails.csv')<jupyter_output><empty_output><jupyter_text>Print the first five rows of data<jupyter_code>#Print first five rows of dataset
df.head(5)<jupyter_output><empty_output><jupyter_text>Print the shape of the dataset(Get the number of rows and columns)<jupyter_code>#Print the number of rows and columns
df.shape<jupyter_output><empty_output><jupyter_text>Get the column names<jupyter_code>#Print the column names
df.columns<jupyter_output><empty_output><jupyter_text>Check for duplicates and remove them<jupyter_code>#Remove duplicates if any
df.drop_duplicates(inplace = True)<jupyter_output><empty_output><jupyter_text>Show the new shape (number of rows and columns)<jupyter_code>#Show the new shape
df.shape<jupyter_output><empty_output><jupyter_text>Show the number of missing data (if any) for each column<jupyter_code>#Show the number of missing data
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Download the stopwords package<jupyter_code>#Download the stopwords package
nltk.download('stopwords')<jupyter_output>[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!<jupyter_text>Processing the input for a cleaner Text<jupyter_code>#Process Text
def process_text(text):
  #1 Remove punctuation
  #2 Remove Stopwords
  #3 Return a list of clean text words

  #1 
  nopunc = [char for char in text if char not in string.punctuation]
  nopunc = ''.join(nopunc)

  #2
  clean_words = [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]

  #3
  return clean_words
#Show the tokenisation(a list of tokens also called lemmas)
df['text'].head().apply(process_text)<jupyter_output><empty_output><jupyter_text>Convert a collection of text to a matrix of tokens<jupyter_code>#Convert a collection of text to a matrix of tokens
from sklearn.feature_extraction.text import CountVectorizer
messages_bow = CountVectorizer(analyzer = process_text).fit_transform(df['text'])<jupyter_output><empty_output><jupyter_text>Split the data into 80% training and 20% testing<jupyter_code>#Split the data into 80% training data and 20 % testing data
from sklearn.model_selection import train_test_split
X_train , X_test , y_train , y_test  = train_test_split(messages_bow , df['spam'], test_size =0.2,random_state =0)<jupyter_output><empty_output><jupyter_text>Training the model<jupyter_code>#Create and train Naive-Bayes Classifier
from sklearn.naive_bayes import MultinomialNB
classifier = MultinomialNB().fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>Printing the predicitons<jupyter_code>#Print the predictions
print(classifier.predict(X_train))

#Print the actual values
print(y_train.values)<jupyter_output>[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]<jupyter_text>Evaluate model on the training data set<jupyter_code>#Evaluate model on training dataset
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
pred= classifier.predict(X_train)
print(classification_report(y_train,pred))
print()
print('Confusion Matrix:\n', confusion_matrix(y_train,pred))
print()
print('Accuracy : ', accuracy_score(y_train,pred))<jupyter_output>precision    recall  f1-score   support

           0       1.00      1.00      1.00      3457
           1       0.99      1.00      0.99      1099

    accuracy                           1.00      4556
   macro avg       0.99      1.00      1.00      4556
weighted avg       1.00      1.00      1.00      4556


Confusion Matrix:
 [[3445   12]
 [   1 1098]]

Accuracy :  0.9971466198419666<jupyter_text>Evaluating model on testing dataset<jupyter_code>#Print the predictions
print(classifier.predict(X_test))

#Print the actual values
print(y_test.values)
#Evaluate model on testing dataset
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
pred= classifier.predict(X_test)
print(classification_report(y_test,pred))
print()
print('Confusion Matrix:\n', confusion_matrix(y_test,pred))
print()
print('Accuracy : ', accuracy_score(y_test,pred))<jupyter_output>precision    recall  f1-score   support

           0       1.00      0.99      0.99       870
           1       0.97      1.00      0.98       269

    accuracy                           0.99      1139
   macro avg       0.98      0.99      0.99      1139
weighted avg       0.99      0.99      0.99      1139


Confusion Matrix:
 [[862   8]
 [  1 268]]

Accuracy :  0.9920983318700615
{'fraction_non_alphanumeric': 0.08853850818677987, 'fraction_numerical': 0.04184354154032747, 'mean_word_length': 4.597285067873303, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '82715', 'license': 'MIT', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1633, 'n_words': 423, 'path': 'Machine Learning/Applications/Streamlit/Spam_Detector_Model/Spam_Detector_Codex_MAIT.ipynb', 'repo_name': 'Akshat-Batra/Ds-Algo-HacktoberFest', 'size': '4947'}