Sentiment Analysis On Amazon Food Reviews: From EDA To Deployment
By: Arun Mohan
Tags: Machine Learning, Sentiment Analysis, Flask, Deeplearing, Deployment

Exploratory Data Analysis

Basic Preprocessing

As a step of basic data cleaning, we first checked for any missing values. Fortunately, we don’t have any missing values. Next, we will check for duplicate entries. On analysis, we found that for different products the same review is given by the same user at the same time. Practically it doesn’t make sense. So we will keep only the first one and remove other duplicates.

Example for a duplicate entry:

data

Now our data points got reduced to about 69%.

Analyzing the review trend

review trend

From 2001 to 2006 the number of reviews is consistent. But after that, the number of reviews began to increase. Out of those, a number of reviews with 5-star ratings were high. Maybe that are unverified accounts boosting the seller inappropriately with fake reviews. The other reason can be due to an increase in the number of user accounts.

Analyzing target variable

As discussed earlier we will assign all data points above rating 3 as positive class and below 3 as a negative class. we will neglect the rest of the points.

target

Observation: It is clear that we have an imbalanced data set for classification. So We cannot choose accuracy as a metric. So here we will go with AUC(Area under ROC curve)

Why accuracy not for imbalanced datasets?

Consider a scenario like this where we have an imbalanced data set. For example, consider the case of credit card fraud detection with 98% percentage of points as non-fraud(1) and rest 2% points as fraud(1). In such cases even if we predict all the points as non-fraud also we will get 98% accuracy. But actually it is not the case. So we can’t use accuracy as a metric.

What is AUC ROC?

AUC is the area under the ROC curve. It tells how much the model is capable of distinguishing between classes. Higher the AUC, the better the model is at predicting 0s as 0s and 1s as 1s. The ROC curve is plotted with TPR against the FPR where TPR is on the y-axis and FPR is on the x-axis.

Analyzing User behavior

user behavior

After analyzing the no of products that the user brought, we came to know that most of the users have brought a single product.

Another thing to note is that the helpfulness denominator should be always greater than the numerator as the helpfulness numerator is the number of users who found the review helpful and the helpfulness denominator is the number of users who indicated whether they found the review helpful or not. There are some data points that violate this. So we remove those points.

After our preprocessing, data got reduced from 568454 to 364162.ie, about 64% of the data is remaining. Now let's get into our important part. Processing review data.

Preprocessing text data

Text data requires some preprocessing before we go on further with analysis and making the prediction model. Hence in the preprocessing phase, we do the following in the order below:-

Begin by removing the Html tags.

Remove any punctuation’s or a limited set of special characters like, or . or #,! etc.

Check if the word is made up of English letters and is not alpha-numeric

Convert the word to lowercase

Finally, remove Stopwords

Train test split

Once we are done with preprocessing, we will split our data into train and test. We will do splitting after sorting the data based on time as a change in time can influence the reviews.

Vectorizing text data

After that, I have applied bow vectorization, tfidf vectorization, average word2vec, and tfidf word2vec techniques for featuring our text and saved them as separate vectors. As vectorizing large amounts of data is expensive, I computed it once and stored so that I do not want to recompute it again and again.

Note: I used a unigram approach for a bag of words and tfidf. In the case of word2vec, I trained the model rather than using pre-trained weights. You can always try with an n-gram approach for bow/tfidf and can use pre-trained embeddings in the case of word2vec.

You should always try to fit your model on train data and transform it on test data. Do not try to fit your vectorizer on test data as it can cause data leakage issues.

TSNE visualization

TSNE which stands for t-distributed stochastic neighbor embedding is one of the most popular dimensional reduction techniques. It is mainly used for visualizing in lower dimensions. Before getting into machine learning models, I tried to visualize it at a lower dimension.

TSNE

Steps I followed for TSNE:

Keeping perplexity constant I ran TSNE at different iterations and found the most stable iteration.

Now keeping that iteration constant I ran TSNE at different perplexity to get a better result.

Once I got the stable result, ran TSNE again with the same parameters.

But I found that TSNE is not able to well separate the points in a lower dimension.

Note: I tried TSNE with random 20000 points (with equal class distribution). May results improve with a large number of datapoints