Sentiment Analysis With NLP Deep Learning
Sentiment Analysis With NLP Deep Learning
Introduction
The main idea of this article is to help you all understand the concept of Sentiment Analysis Deep Learning
& NLP. Let’s try to understand this with the help of a case. Anirudh owns an e-commerce company-Universal
for the past 1 year and he was very happy as more and more new customers were coming to purchase
through his platform. One day he came to know that one of his friends was not satisfied with the product he
purchased through his platform. He purchased a foldable geared cycle and the parts required for assembly
were missing. He saw few negative reviews by other customers but he purchased from Anirudh as he was his
friend. After listening to his friend, Anirudh decided to deploy a machine-learning algorithm to categorize
user reviews and their sentiments so that his team can understand their customers better and provide the
products and services without any inconvenience to the customers.
Goal: To know what users are saying about products and services. It can help in future decision-making.
2. Use of NLP and Deep Learning to classify the Reviews and find out the Polarity/Sentiment.
His Team labelled the past User Review data. They were reading the Reviews and Classifying them into one
or more categories.
1 indicates the presence of that category. For Example, First Review is talking about usability and its
polarity/sentiment is negative as the user is complaining(indicated by 0) whereas the second review is
talking about features and Functionality having a positive Polarity/Sentiment(indicated by 1)
Anant is the Data Scientist in the company. Now it’s his time to be in the playground. He is using Jupyter
Notebook for the model building.
1 import pandas as pd 2 import numpy as np 3 import matplotlib.pyplot as plt # For Plotting 4 import seaborn
as sns # For Plotting 5 from sklearn.metrics import log_loss # For Model Evaluation 6 from
sklearn.model_selection import RepeatedKFold # For Cross-Validation
Reading data
Observation: By looking at the data, it is clear that it has multiclass(12 classes). It is called a multiclass
problem. Apar t from multiclass a review can have >=1 category. For Example, Id-1 has 2 categories,
Features & Functionality. Categories are not mutually exclusive which is called multilabel. By combining
both, we can say that it is a multiclass, multilabel challenge.
data.info()
data.shape
Exploratory Data Analysis (EDA)
['green', 'red'])
Observation: 22.3 % of users are giving negative reviews. In simple terms, only 1 out 5 users are giving
negative reviews.
Out of total Reviews in different categories, What % of reviews are negative in different categories?
col = ['Components', 'Delivery and Customer Support', 'Design and Aesthetics', 'Dimensions', 'Features',
Before we feed our data as input to the machine learning algorithm, it’s impor tant to prepare the data in
such a way that it reduces the time for processing, takes less memory space, and gives the highest
metric evaluation.
The lower casing is removing capitalization from words so that it is treated the same. For example, Look
& look are considered different as the first one is capitalized.
import re def clean_text(text): text = text.lower() text = re.sub(r"what's", "what is ", text) text =
re.sub(r"'s", " ", text) text = re.sub(r"'ve", " have ", text) text = re.sub(r"can't", "can not ", text) text
= re.sub(r"n't", " not ", text) text = re.sub(r"i'm", "i am ", text) text = re.sub(r"'re", " are ", text)
text = re.sub(r"'d", " would ", text) text = re.sub(r"'ll", " will ", text) text = text.strip(' ') return
text
Stop words are used for grammatical flow and connecting sentences. For example, I ,are, my, me etc. It
does not convey any meaning. If we get rid of stop words, we can reduce the size of our data without
information loss. NLTK library is used here to remove stop words.
!pip install nltk import nltk from nltk.corpus import stopwords stop_words = stopwords.words('english')
data['Review'] = data['Review'].apply(lambda x: ' '.join([word for word in x.split() if word not in
(stop_words)])) data['Review'][0]
y = np.array(data[[ 'Components', 'Delivery and Customer Support', 'Design and Aesthetics', 'Dimensions',
'Features', 'Functionality', 'Installation', 'Material', 'Price', 'Quality', 'Usability','Polarity']]) X =
Deep learning attempts to mimic the human brain, and analysis with deep learning fetches fruitful results
when we implement it in our model. In deep learning, there are at least three hidden layers. Each unit that
takes, processes or outputs the data is called a neuron just like we have in our brain. Based on the work
these neurons do, deep learning neurons are divided into input, hidden & output layers. More Hidden layers
mean a more complex model!
!pip install tensorflow from tensorflow.keras.models import Sequential from tensorflow.keras.layers import
Dense, Activation, Dropout from tensorflow.keras.initializers import RandomNormal def get_model(n_inputs,
print(sum(results_train)/len(results_train) print(sum(results_test)/len(results_test)
Preprocessing
prediction_on_test_data = model.predict(test_vectorised_data)
df_test
Conclusion
Let’s check a few Reviews classification and Polarity Suggested by our Model
Review: Made of very thin cheap metal broke on the very first crimp. Had to rush to a local hardware store
spend 60 more on another because the water was shut off in my home.Did not return because using the case
for the new one.
Review: As good as the brand names, no jams or misfires on my Paslode fuel cell nailer or on my Banks
(HF) nailer.
Different departments now can take actions based on negative reviews in their bucket.
Thus from the above article, it has been lucidly explained as to how we can categorise user reviews and
and study the sentiment analysis with Deep Learning & NLP.
The media shown in this ar ticle is not owned by Analytics Vidhya and are used at the Author’s discretion.
DEVENDRA Maindola