Sentiment_Analysis

A machine learning model for analyzing text for user sentiment and determine whether its a positive, neutral, or negative review.

View the Project on GitHub rsanchez-dv/Sentiment_Analysis

Sentiment Analysis on Yelp’s Dataset

Author: Roberto Sanchez, Talent Path: D1 Group

Overview

The scope of this capstone is centered around data processing, exploratory data analysis, and training of a model to predict sentiment on user reviews.

End goal of the model

Business Goals

Create a model to be able to be used in generating sentiment on reviews or comments found on external/internal websites to give insights on how people feel about certain topics.

This could give the company insights not easily available on sites where ratings are required or for internal use to determine sentiment on blogs or comments.

Business Applications

By utilizing this model, the business can use it for the following purposes:

External:

Internal:

Current methods of achieving these are using outside resources which come at a cost and increases the risk of leaking sensitive data to the public. This product will bypass these outside resources and give the company the ability to do it in-house.

Model Deployment

Link: Review Analyzer

After running multiple models and comparing accuracy, I found that the LinearSVC model is a viable candidate to be used in production for analyzing reviews of services or food.

Classification Report / Confusion Matrix:

Classification Report

Technology Stack

I have been using these technologies for this project:

Data Processing

This capstone uses the Yelp dataset found on Kaggle which comprises of multiple files:

Stage 1 - Read in From JSON files into Postgres

Overview

As stated above, Kaggle provided several JSON files with a large amount of data that needed to be stored in a location for easy access and provide a quick way to query data on the fly. As the files were read in Jupyter notebook a general observation was made to the feature names and amount each file contained to see what data I was dealing with along with the types associated with them. The business data contained a strange number of attributes that had to be broken up into separate data frames to be normalized for Postgres.

Stage 2 - Pre-Processing Data

Overview

In this stage, I performed exploratory data analysis where I analyze any null values, see the distribution of my ratings, and review lengths.

Stage 3 - Cleaning Up Data

Overview

Exploratory Data Analysis

Analyzing Null Values in Dataset

Below is a visualization of the data provided by Kaggle showing which features have “NaN “ values. It is clear that the review ratings (review_stars) and reviews (text) are fully populated. Some of the business attributes are sparse but have enough values to be useful for other things. Note several other features were dropped in the Data Processing since they did not provide any insights for the scope of this project.

Heatmap of several million rows of data.

Looking Closer at the Ratings (review_stars)

This is a sample of 2 million rows from the original 8 million in the dataset. This distribution of ratings has a left skew on it where most of the reviews are 4 to 5 stars.

A bar graph showing the distribution of ratings between 1 to 5. there is a significant amount of 5 stars compared to 1-3 combined.

I simplified the ratings to better categorize the sentiment of the review by grouping 1 and 2 star reviews as ‘negative’, 3 star review as ‘neutral’, and 4 and 5 star reviews as ‘positive’.

Simplified Barchar showing just the negative, neutral, and positive ratings

Looking Closer at the Reviews (text)

To analyze the text, I’ve calculated the length of each review in the sample and plotted a distribution graph showing them the number of characters of each review. The statistics were that the median review was approx. 606 characters with a range of 0 through 5000 characters.

Showing a distribution chart of the length of the reviews. Clearly the distribution skews right with a median around 400 characters.

A closer inspection on the range 0 - 2000 we can see that most of the reviews are around this general area.

A zoomed in version of the same distribution chart now focusing on 0 - 2000 characters

In order to produce a viable word cloud, I’ve had to process all of the text in the sample to remove special characters and stop words from NLTK to produce a viable string to be used in the word cloud. Below is a visualization of all of the keywords found in the positive reviews.

Created a word cloud from the positive words after cleaning

As expected, words like “perfect”, “great”, “good”, “great place”, and “highly recommend” came out on top.

A word cloud showing all the words from the negative reviews

On the negative word cloud, words like “bad”, “customer service”, “never”, “horrible”, and “awful” are appearing on the word cloud.

Model Training

Model Selection

model selection flow chart

These four models were chosen to be trained with this data. Each of these models had a pipeline created with TfidfVectorizer.

Model Training

Below is the average metrics after running 5 fold cross validation on LinearSVC

average metrics for linearSVC model

Testing Model

After the model was trained, I fed it some reviews I found online to test out whether or not the model can properly detect the right sentiment. The following reviews are ordered as “Negative”, “Neutral”, and “Positive”:

new_test_data = [
    "This was the worst place I've ever eaten at. The staff was rude and did not take my order until after i pulled out my wallet.",
    "It was ok, I guess",
    "I had a pleasent time with kimberly at the granny shack. The food was amazing and very family friendly.",
]
res = model.prediction(new_test_data)

Below is a screenshot of the results when feed into the LinearSVC model for predictions.Results from the prediction

End Notes

There are some improvements to be made such as the following: