Predicting Amazon product reviews’ ratings | by Bhavesh Patel | Towards Data Science

Classifier algorithms
INTRODUCTION
In machine learning, classification is used to classify a new observation into a specific set/category based on a training set of data containing observations whose category is known in advance. The most common example is “spam” or “non-spam” classes for emails. In E-Commerce, classifier algorithms can be used to classify sentiments of review based on words. The specific words in the language are categorized in advance for their positive or negative sentiments.
Classification is an instance of supervised learning. Training set has correctly identified observations. Classifier algorithms are used to create cluster/sets from the uncategorized unsupervised data based on similarity and/or distance from the training data set.
In simple classifier model, a simple count of positive and negative data points will define overall positive or negative sets. There is a problem with this. For example, in the case of words in sentences, “Great” and “Good” both are positive words. But “Great” has a higher impact than “Good”. We need to train our model to weigh identified data points.
Classifier model boundaries can be a simple line to separate “positive” from “negative” outcome to more complex hyperplane to separate multiple groups.
The validity of the model can be observed using error or accuracy of the model along with “false positive” and “false negatives”.
PythonGraphLabS Frame (similar to Pandas Dataframe)
Python
GraphLab
S Frame (similar to Pandas Dataframe)
We are going to classify Amazon product reviews to understand the positive or negative review. Amazon has 5-star rating. We will use that to compare our prediction.
> product_reviews=graphlab.SFrame(‘amazon_baby.gl/’)> product_reviews.head()
> product_reviews.show()
Create a dictionary of words, which will be used my model to classify.
> product_reviews[‘wordcount’] = graphlab.text_analytics.count_words(product_reviews[‘review’])
Select one specific product to predict the sentiment of the reviews. Vulli Shopie is a giraffe toy for baby teething. Filter all reviews for the product.
> vs_reviews = product_reviews[product_reviews[‘name’]==’Vulli Sophie the Giraffe Teether’]> vs_reviews[‘rating’].show(view=’Categorical’)
Filter out the middle rating of 3. Rating higher than 4 means it is positive rating.
> product_reviews = product_reviews[product_reviews[‘rating’] !=3]> product_reviews[‘binrating’] = product_reviews[‘rating’] >= 4
Create training and test data set.
> train_data, test_data = product_reviews.random_split(0.8, seed=0)
Build a sentiment classifier to identify whether the review has positive or negative sentiment. Logistic Classifier model will use the words (wordcount column) and ratings (binrating) from the training data to develop a model to predict target (binrating).
> sentiment_model = graphlab.logistic_classifier.create (train_data,                    target=’binrating’,                    features=[‘wordcount’],                    validation_set=test_data)
Evaluate the model for the test data and display using ROC (Receiver Operating Characteristic) curve.
> sentiment_model.evaluate(test_data, metric=’roc_curve’)
> sentiment_model.show(view=’Evaluation’)
91% accuracy is good.
Using the model we built, predict each review sentiment for Vullie Sophie Giraffe toy.
> vs_reviews[‘predicted_sentiment_by_model’]=sentiment_model.predict(vs_reviews, output_type=’probability’)
Our model has worked very well. For higher number of sentiment (closer to 1), we can observe that Amazon product star rating is 5.
We can view the most positive and negative review based on predicted sentiment from the model.
> vs_reviews=vs_reviews.sort(‘predicted_sentiment_by_model’, ascending=False)> vs_reviews[0][‘review’]
“Sophie, oh Sophie, your time has come. My granddaughter, Violet is 5 months old and starting to teeth. What joy little Sophie brings to Violet. Sophie is made of a very pliable rubber that is sturdy but not tough. It is quite easy for Violet to twist Sophie into unheard of positions to get Sophie into her mouth. The little nose and hooves fit perfectly into small mouths, and the drooling has purpose. The paint on Sophie is food quality.Sophie was born in 1961 in France. The maker had wondered why there was nothing available for babies and made Sophie from the finest rubber, phthalate-free on St Sophie’s Day, thus the name was born. Since that time millions of Sophie’s populate the world. She is soft and for babies little hands easy to grasp. Violet especially loves the bumpy head and horns of Sophie. Sophie has a long neck that easy to grasp and twist. She has lovely, sizable spots that attract Violet’s attention. Sophie has happy little squeaks that bring squeals of delight from Violet. She is able to make Sophie squeak and that brings much joy. Sophie’s smooth skin is soothing to Violet’s little gums. Sophie is 7 inches tall and is the exact correct size for babies to hold and love.As you well know the first thing babies grasp, goes into their mouths- how wonderful to have a toy that stimulates all of the senses and helps with the issue of teething. Sophie is small enough to fit into any size pocket or bag. Sophie is the perfect find for babies from a few months to a year old. How wonderful to hear the giggles and laughs that emanate from babies who find Sophie irresistible. Viva La Sophie!Highly Recommended. prisrob 12–11–09”
Most negative review.
> vs_reviews[-1][‘review’] # most negative
“My son (now 2.5) LOVED his Sophie, and I bought one for every baby shower I’ve gone to. Now, my daughter (6 months) just today nearly choked on it and I will never give it to her again. Had I not been within hearing range it could have been fatal. The strange sound she was making caught my attention and when I went to her and found the front curved leg shoved well down her throat and her face a purply/blue I panicked. I pulled it out and she vomited all over the carpet before screaming her head off. I can’t believe how my opinion of this toy has changed from a must-have to a must-not-use. Please don’t disregard any of the choking hazard comments, they are not over exaggerated!”