<jupyter_start><jupyter_text>Importing The Required Libraries<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
from collections import defaultdict
from collections import  Counter
plt.style.use('ggplot')
stop=set(stopwords.words('english'))
import re
from nltk.tokenize import word_tokenize
import gensim
import string
import tensorflow as tf
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.regularizers import l1
from tqdm import tqdm
from keras.models import Sequential
from keras.layers import Embedding,LSTM,Dense,SpatialDropout1D
from keras.initializers import Constant
from sklearn.model_selection import train_test_split
from keras.optimizers import Adam
from sklearn.metrics import accuracy_score 
import numpy as np
from time import time
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))<jupyter_output>/kaggle/input/glove-global-vectors-for-word-representation/glove.6B.50d.txt
/kaggle/input/glove-global-vectors-for-word-representation/glove.6B.200d.txt
/kaggle/input/glove-global-vectors-for-word-representation/glove.6B.100d.txt
/kaggle/input/nlp-getting-started/train.csv
/kaggle/input/nlp-getting-started/test.csv
/kaggle/input/nlp-getting-started/sample_submission.csv
/kaggle/input/sms-spam-collection-dataset/spam.csv<jupyter_text>Loading The Dataset.<jupyter_code>data= pd.read_csv('/kaggle/input/sms-spam-collection-dataset/spam.csv', encoding='latin-1')
data<jupyter_output><empty_output><jupyter_text>We are only going to use the first two columns also we are going to replace spam with 1 and ham with 0 in column 1.<jupyter_code>data=data.iloc[:,:2]
data['message_len'] = data.v2.apply(len)
data['v1']=data['v1'].replace({'ham':0,'spam':1})
data.head()<jupyter_output>/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  This is separate from the ipykernel package so we can avoid doing imports until<jupyter_text>Lets Do Some EDA. Lets checkout the message length whether the sms is a spam or not.<jupyter_code>plt.figure(figsize=(12, 8))

data[data.v1==0].message_len.plot(bins=35, kind='hist', color='blue', 
                                       label='Ham messages', alpha=0.6)
data[data.v1==1].message_len.plot(kind='hist', color='red', 
                                       label='Spam messages', alpha=0.6)
plt.legend()
plt.xlabel("Message Length")<jupyter_output><empty_output><jupyter_text>Text Preprocessing.<jupyter_code>df=data.iloc[:,:2]
df.head()
def clean_text(text):
 
    text = re.sub('[^a-zA-Z]', ' ', text)  

    text = text.lower()  

    text = text.split(' ')  
    
    text = [w for w in text if not w in set(stopwords.words('english'))] 

    text = ' '.join(text)    
            
    return text



df['v2'] = df['v2'].apply(lambda x : clean_text(x))
df.head()<jupyter_output><empty_output><jupyter_text>GloVe for Vectorization Here we will use GloVe pretrained corpus model to represent our words.It is available in 3 varieties :50D ,100D and 200 Dimentional.We will try 50 D here because the Dataset is small.If you have a larger dataset,you can use 100D or 200D.<jupyter_code>def create_corpus(df):
    corpus=[]
    for tweet in tqdm(df['v2']):
        words=[word.lower() for word in word_tokenize(tweet) if((word.isalpha()==1) & (word not in stop))]
        corpus.append(words)
    return corpus

corpus=create_corpus(df)
embedding_dict={}
with open('/kaggle/input/glove-global-vectors-for-word-representation/glove.6B.50d.txt','r') as f:
    for line in f:
        values=line.split()
        word=values[0]
        vectors=np.asarray(values[1:],'float32')
        embedding_dict[word]=vectors
f.close()
MAX_LEN=10
tokenizer_obj=Tokenizer()
tokenizer_obj.fit_on_texts(corpus)
sequences=tokenizer_obj.texts_to_sequences(corpus)

tweet_pad=pad_sequences(sequences,maxlen=MAX_LEN,truncating='post',padding='post')
word_index=tokenizer_obj.word_index
print('Number of unique words:',len(word_index))
num_words=len(word_index)+1
embedding_matrix=np.zeros((num_words,50))

for word,i in tqdm(word_index.items()):
    if i > num_words:
        continue
    
    emb_vec=embedding_dict.get(word)
    if emb_vec is not None:
        embedding_matrix[i]=emb_vec<jupyter_output>100%|██████████| 7560/7560 [00:00<00:00, 330150.12it/s]<jupyter_text>Splitting The Data into train and validation set.<jupyter_code>X_train,X_val, y_train, y_val = train_test_split(tweet_pad,df.v1, test_size=.2, random_state=2)
