<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf 
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.text import Tokenizer 
from tensorflow.keras.preprocessing.sequence import pad_sequences 
Messages= pd.read_csv("https://raw.githubusercontent.com/Portia-dot/DataSet/main/SPAM%20text%20message%2020170820%20-%20Data.csv?token=<KEY>")
Messages.head()
#Get Description of the data in the dataFrame
Messages.groupby("Category").describe().T
import seaborn as sns
plt.figure(figsize=(10,10))
sns.countplot(Messages['Category'])
#Make the data balance
ham_msg = Messages[Messages.Category == 'ham']
spam_msg = Messages[Messages.Category == 'spam']
len(ham_msg), len(spam_msg)
#Randomly take data from the ham_message to match the length of spam messages 
ham_msg = ham_msg.sample(n=len(spam_msg), random_state=42)
len(ham_msg)
ham_msg.shape, spam_msg.shape
balanced_data = ham_msg.append(spam_msg).reset_index(drop=True)
plt.figure(figsize = (8,6))
sns.countplot(balanced_data.Category)
plt.xlabel('Distribution of ham and spam emails messages after cuting size')
plt.ylabel('Message Type')
balanced_data.head()
balanced_data['label'] = balanced_data['Category'].map({'ham': 0, 'spam': 1})
#Split the data using sklearn 

train_msg, test_msg, train_label, test_label = train_test_split(balanced_data['Message'], balanced_data['label'], test_size =0.2, random_state=42)
vocab_size = 500
oov_tok ='<OOV>'
max_len=50
#Preprocessing 
token = Tokenizer(num_words = vocab_size, oov_token=oov_tok)
token.fit_on_texts(train_msg)
word_index = token.word_index
word_index
padding_type = 'post'
truncate_type ='post'
Training_seq = token.texts_to_sequences(train_msg)
Training_pad = pad_sequences(Training_seq, maxlen=50, padding = padding_type, truncating = truncate_type)
Testing_seq = token.texts_to_sequences(test_msg)
Testing_pad = pad_sequences(Testing_seq, maxlen=50, padding = padding_type, truncating = truncate_type)
# Create a model 
model = tf.keras.Sequential([
                             tf.keras.layers.Embedding(vocab_size, 16, input_length=50),
                             tf.keras.layers.GlobalAveragePooling1D(),
                             tf.keras.layers.Dense(32, activation='relu'),
                             tf.keras.layers.Dense(32,),
                             tf.keras.layers.Dropout(0.3),
                             tf.keras.layers.Dense(1, activation='sigmoid')

])

#Compile model 
model.compile(loss = tf.keras.losses.BinaryCrossentropy(from_logits=True),
              optimizer = tf.keras.optimizers.Adam(learning_rate=0.001),
              metrics =['accuracy'])

early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)
history = model.fit(Training_pad, train_label, validation_data=(Testing_pad, test_label), epochs= 30, callbacks=[early_stop])
model.evaluate(Testing_pad, test_label)
#Visualize
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title("model accuracy")
plt.legend()
plt.ylabel('Accuracy')
plt.xlabel('epochs')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12054275796781319, 'fraction_numerical': 0.018933417481855473, 'mean_word_length': 5.049618320610687, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '407640', 'license': 'MIT', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1050, 'n_words': 194, 'path': 'Classification_Problem.ipynb', 'repo_name': 'Portia-dot/API-', 'size': '3206'}