NLP: A Complete Sentiment Classification on Amazon Reviews
By: Ernest Leem
Tags: Sentiment Analysis, NLP, Classification, Text Preprocessing, Machine Learning

What is Sentiment Analysis?

Sentiment Analysis, also known as Opinion Mining, is one of the common research areas which performs Natural Language Processing (NLP) tasks for the purpose to extract subjective information by analyzing text data written by users. In the case of sentiment analysis of review data, the main goal is to identify the user’s subjectivity and classify the statements into different groups of sentiments.

In this article, we aim to perform a sentiment analysis of product reviews written by online users from Amazon. The textual review data comes with numerical rating data, ranging from 1 to 5 (1: negative, 5: positive). This numerical indicator will be used as labels that represent the sentiment of the review text. Thus, this problem will be viewed as a multi-classification process and we seek to predict the sentiment scale of the user reviews based on machine learning classifiers and deep learning algorithms.

Content

About the Dataset

Text Preprocessing & Lemmatization

Word Vectorization (BOW vs. TF-IDF)

Implementing ML Classifiers & Evaluation Metrics

Results

Hyperparameter Tuning

Discussion

Any Future Work?

Required Python Libraries

Dataset

Let’s try downloading the Amazon review dataset by using the link below.

http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/reviews_Electronics_5.json.gz OR to directly download from notebooks,

The raw data is compressed in gzip format, hence extra codes are needed to store the data into pandas DataFrame format. Following the code:

We now have the review data in DataFrame format as shown below. Let’s don't think too much about other features. For this post, we will be only focusing on ‘reviewText’ and ‘overall’ columns, so let’s only keep these columns. ‘reviewText’ contains the raw text of the reviews of a product and the ‘overall’ column is in numerical values ranging from 1 to 5. Here, these values can be seen as sentiment labels: value 1 for ‘Very Negative’, 2 for ‘Somewhat Negative’, 3 for ‘Neutral’, 4 for ‘Somewhat Negative’, and 5 for ‘Very Positive’.

Let’s briefly look at the distribution of the ‘overall’ ratings in our dataset. And as shown, we are observing imbalanced data! This could be harmful to our machine learning classifiers as these algorithms expect an equal number of examples per class to perform properly.

Thus, we apply a simple code that will sort out an equal number of observations for each class.

Now, we have a total of 250,000 rows which contains equal counts for each class. Time to move on to the next step.

What’s Next? Text preprocessing!

For NLP projects, text preprocessing is traditionally a vital step as it has a potential impact on the final performance of our classifier model. Such a process not only reduces the dimensionality of the input data but also converts the input text into a form that is more meaningful or predictable. We’ll go through each step.

Step 1: Fundamental preprocessing tasks

Some fundamental text preprocessing steps include lowercasing, punctuations removal, and removal of stopwords. Stopwords are referred to a set of commonly used words such as ‘to’, ‘from’, and ‘or’. In the NLP approach, these stopwords are perceived as insignificant information, and further, removing stopwords can be beneficial for downsizing the input tokens. A simple function is created to handle this process at once.

Step 2: Lemmatization

Lemmatization is the process of grouping inflected words into a root word. For instance, the word ‘ran’ is lemmatized into ‘run’, ‘crying’ into ‘cry’, and ‘better’ into ‘good’. Although processing time could take a while, lemmatizing is critical for reducing the number of unique words and also, reduce any noise (=unwanted words). Additional function (morphological analysis) is added on top of the lemmatizing function, to first identify and cut down the inflectional forms into a common base word.

Don’t be anxious about the cell running time. As mentioned earlier, lemmatization requires some computation time. Applying step 1 and 2’s functions, now our data looks like this.