print('Shape of train sequences: ',X_train.shape)
print('Shape of train labels: ',y_train.shape)
print("Shape of Validation sequences: ",X_val.shape)
print("Shape of Validation  labels: ",y_val.shape)<jupyter_output>Shape of train sequences:  (4457, 10)
Shape of train labels:  (4457,)
Shape of Validation sequences:  (1115, 10)
Shape of Validation  labels:  (1115,)<jupyter_text>Creating Our Model.<jupyter_code>model=Sequential()

embedding_layer=Embedding(num_words,50,embeddings_initializer=Constant(embedding_matrix),
                   input_length=MAX_LEN,trainable=False)

model.add(embedding_layer)
model.add(SpatialDropout1D(0.2))
model.add(LSTM(64, dropout=0.2, recurrent_dropout=0.2,return_sequences=True))
model.add(tf.keras.layers.LSTM(32,return_sequences=True))
model.add(tf.keras.layers.LSTM(16))
model.add(tf.keras.layers.Dense(16, activation='relu'))

model.add(Dense(1, activation='sigmoid'))
optimzer=Adam(learning_rate=1e-4)
model.compile(loss='binary_crossentropy',optimizer=optimzer,metrics=['acc'])
model.summary()<jupyter_output>Model: "sequential_6"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_6 (Embedding)      (None, 10, 50)            378050    
_________________________________________________________________
spatial_dropout1d_6 (Spatial (None, 10, 50)            0         
_________________________________________________________________
lstm_16 (LSTM)               (None, 10, 64)            29440     
_________________________________________________________________
lstm_17 (LSTM)               (None, 10, 32)            12416     
_________________________________________________________________
lstm_18 (LSTM)               (None, 16)                3136      
_________________________________________________________________
dense_13 (Dense)             (None, 16)                272       
______________________________________________________[...]<jupyter_text>Fitting The Model.<jupyter_code>history=model.fit(X_train,y_train,batch_size=32,epochs=10,validation_data=(X_val,y_val),verbose=1)<jupyter_output>Epoch 1/10
140/140 [==============================] - 8s 58ms/step - loss: 0.6299 - acc: 0.7907 - val_loss: 0.5057 - val_acc: 0.8583
Epoch 2/10
140/140 [==============================] - 7s 52ms/step - loss: 0.4270 - acc: 0.8681 - val_loss: 0.3996 - val_acc: 0.8583
Epoch 3/10
140/140 [==============================] - 7s 53ms/step - loss: 0.3346 - acc: 0.8858 - val_loss: 0.2860 - val_acc: 0.9211
Epoch 4/10
140/140 [==============================] - 7s 53ms/step - loss: 0.2638 - acc: 0.9145 - val_loss: 0.2370 - val_acc: 0.9247
Epoch 5/10
140/140 [==============================] - 8s 54ms/step - loss: 0.2255 - acc: 0.9304 - val_loss: 0.2078 - val_acc: 0.9363
Epoch 6/10
140/140 [==============================] - 7s 53ms/step - loss: 0.2087 - acc: 0.9325 - val_loss: 0.1914 - val_acc: 0.9444
Epoch 7/10
140/140 [==============================] - 8s 59ms/step - loss: 0.1893 - acc: 0.9370 - val_loss: 0.1902 - val_acc: 0.9390
Epoch 8/10
140/140 [==============================] - 8s 56ms/step - [...]<jupyter_text>Creating a Dataframe of Loss And Accuracy.<jupyter_code>model_loss = pd.DataFrame(model.history.history)
model_loss.head()<jupyter_output><empty_output><jupyter_text>Lets visualize the Loss over the Epochs.<jupyter_code>model_loss[['loss','val_loss']].plot(ylim=[0,1])
plt.show()<jupyter_output><empty_output><jupyter_text>Lets visualize the Accuracy over the Epochs.<jupyter_code>model_loss[['acc','val_acc']].plot(ylim=[0,1])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15100410518140464, 'fraction_numerical': 0.049595029401974924, 'mean_word_length': 4.834304207119741, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 2, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '155502', 'license': 'MIT', 'n_tokens_mistral': 3392, 'n_tokens_neox': 3028, 'n_words': 627, 'path': '8_Natural_Language_Processing/Application of NLP/glove-lstm-sentiment-analysis-for-beginners.ipynb', 'repo_name': 'joshuamoorexyz/100DaysofMLCode', 'size': '9013'}