<jupyter_start><jupyter_text>Import necessary classes<jupyter_code>from keras.preprocessing.text import Tokenizer
from keras.models import Sequential
from keras import layers
from keras.preprocessing.sequence import pad_sequences

import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>Define your variables and parameters<jupyter_code>epochs = 20
maxlen = 100
embedding_dim = 50
num_filters = 64
kernel_size = 5
batch_size = 32<jupyter_output><empty_output><jupyter_text>Import the data.<jupyter_code>#If you are using Google CO-Lab,use the code below to import the file into your notebook. 
#PS: Ensure you have downloaded the data from the UCI Machine Learning Repository: https://archive.ics.uci.edu/ml/datasets/Sentiment+Labelled+Sentences
from google.colab import files
import io
uploaded = files.upload()
url = io.BytesIO(uploaded['yelp_labelled.txt'])

#If you are loading this file from a local directory, specify the path
#url = 'data/sentiment labelled sentences/yelp_labelled.txt'

#import the data into a data frame
data = pd.read_csv(url,names=['sentence', 'label'], sep='\t')
data.head()<jupyter_output><empty_output><jupyter_text>Select the ‘sentence’ and ‘label’ columns<jupyter_code>sentences=data['sentence'].values
labels=data['label'].values<jupyter_output><empty_output><jupyter_text>Split your data into training and test set<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
    sentences, labels, test_size=0.30, random_state=1000)<jupyter_output><empty_output><jupyter_text>Tokenize<jupyter_code>tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(X_train)
X_train = tokenizer.texts_to_sequences(X_train)
X_test = tokenizer.texts_to_sequences(X_test)
vocab_size = len(tokenizer.word_index) + 1 #The vocabulary size has an additional 1 due to the 0 reserved index<jupyter_output><empty_output><jupyter_text>Pad in order to ensure that all sequences have the same length<jupyter_code>X_train = pad_sequences(X_train, padding='post', maxlen=maxlen)
X_test = pad_sequences(X_test, padding='post', maxlen=maxlen)<jupyter_output><empty_output><jupyter_text>Create the model. Note that we use  a sigmoid activation function on the last layer and the binary cross entropy for calculating loss. This is because we are doing a binary classification.<jupyter_code>model = Sequential()
model.add(layers.Embedding(vocab_size, embedding_dim, input_length=maxlen))
model.add(layers.Conv1D(num_filters, kernel_size, activation='relu'))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(10, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])
model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_2 (Embedding)      (None, 100, 50)           84900     
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 96, 64)            16064     
_________________________________________________________________
global_max_pooling1d_2 (Glob (None, 64)                0         
_________________________________________________________________
dense_3 (Dense)              (None, 10)                650       
_________________________________________________________________
dense_4 (Dense)              (None, 1)                 11        
=================================================================
Total params: 101,625
Trainable params: 101,625
Non-trainable params: 0
_________________________________________________________________<jupyter_text>Train and test the model<jupyter_code>model.fit(X_train, y_train,
                    epochs=epochs,
                    verbose=False,
                    validation_data=(X_test, y_test),
                    batch_size=batch_size)
loss, accuracy = model.evaluate(X_train, y_train, verbose=False)
print("Training Accuracy: {:.4f}".format(accuracy))
loss, accuracy = model.evaluate(X_test, y_test, verbose=False)
print("Testing Accuracy:  {:.4f}".format(accuracy))<jupyter_output>Training Accuracy: 1.0000
Testing Accuracy:  0.8167
{'fraction_non_alphanumeric': 0.11632699793908861, 'fraction_numerical': 0.021525074421799864, 'mean_word_length': 5.451994091580502, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '323607', 'license': 'MIT', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1245, 'n_words': 296, 'path': 'Lesson 04/Activity_5_Sentiment_analysis_on_real_life_dataset (1).ipynb', 'repo_name': 'TrainingByPackt/Deep-Learning-for-Natural-Language-Processing', 'size': '4367'}