Analyzing Amazon Product Sentiment

Published Sun 21 February 2016 in GraphLab

by Prashant Gonarkar python

In this article, we focused on classifiers, applying them to analyzing product sentiment, and understanding the types of errors a classifier makes.Further in IPython notebook we are going to explore this application further, training a sentiment analysis model using a set of key polarizing words, verify the weights learned to each of these words, and compare the results of this simpler classifier with those of the one using all of the words.

# importing GraphLab
import graphlab

Read some product review data

# dataset in CSV format https://d396qusza40orc.cloudfront.net/phoenixassets/amazon_baby.csv
products = graphlab.SFrame('coursera-notebooks/course-1/amazon_baby.gl')
[INFO] This non-commercial license of GraphLab Create is assigned to prashantgonarkar@gmail.com and will expire on February 13, 2017. For commercial licensing options, visit https://dato.com/buy/.

[INFO] Start server at: ipc:///tmp/graphlab_server-1172 - Server binary: /usr/local/lib/python2.7/dist-packages/graphlab/unity_server - Server log: /tmp/graphlab_server_1455978510.log
[INFO] GraphLab Server Version: 1.8

Lets explore this data together

products.head()
name review rating
Planetwise Flannel Wipes These flannel wipes are
OK, but in my opinion ...
3.0
Planetwise Wipe Pouch it came early and was not
disappointed. i love ...
5.0
Annas Dream Full Quilt
with 2 Shams ...
Very soft and comfortable
and warmer than it ...
5.0
Stop Pacifier Sucking
without tears with ...
This is a product well
worth the purchase. I ...
5.0
Stop Pacifier Sucking
without tears with ...
All of my kids have cried
non-stop when I tried to ...
5.0
Stop Pacifier Sucking
without tears with ...
When the Binky Fairy came
to our house, we didn't ...
5.0
A Tale of Baby's Days
with Peter Rabbit ...
Lovely book, it's bound
tightly so you may no ...
4.0
Baby Tracker® - Daily
Childcare Journal, ...
Perfect for new parents.
We were able to keep ...
5.0
Baby Tracker® - Daily
Childcare Journal, ...
A friend of mine pinned
this product on Pinte ...
5.0
Baby Tracker® - Daily
Childcare Journal, ...
This has been an easy way
for my nanny to record ...
4.0
[10 rows x 3 columns]
products['name' == 'Planetwise Flannel Wipes']
{'name': 'Planetwise Flannel Wipes',
 'rating': 3.0,
 'review': 'These flannel wipes are OK, but in my opinion not worth keeping.  I also ordered someImse Vimse Cloth Wipes-Ocean Blue-12 countwhich are larger, had a nicer, softer texture and just seemed higher quality.  I use cloth wipes for hands and faces and have been usingThirsties 6 Pack Fab Wipes, Boyfor about 8 months now and need to replace them because they are starting to get rough and have had stink issues for a while that stripping no longer handles.'}

# Build word count vector for each review

products['word_count'] = graphlab.text_analytics.count_words(products['review'])
products.head()
name review rating word_count
Planetwise Flannel Wipes These flannel wipes are
OK, but in my opinion ...
3.0 {'and': 5, 'stink': 1,
'because': 1, 'ordered': ...
Planetwise Wipe Pouch it came early and was not
disappointed. i love ...
5.0 {'and': 3, 'love': 1,
'it': 2, 'highly': 1, ...
Annas Dream Full Quilt
with 2 Shams ...
Very soft and comfortable
and warmer than it ...
5.0 {'and': 2, 'quilt': 1,
'it': 1, 'comfortable': ...
Stop Pacifier Sucking
without tears with ...
This is a product well
worth the purchase. I ...
5.0 {'ingenious': 1, 'and':
3, 'love': 2, ...
Stop Pacifier Sucking
without tears with ...
All of my kids have cried
non-stop when I tried to ...
5.0 {'and': 2, 'parents!!':
1, 'all': 2, 'puppet.': ...
Stop Pacifier Sucking
without tears with ...
When the Binky Fairy came
to our house, we didn't ...
5.0 {'and': 2, 'cute': 1,
'help': 2, 'doll': 1, ...
A Tale of Baby's Days
with Peter Rabbit ...
Lovely book, it's bound
tightly so you may no ...
4.0 {'shop': 1, 'be': 1,
'is': 1, 'it': 1, 'as': ...
Baby Tracker® - Daily
Childcare Journal, ...
Perfect for new parents.
We were able to keep ...
5.0 {'feeding,': 1, 'and': 2,
'all': 1, 'right': 1, ...
Baby Tracker® - Daily
Childcare Journal, ...
A friend of mine pinned
this product on Pinte ...
5.0 {'and': 1, 'help': 1,
'give': 1, 'is': 1, ...
Baby Tracker® - Daily
Childcare Journal, ...
This has been an easy way
for my nanny to record ...
4.0 {'journal.': 1, 'all': 1,
'standarad': 1, ...
[10 rows x 4 columns]
products['name' == 'Planetwise Flannel Wipes']
{'name': 'Planetwise Flannel Wipes',
 'rating': 3.0,
 'review': 'These flannel wipes are OK, but in my opinion not worth keeping.  I also ordered someImse Vimse Cloth Wipes-Ocean Blue-12 countwhich are larger, had a nicer, softer texture and just seemed higher quality.  I use cloth wipes for hands and faces and have been usingThirsties 6 Pack Fab Wipes, Boyfor about 8 months now and need to replace them because they are starting to get rough and have had stink issues for a while that stripping no longer handles.',
 'word_count': {'6': 1,
  '8': 1,
  'a': 2,
  'about': 1,
  'also': 1,
  'and': 5,
  'are': 3,
  'because': 1,
  'been': 1,
  'blue-12': 1,
  'boyfor': 1,
  'but': 1,
  'cloth': 2,
  'countwhich': 1,
  'fab': 1,
  'faces': 1,
  'flannel': 1,
  'for': 2,
  'get': 1,
  'had': 2,
  'handles.': 1,
  'hands': 1,
  'have': 2,
  'higher': 1,
  'i': 2,
  'in': 1,
  'issues': 1,
  'just': 1,
  'keeping.': 1,
  'larger,': 1,
  'longer': 1,
  'months': 1,
  'my': 1,
  'need': 1,
  'nicer,': 1,
  'no': 1,
  'not': 1,
  'now': 1,
  'ok,': 1,
  'opinion': 1,
  'ordered': 1,
  'pack': 1,
  'quality.': 1,
  'replace': 1,
  'rough': 1,
  'seemed': 1,
  'softer': 1,
  'someimse': 1,
  'starting': 1,
  'stink': 1,
  'stripping': 1,
  'texture': 1,
  'that': 1,
  'them': 1,
  'these': 1,
  'they': 1,
  'to': 2,
  'use': 1,
  'usingthirsties': 1,
  'vimse': 1,
  'while': 1,
  'wipes': 2,
  'wipes,': 1,
  'wipes-ocean': 1,
  'worth': 1}}
graphlab.canvas.set_target('ipynb')
products['name'].show()
# Explore Vulli Sophie
giraffe_reviews = products[products['name'] == 'Vulli Sophie the Giraffe Teether']
len(giraffe_reviews)
785
giraffe_reviews['rating'].show(view='Categorical')

Build a sentiment classifiers

products['rating'].show(view='Categorical')

Define what's positive and what's negative sentiment

#ignore all 3 reviews 
products = products[products['rating'] != 3 ]
len(products)
166752
# product sentiment = 4* or 5 * reviews 
products['sentiment'] = products['rating'] >=4
products.head()
name review rating word_count sentiment
Planetwise Wipe Pouch it came early and was not
disappointed. i love ...
5.0 {'and': 3, 'love': 1,
'it': 2, 'highly': 1, ...
1
Annas Dream Full Quilt
with 2 Shams ...
Very soft and comfortable
and warmer than it ...
5.0 {'and': 2, 'quilt': 1,
'it': 1, 'comfortable': ...
1
Stop Pacifier Sucking
without tears with ...
This is a product well
worth the purchase. I ...
5.0 {'ingenious': 1, 'and':
3, 'love': 2, ...
1
Stop Pacifier Sucking
without tears with ...
All of my kids have cried
non-stop when I tried to ...
5.0 {'and': 2, 'parents!!':
1, 'all': 2, 'puppet.': ...
1
Stop Pacifier Sucking
without tears with ...
When the Binky Fairy came
to our house, we didn't ...
5.0 {'and': 2, 'cute': 1,
'help': 2, 'doll': 1, ...
1
A Tale of Baby's Days
with Peter Rabbit ...
Lovely book, it's bound
tightly so you may no ...
4.0 {'shop': 1, 'be': 1,
'is': 1, 'it': 1, 'as': ...
1
Baby Tracker® - Daily
Childcare Journal, ...
Perfect for new parents.
We were able to keep ...
5.0 {'feeding,': 1, 'and': 2,
'all': 1, 'right': 1, ...
1
Baby Tracker® - Daily
Childcare Journal, ...
A friend of mine pinned
this product on Pinte ...
5.0 {'and': 1, 'help': 1,
'give': 1, 'is': 1, ...
1
Baby Tracker® - Daily
Childcare Journal, ...
This has been an easy way
for my nanny to record ...
4.0 {'journal.': 1, 'all': 1,
'standarad': 1, ...
1
Baby Tracker® - Daily
Childcare Journal, ...
I love this journal and
our nanny uses it ...
4.0 {'all': 1, 'forget': 1,
'just': 1, "daughter's": ...
1
[10 rows x 5 columns]

Let's train the sentiment classifier

train_data,test_data = products.random_split(0.8,seed=0)
sentiment_model = graphlab.logistic_classifier.create(train_data,
                                                     target='sentiment',
                                                     features=['word_count'],
                                                     validation_set=test_data)
PROGRESS: Logistic regression:
PROGRESS: --------------------------------------------------------
PROGRESS: Number of examples          : 133448
PROGRESS: Number of classes           : 2
PROGRESS: Number of feature columns   : 1
PROGRESS: Number of unpacked features : 219217
PROGRESS: Number of coefficients    : 219218
PROGRESS: Starting L-BFGS
PROGRESS: --------------------------------------------------------
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | Iteration | Passes   | Step size | Elapsed Time | Training-accuracy | Validation-accuracy |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | 1         | 5        | 0.000002  | 3.822951     | 0.841481          | 0.839989            |
PROGRESS: | 2         | 9        | 3.000000  | 6.524749     | 0.947425          | 0.894877            |
PROGRESS: | 3         | 10       | 3.000000  | 7.537581     | 0.923768          | 0.866232            |
PROGRESS: | 4         | 11       | 3.000000  | 8.551528     | 0.971779          | 0.912743            |
PROGRESS: | 5         | 12       | 3.000000  | 9.571802     | 0.975511          | 0.908900            |
PROGRESS: | 6         | 13       | 3.000000  | 10.587506    | 0.899991          | 0.825967            |
PROGRESS: | 7         | 15       | 1.000000  | 12.165112    | 0.984548          | 0.921451            |
PROGRESS: | 8         | 16       | 1.000000  | 13.183553    | 0.985118          | 0.921871            |
PROGRESS: | 9         | 17       | 1.000000  | 14.204816    | 0.987066          | 0.919709            |
PROGRESS: | 10        | 18       | 1.000000  | 15.215279    | 0.988715          | 0.916256            |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: TERMINATED: Iteration limit reached.
PROGRESS: This model may not be optimal. To improve it, consider increasing `max_iterations`.

Evaluate sentiment model

sentiment_model.evaluate(test_data,metric='roc_curve')
{'roc_curve': Columns:
    threshold   float
    fpr float
    tpr float
    p   int
    n   int

 Rows: 100001

 Data:
 +-----------+----------------+----------------+-------+------+
 | threshold |      fpr       |      tpr       |   p   |  n   |
 +-----------+----------------+----------------+-------+------+
 |    0.0    |      1.0       |      1.0       | 27976 | 5328 |
 |   1e-05   | 0.909346846847 | 0.998856162425 | 27976 | 5328 |
 |   2e-05   | 0.896021021021 | 0.998748927652 | 27976 | 5328 |
 |   3e-05   | 0.886448948949 | 0.998462968259 | 27976 | 5328 |
 |   4e-05   | 0.879692192192 | 0.998284243637 | 27976 | 5328 |
 |   5e-05   | 0.875187687688 | 0.998212753789 | 27976 | 5328 |
 |   6e-05   | 0.872184684685 | 0.998177008865 | 27976 | 5328 |
 |   7e-05   | 0.868618618619 | 0.998034029168 | 27976 | 5328 |
 |   8e-05   | 0.864677177177 | 0.997998284244 | 27976 | 5328 |
 |   9e-05   | 0.860735735736 | 0.997962539319 | 27976 | 5328 |
 +-----------+----------------+----------------+-------+------+
 [100001 rows x 5 columns]
 Note: Only the head of the SFrame is printed.
 You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.}
sentiment_model.show(view='Evaluation')

Apply the learned model to understand sentiment for Giraffe

giraffe_reviews['predicted_sentiment'] = sentiment_model.predict(giraffe_reviews,output_type='probability')
giraffe_reviews.head()
name review rating word_count predicted_sentiment
Vulli Sophie the Giraffe
Teether ...
He likes chewing on all
the parts especially the ...
5.0 {'and': 1, 'all': 1,
'because': 1, 'it': 1, ...
0.999513023521
Vulli Sophie the Giraffe
Teether ...
My son loves this toy and
fits great in the diaper ...
5.0 {'and': 1, 'right': 1,
'help': 1, 'just': 1, ...
0.999320678306
Vulli Sophie the Giraffe
Teether ...
There really should be a
large warning on the ...
1.0 {'and': 2, 'all': 1,
'latex.': 1, 'being': 1, ...
0.013558811687
Vulli Sophie the Giraffe
Teether ...
All the moms in my moms'
group got Sophie for ...
5.0 {'and': 2, 'one!': 1,
'all': 1, 'love': 1, ...
0.995769474148
Vulli Sophie the Giraffe
Teether ...
I was a little skeptical
on whether Sophie was ...
5.0 {'and': 3, 'all': 1,
'old': 1, 'her.': 1, ...
0.662374415673
Vulli Sophie the Giraffe
Teether ...
I have been reading about
Sophie and was going ...
5.0 {'and': 6, 'seven': 1,
'already': 1, 'love': 1, ...
0.999997148186
Vulli Sophie the Giraffe
Teether ...
My neice loves her sophie
and has spent hours ...
5.0 {'and': 4, 'drooling,':
1, 'love': 1, 'her.': 1, ...
0.989190989536
Vulli Sophie the Giraffe
Teether ...
What a friendly face!
And those mesmerizing ...
5.0 {'and': 3, 'chew': 1,
"don't": 1, 'is': 1, ...
0.999563518413
Vulli Sophie the Giraffe
Teether ...
We got this just for my
son to chew on instea ...
5.0 {'chew': 2, 'because': 1,
'just': 2, 'what': 1, ...
0.970160542725
Vulli Sophie the Giraffe
Teether ...
My baby seems to like
this toy, but I could ...
3.0 {'and': 2, 'already': 1,
'in': 1, 'some': 1, ' ...
0.195367644588
[10 rows x 5 columns]

sort reviews based on predicted sentiment and explore

giraffe_reviews = giraffe_reviews.sort('predicted_sentiment',ascending=False)
giraffe_reviews.head()
name review rating word_count predicted_sentiment
Vulli Sophie the Giraffe
Teether ...
Sophie, oh Sophie, your
time has come. My ...
5.0 {'giggles': 1, 'all': 1,
"violet's": 2, 'food' ...
1.0
Vulli Sophie the Giraffe
Teether ...
I'm not sure why Sophie
is such a hit with the ...
4.0 {'peace': 1, 'month': 1,
'bright': 1, 'softer' ...
0.999999999703
Vulli Sophie the Giraffe
Teether ...
I'll be honest...I bought
this toy because all the ...
4.0 {'all': 2, 'pops': 1,
'existence.': 1, ...
0.999999999392
Vulli Sophie the Giraffe
Teether ...
We got this little
giraffe as a gift from a ...
5.0 {'all': 2, "don't": 1,
'(literally).so': 1, ...
0.99999999919
Vulli Sophie the Giraffe
Teether ...
As a mother of 16month
old twins; I bought ...
5.0 {'cute': 1, 'all': 1,
'reviews.': 2, 'just' ...
0.999999998657
Vulli Sophie the Giraffe
Teether ...
Sophie the Giraffe is the
perfect teething toy. ...
5.0 {'just': 2, 'both': 1,
'month': 1, 'ears,': 1, ...
0.999999997108
Vulli Sophie the Giraffe
Teether ...
Sophie la giraffe is
absolutely the best toy ...
5.0 {'and': 5, 'the': 1,
'all': 1, 'that': 2, ...
0.999999995589
Vulli Sophie the Giraffe
Teether ...
My 5-mos old son took to
this immediately. The ...
5.0 {'just': 1, 'shape': 2,
'mutt': 1, '"dog': 1, ...
0.999999995573
Vulli Sophie the Giraffe
Teether ...
My nephews and my four
kids all had Sophie in ...
5.0 {'and': 4, 'chew': 1,
'all': 1, 'perfect;': 1, ...
0.999999989527
Vulli Sophie the Giraffe
Teether ...
Never thought I'd see my
son French kissing a ...
5.0 {'giggles': 1, 'all': 1,
'out,': 1, 'over': 1, ...
0.999999985069
[10 rows x 5 columns]
giraffe_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"
giraffe_reviews[1]['review']
"I'm not sure why Sophie is such a hit with the little ones, but my 7 month old baby girl is one of her adoring fans.  The rubber is softer and more pleasant to handle, and my daughter has enjoyed chewing on her legs and the nubs on her head even before she started teething.  She also loves the squeak that Sophie makes when you squeeze her.  Not sure what it is but if Sophie is amongst a pile of her other toys, my daughter will more often than not reach for Sophie.  And I have the peace of mind of knowing that only edible and safe paints and materials have been used to make Sophie, as opposed to Bright Starts and other baby toys made in China.  Now that the research is out on phthalates and other toxic substances in baby toys, I think it's more important than ever to find good quality toys that are also safe for our babies to handle and put in their mouths.  Sophie is a must-have for every new mom in my opinion.  Even if your kid is one of the few that can take or leave her, it's worth a try.  Vulli, the makers of Sophie, also make natural rubber teething rings that my daughter loves as well."

show most negative reviews

giraffe_reviews[-1]['review']
"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!"
giraffe_reviews[-2]['review']
"This children's toy is nostalgic and very cute. However, there is a distinct rubber smell and a very odd taste, yes I tried it, that my baby did not enjoy. Also, if it is soiled it is extremely difficult to clean as the rubber is a kind of porus material and does not clean well. The final thing is the squeaking device inside which stopped working after the first couple of days. I returned this item feeling I had overpaid for a toy that was defective and did not meet my expectations. Please do not be swayed by the cute packaging and hype surounding it as I was. One more thing, I was given a full refund from Amazon without any problem."

Use .apply() to build a new feature with the counts for each of the selected_words

def wordcount(x):
    if 'and' in x:
        return x['and']
    else:
        return 0
temp_products = products.head()
temp_products['awesome'] = temp_products['word_count'].apply(wordcount)
temp_products
name review rating word_count sentiment awesome
Planetwise Wipe Pouch it came early and was not
disappointed. i love ...
5.0 {'and': 3, 'love': 1,
'it': 2, 'highly': 1, ...
1 3
Annas Dream Full Quilt
with 2 Shams ...
Very soft and comfortable
and warmer than it ...
5.0 {'and': 2, 'quilt': 1,
'it': 1, 'comfortable': ...
1 2
Stop Pacifier Sucking
without tears with ...
This is a product well
worth the purchase. I ...
5.0 {'ingenious': 1, 'and':
3, 'love': 2, ...
1 3
Stop Pacifier Sucking
without tears with ...
All of my kids have cried
non-stop when I tried to ...
5.0 {'and': 2, 'parents!!':
1, 'all': 2, 'puppet.': ...
1 2
Stop Pacifier Sucking
without tears with ...
When the Binky Fairy came
to our house, we didn't ...
5.0 {'and': 2, 'cute': 1,
'help': 2, 'doll': 1, ...
1 2
A Tale of Baby's Days
with Peter Rabbit ...
Lovely book, it's bound
tightly so you may no ...
4.0 {'shop': 1, 'be': 1,
'is': 1, 'it': 1, 'as': ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
Perfect for new parents.
We were able to keep ...
5.0 {'feeding,': 1, 'and': 2,
'all': 1, 'right': 1, ...
1 2
Baby Tracker® - Daily
Childcare Journal, ...
A friend of mine pinned
this product on Pinte ...
5.0 {'and': 1, 'help': 1,
'give': 1, 'is': 1, ...
1 1
Baby Tracker® - Daily
Childcare Journal, ...
This has been an easy way
for my nanny to record ...
4.0 {'journal.': 1, 'all': 1,
'standarad': 1, ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
I love this journal and
our nanny uses it ...
4.0 {'all': 1, 'forget': 1,
'just': 1, "daughter's": ...
1 5
[10 rows x 6 columns]

defining lamba function

temp_products['awesome'] = temp_products['word_count'].apply(lambda x: x['it'] if 'it' in x else 0)
temp_products
name review rating word_count sentiment awesome
Planetwise Wipe Pouch it came early and was not
disappointed. i love ...
5.0 {'and': 3, 'love': 1,
'it': 2, 'highly': 1, ...
1 2
Annas Dream Full Quilt
with 2 Shams ...
Very soft and comfortable
and warmer than it ...
5.0 {'and': 2, 'quilt': 1,
'it': 1, 'comfortable': ...
1 1
Stop Pacifier Sucking
without tears with ...
This is a product well
worth the purchase. I ...
5.0 {'ingenious': 1, 'and':
3, 'love': 2, ...
1 1
Stop Pacifier Sucking
without tears with ...
All of my kids have cried
non-stop when I tried to ...
5.0 {'and': 2, 'parents!!':
1, 'all': 2, 'puppet.': ...
1 1
Stop Pacifier Sucking
without tears with ...
When the Binky Fairy came
to our house, we didn't ...
5.0 {'and': 2, 'cute': 1,
'help': 2, 'doll': 1, ...
1 1
A Tale of Baby's Days
with Peter Rabbit ...
Lovely book, it's bound
tightly so you may no ...
4.0 {'shop': 1, 'be': 1,
'is': 1, 'it': 1, 'as': ...
1 1
Baby Tracker® - Daily
Childcare Journal, ...
Perfect for new parents.
We were able to keep ...
5.0 {'feeding,': 1, 'and': 2,
'all': 1, 'right': 1, ...
1 1
Baby Tracker® - Daily
Childcare Journal, ...
A friend of mine pinned
this product on Pinte ...
5.0 {'and': 1, 'help': 1,
'give': 1, 'is': 1, ...
1 2
Baby Tracker® - Daily
Childcare Journal, ...
This has been an easy way
for my nanny to record ...
4.0 {'journal.': 1, 'all': 1,
'standarad': 1, ...
1 1
Baby Tracker® - Daily
Childcare Journal, ...
I love this journal and
our nanny uses it ...
4.0 {'all': 1, 'forget': 1,
'just': 1, "daughter's": ...
1 3
[10 rows x 6 columns]
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
for word in selected_words:
    temp_products[word] = temp_products['word_count'].apply(lambda x: x[word] if word in x else 0)
temp_products
name review rating word_count sentiment awesome
Planetwise Wipe Pouch it came early and was not
disappointed. i love ...
5.0 {'and': 3, 'love': 1,
'it': 2, 'highly': 1, ...
1 0
Annas Dream Full Quilt
with 2 Shams ...
Very soft and comfortable
and warmer than it ...
5.0 {'and': 2, 'quilt': 1,
'it': 1, 'comfortable': ...
1 0
Stop Pacifier Sucking
without tears with ...
This is a product well
worth the purchase. I ...
5.0 {'ingenious': 1, 'and':
3, 'love': 2, ...
1 0
Stop Pacifier Sucking
without tears with ...
All of my kids have cried
non-stop when I tried to ...
5.0 {'and': 2, 'parents!!':
1, 'all': 2, 'puppet.': ...
1 0
Stop Pacifier Sucking
without tears with ...
When the Binky Fairy came
to our house, we didn't ...
5.0 {'and': 2, 'cute': 1,
'help': 2, 'doll': 1, ...
1 0
A Tale of Baby's Days
with Peter Rabbit ...
Lovely book, it's bound
tightly so you may no ...
4.0 {'shop': 1, 'be': 1,
'is': 1, 'it': 1, 'as': ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
Perfect for new parents.
We were able to keep ...
5.0 {'feeding,': 1, 'and': 2,
'all': 1, 'right': 1, ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
A friend of mine pinned
this product on Pinte ...
5.0 {'and': 1, 'help': 1,
'give': 1, 'is': 1, ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
This has been an easy way
for my nanny to record ...
4.0 {'journal.': 1, 'all': 1,
'standarad': 1, ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
I love this journal and
our nanny uses it ...
4.0 {'all': 1, 'forget': 1,
'just': 1, "daughter's": ...
1 0
great fantastic amazing love horrible bad terrible awful wow hate
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0
[10 rows x 16 columns]
for word in selected_words:
    products[word] = products['word_count'].apply(lambda x: x[word] if word in x else 0)
products.head()
name review rating word_count sentiment awesome
Planetwise Wipe Pouch it came early and was not
disappointed. i love ...
5.0 {'and': 3, 'love': 1,
'it': 2, 'highly': 1, ...
1 0
Annas Dream Full Quilt
with 2 Shams ...
Very soft and comfortable
and warmer than it ...
5.0 {'and': 2, 'quilt': 1,
'it': 1, 'comfortable': ...
1 0
Stop Pacifier Sucking
without tears with ...
This is a product well
worth the purchase. I ...
5.0 {'ingenious': 1, 'and':
3, 'love': 2, ...
1 0
Stop Pacifier Sucking
without tears with ...
All of my kids have cried
non-stop when I tried to ...
5.0 {'and': 2, 'parents!!':
1, 'all': 2, 'puppet.': ...
1 0
Stop Pacifier Sucking
without tears with ...
When the Binky Fairy came
to our house, we didn't ...
5.0 {'and': 2, 'cute': 1,
'help': 2, 'doll': 1, ...
1 0
A Tale of Baby's Days
with Peter Rabbit ...
Lovely book, it's bound
tightly so you may no ...
4.0 {'shop': 1, 'be': 1,
'is': 1, 'it': 1, 'as': ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
Perfect for new parents.
We were able to keep ...
5.0 {'feeding,': 1, 'and': 2,
'all': 1, 'right': 1, ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
A friend of mine pinned
this product on Pinte ...
5.0 {'and': 1, 'help': 1,
'give': 1, 'is': 1, ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
This has been an easy way
for my nanny to record ...
4.0 {'journal.': 1, 'all': 1,
'standarad': 1, ...
1 0
Baby Tracker® - Daily
Childcare Journal, ...
I love this journal and
our nanny uses it ...
4.0 {'all': 1, 'forget': 1,
'just': 1, "daughter's": ...
1 0
great fantastic amazing love horrible bad terrible awful wow hate
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0
[10 rows x 16 columns]
len(products)
166752
products['awesome'].sum()
2002
sumlist = []
for word in selected_words:
    temp_var = products[word].sum()
    print word, "occurance",temp_var
    sumlist.append(temp_var)
awesome occurance 2002
great occurance 42420
fantastic occurance 873
amazing occurance 1305
love occurance 40277
horrible occurance 659
bad occurance 3197
terrible occurance 673
awful occurance 345
wow occurance 131
hate occurance 1057
max(sumlist)
42420
min(sumlist)
131

Creating a new sentiment analysis model using only the selected_words as features:

train_data,test_data = products.random_split(.8, seed=0)
selected_words_model = graphlab.logistic_classifier.create(train_data,
                                                     target='sentiment',
                                                     features=selected_words,
                                                     validation_set=test_data)
PROGRESS: Logistic regression:
PROGRESS: --------------------------------------------------------
PROGRESS: Number of examples          : 133448
PROGRESS: Number of classes           : 2
PROGRESS: Number of feature columns   : 11
PROGRESS: Number of unpacked features : 11
PROGRESS: Number of coefficients    : 12
PROGRESS: Starting Newton Method
PROGRESS: --------------------------------------------------------
PROGRESS: +-----------+----------+--------------+-------------------+---------------------+
PROGRESS: | Iteration | Passes   | Elapsed Time | Training-accuracy | Validation-accuracy |
PROGRESS: +-----------+----------+--------------+-------------------+---------------------+
PROGRESS: | 1         | 2        | 0.550229     | 0.844299          | 0.842842            |
PROGRESS: | 2         | 3        | 0.905510     | 0.844186          | 0.842842            |
PROGRESS: | 3         | 4        | 1.255980     | 0.844276          | 0.843142            |
PROGRESS: | 4         | 5        | 1.606155     | 0.844269          | 0.843142            |
PROGRESS: | 5         | 6        | 1.957893     | 0.844269          | 0.843142            |
PROGRESS: | 6         | 7        | 2.308999     | 0.844269          | 0.843142            |
PROGRESS: +-----------+----------+--------------+-------------------+---------------------+
PROGRESS: SUCCESS: Optimal solution found.
PROGRESS:
selected_words_model['coefficients']
name index class value stderr
(intercept) None 1 1.36728315229 0.00861805467825
awesome None 1 1.05800888878 0.110865296265
great None 1 0.883937894898 0.0217379527921
fantastic None 1 0.891303090304 0.154532343591
amazing None 1 0.892802422508 0.127989503231
love None 1 1.39989834302 0.0287147460124
horrible None 1 -1.99651800559 0.0973584169028
bad None 1 -0.985827369929 0.0433603009142
terrible None 1 -2.09049998487 0.0967241912229
awful None 1 -1.76469955631 0.134679803365
[12 rows x 5 columns]
Note: Only the head of the SFrame is printed.
You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.
selected_words_model['coefficients']['value'].sort()
dtype: float
Rows: 12
[-2.090499984872604, -1.9965180055877998, -1.7646995563132146, -1.4091640627563817, -0.9858273699287564, -0.05414501233324819, 0.883937894897966, 0.8913030903043112, 0.8928024225084774, 1.0580088887848158, 1.3672831522930367, 1.3998983430174745]
selected_words_model.evaluate(test_data)
{'accuracy': 0.8431419649291376,
 'auc': 0.6648096413721418,
 'confusion_matrix': Columns:
    target_label    int
    predicted_label int
    count   int

 Rows: 4

 Data:
 +--------------+-----------------+-------+
 | target_label | predicted_label | count |
 +--------------+-----------------+-------+
 |      0       |        0        |  234  |
 |      1       |        0        |  130  |
 |      0       |        1        |  5094 |
 |      1       |        1        | 27846 |
 +--------------+-----------------+-------+
 [4 rows x 3 columns],
 'f1_score': 0.914242563530107,
 'log_loss': 0.40547471103673566,
 'precision': 0.8453551912568306,
 'recall': 0.9953531598513011,
 'roc_curve': Columns:
    threshold   float
    fpr float
    tpr float
    p   int
    n   int

 Rows: 100001

 Data:
 +-----------+-----+-----+-------+------+
 | threshold | fpr | tpr |   p   |  n   |
 +-----------+-----+-----+-------+------+
 |    0.0    | 1.0 | 1.0 | 27976 | 5328 |
 |   1e-05   | 1.0 | 1.0 | 27976 | 5328 |
 |   2e-05   | 1.0 | 1.0 | 27976 | 5328 |
 |   3e-05   | 1.0 | 1.0 | 27976 | 5328 |
 |   4e-05   | 1.0 | 1.0 | 27976 | 5328 |
 |   5e-05   | 1.0 | 1.0 | 27976 | 5328 |
 |   6e-05   | 1.0 | 1.0 | 27976 | 5328 |
 |   7e-05   | 1.0 | 1.0 | 27976 | 5328 |
 |   8e-05   | 1.0 | 1.0 | 27976 | 5328 |
 |   9e-05   | 1.0 | 1.0 | 27976 | 5328 |
 +-----------+-----+-----+-------+------+
 [100001 rows x 5 columns]
 Note: Only the head of the SFrame is printed.
 You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.}
sentiment_model.evaluate(test_data)
{'accuracy': 0.916256305548883,
 'auc': 0.9446492867438502,
 'confusion_matrix': Columns:
    target_label    int
    predicted_label int
    count   int

 Rows: 4

 Data:
 +--------------+-----------------+-------+
 | target_label | predicted_label | count |
 +--------------+-----------------+-------+
 |      1       |        0        |  1461 |
 |      0       |        1        |  1328 |
 |      0       |        0        |  4000 |
 |      1       |        1        | 26515 |
 +--------------+-----------------+-------+
 [4 rows x 3 columns],
 'f1_score': 0.9500349343413533,
 'log_loss': 0.26106698432422115,
 'precision': 0.9523039902309378,
 'recall': 0.9477766657134686,
 'roc_curve': Columns:
    threshold   float
    fpr float
    tpr float
    p   int
    n   int

 Rows: 100001

 Data:
 +-----------+----------------+----------------+-------+------+
 | threshold |      fpr       |      tpr       |   p   |  n   |
 +-----------+----------------+----------------+-------+------+
 |    0.0    |      1.0       |      1.0       | 27976 | 5328 |
 |   1e-05   | 0.909346846847 | 0.998856162425 | 27976 | 5328 |
 |   2e-05   | 0.896021021021 | 0.998748927652 | 27976 | 5328 |
 |   3e-05   | 0.886448948949 | 0.998462968259 | 27976 | 5328 |
 |   4e-05   | 0.879692192192 | 0.998284243637 | 27976 | 5328 |
 |   5e-05   | 0.875187687688 | 0.998212753789 | 27976 | 5328 |
 |   6e-05   | 0.872184684685 | 0.998177008865 | 27976 | 5328 |
 |   7e-05   | 0.868618618619 | 0.998034029168 | 27976 | 5328 |
 |   8e-05   | 0.864677177177 | 0.997998284244 | 27976 | 5328 |
 |   9e-05   | 0.860735735736 | 0.997962539319 | 27976 | 5328 |
 +-----------+----------------+----------------+-------+------+
 [100001 rows x 5 columns]
 Note: Only the head of the SFrame is printed.
 You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.}
products['rating'].show(view='Categorical')
 diaper_champ_reviews = products[products['name'] == 'Baby Trend Diaper Champ']
len(diaper_champ_reviews)
298
diaper_champ_reviews['rating'].show(view='Categorical')

Comparing the accuracy of different sentiment analysis model

diaper_champ_reviews['predicted_sentiment'] = sentiment_model.predict(diaper_champ_reviews,output_type='probability')
diaper_champ_reviews = diaper_champ_reviews.sort('predicted_sentiment',ascending=False)
diaper_champ_reviews.head()
name review rating word_count sentiment awesome
Baby Trend Diaper Champ Baby Luke can turn a
clean diaper to a dirty ...
5.0 {'all': 1, 'less': 1,
"friend's": 1, '(which': ...
1 0
Baby Trend Diaper Champ I LOOOVE this diaper
pail! Its the easies ...
5.0 {'just': 1, 'over': 1,
'rweek': 1, 'sooo': 1, ...
1 0
Baby Trend Diaper Champ We researched all of the
different types of di ...
4.0 {'all': 2, 'just': 4,
"don't": 2, 'one,': 1, ...
1 0
Baby Trend Diaper Champ My baby is now 8 months
and the can has been ...
5.0 {"don't": 1, 'when': 1,
'over': 1, 'soon': 1, ...
1 0
Baby Trend Diaper Champ This is absolutely, by
far, the best diaper ...
5.0 {'just': 3, 'money': 1,
'not': 2, 'mechanism' ...
1 0
Baby Trend Diaper Champ Diaper Champ or Diaper
Genie? That was my ...
5.0 {'all': 1, 'bags.': 1,
'son,': 1, '(i': 1, ...
1 0
Baby Trend Diaper Champ Wow! This is fabulous.
It was a toss-up between ...
5.0 {'and': 4, '"genie".': 1,
'since': 1, 'garbage' ...
1 0
Baby Trend Diaper Champ I originally put this
item on my baby registry ...
5.0 {'lysol': 1, 'all': 2,
'bags.': 1, 'feedback': ...
1 0
Baby Trend Diaper Champ Two girlfriends and two
family members put me ...
5.0 {'just': 1, 'when': 1,
'both': 1, 'results': 1, ...
1 0
Baby Trend Diaper Champ I am one of those super-
critical shoppers who ...
5.0 {'taller': 1, 'bags.': 1,
'just': 1, "don't": 4, ...
1 0
great fantastic amazing love horrible bad terrible awful wow hate predicted_sentiment
0 0 0 0 0 0 0 0 0 0 0.999999937267
0 0 0 1 0 0 0 0 0 0 0.999999917406
0 0 0 0 0 1 0 0 0 0 0.999999899509
2 0 0 0 0 1 0 0 0 0 0.999999836182
0 0 0 2 0 0 0 0 0 0 0.999999824745
0 0 0 0 0 0 0 0 0 0 0.999999759315
0 0 0 0 0 0 0 0 0 0 0.999999692111
0 0 0 0 0 0 0 0 0 0 0.999999642488
0 0 0 0 1 0 0 0 0 0 0.999999604504
0 0 0 1 0 0 0 0 0 0 0.999999486804
[10 rows x 17 columns]
diaper_champ_reviews.head()[0]
{'amazing': 0,
 'awesome': 0,
 'awful': 0,
 'bad': 0,
 'fantastic': 0,
 'great': 0,
 'hate': 0,
 'horrible': 0,
 'love': 0,
 'name': 'Baby Trend Diaper Champ',
 'predicted_sentiment': 0.9999999372669541,
 'rating': 5.0,
 'review': 'Baby Luke can turn a clean diaper to a dirty diaper in 3 seconds flat. The diaper champ turns the smelly diaper into "what diaper smell" in less time than that. I hesitated and wondered what I REALLY needed for the nursery. This is one of the best purchases we made. The champ, the baby bjorn, fluerville diaper bag, and graco pack and play bassinet all vie for the best baby purchase.Great product, easy to use, economical, effective, absolutly fabulous.UpdateI knew that I loved the champ, and useing the diaper genie at a friend\'s house REALLY reinforced that!! There is no comparison, the chanp is easy and smell free, the genie was difficult to use one handed (which is absolutly vital if you have a little one on a changing pad) and there was a deffinite odor eminating from the genieplus we found that the quick tie garbage bags where the ties are integrated into the bag work really well because there isn\'t any added bulk around the sealing edge of the champ.',
 'sentiment': 1,
 'terrible': 0,
 'word_count': {'"what': 1,
  '(which': 1,
  '3': 1,
  'a': 6,
  'absolutly': 2,
  'added': 1,
  'all': 1,
  'and': 6,
  'any': 1,
  'are': 1,
  'around': 1,
  'at': 1,
  'baby': 3,
  'bag': 1,
  'bag,': 1,
  'bags': 1,
  'bassinet': 1,
  'because': 1,
  'best': 2,
  'bjorn,': 1,
  'bulk': 1,
  'can': 1,
  'champ': 1,
  'champ,': 2,
  'champ.': 1,
  'changing': 1,
  'chanp': 1,
  'clean': 1,
  'comparison,': 1,
  'deffinite': 1,
  'diaper': 7,
  'difficult': 1,
  'dirty': 1,
  'easy': 2,
  'economical,': 1,
  'edge': 1,
  'effective,': 1,
  'eminating': 1,
  'fabulous.updatei': 1,
  'flat.': 1,
  'fluerville': 1,
  'for': 2,
  'found': 1,
  'free,': 1,
  "friend's": 1,
  'from': 1,
  'garbage': 1,
  'genie': 2,
  'genieplus': 1,
  'graco': 1,
  'handed': 1,
  'have': 1,
  'hesitated': 1,
  'house': 1,
  'i': 3,
  'if': 1,
  'in': 2,
  'integrated': 1,
  'into': 2,
  'is': 4,
  "isn't": 1,
  'knew': 1,
  'less': 1,
  'little': 1,
  'loved': 1,
  'luke': 1,
  'made.': 1,
  'needed': 1,
  'no': 1,
  'nursery.': 1,
  'odor': 1,
  'of': 2,
  'on': 1,
  'one': 3,
  'pack': 1,
  'pad)': 1,
  'play': 1,
  'product,': 1,
  'purchase.great': 1,
  'purchases': 1,
  'quick': 1,
  'really': 3,
  'reinforced': 1,
  'sealing': 1,
  'seconds': 1,
  'smell': 1,
  'smell"': 1,
  'smelly': 1,
  'than': 1,
  'that': 2,
  'that!!': 1,
  'that.': 1,
  'the': 17,
  'there': 3,
  'this': 1,
  'tie': 1,
  'ties': 1,
  'time': 1,
  'to': 3,
  'turn': 1,
  'turns': 1,
  'use': 1,
  'use,': 1,
  'useing': 1,
  'vie': 1,
  'vital': 1,
  'was': 2,
  'we': 2,
  'well': 1,
  'what': 1,
  'where': 1,
  'wondered': 1,
  'work': 1,
  'you': 1},
 'wow': 0}
diaper_champ_reviews['selected_predicted_sentiment'] = selected_words_model.predict(diaper_champ_reviews,output_type='probability')
diaper_champ_reviews.head()
name review rating word_count sentiment awesome
Baby Trend Diaper Champ Baby Luke can turn a
clean diaper to a dirty ...
5.0 {'all': 1, 'less': 1,
"friend's": 1, '(which': ...
1 0
Baby Trend Diaper Champ I LOOOVE this diaper
pail! Its the easies ...
5.0 {'just': 1, 'over': 1,
'rweek': 1, 'sooo': 1, ...
1 0
Baby Trend Diaper Champ We researched all of the
different types of di ...
4.0 {'all': 2, 'just': 4,
"don't": 2, 'one,': 1, ...
1 0
Baby Trend Diaper Champ My baby is now 8 months
and the can has been ...
5.0 {"don't": 1, 'when': 1,
'over': 1, 'soon': 1, ...
1 0
Baby Trend Diaper Champ This is absolutely, by
far, the best diaper ...
5.0 {'just': 3, 'money': 1,
'not': 2, 'mechanism' ...
1 0
Baby Trend Diaper Champ Diaper Champ or Diaper
Genie? That was my ...
5.0 {'all': 1, 'bags.': 1,
'son,': 1, '(i': 1, ...
1 0
Baby Trend Diaper Champ Wow! This is fabulous.
It was a toss-up between ...
5.0 {'and': 4, '"genie".': 1,
'since': 1, 'garbage' ...
1 0
Baby Trend Diaper Champ I originally put this
item on my baby registry ...
5.0 {'lysol': 1, 'all': 2,
'bags.': 1, 'feedback': ...
1 0
Baby Trend Diaper Champ Two girlfriends and two
family members put me ...
5.0 {'just': 1, 'when': 1,
'both': 1, 'results': 1, ...
1 0
Baby Trend Diaper Champ I am one of those super-
critical shoppers who ...
5.0 {'taller': 1, 'bags.': 1,
'just': 1, "don't": 4, ...
1 0
great fantastic amazing love horrible bad terrible awful wow hate predicted_sentiment
0 0 0 0 0 0 0 0 0 0 0.999999937267
0 0 0 1 0 0 0 0 0 0 0.999999917406
0 0 0 0 0 1 0 0 0 0 0.999999899509
2 0 0 0 0 1 0 0 0 0 0.999999836182
0 0 0 2 0 0 0 0 0 0 0.999999824745
0 0 0 0 0 0 0 0 0 0 0.999999759315
0 0 0 0 0 0 0 0 0 0 0.999999692111
0 0 0 0 0 0 0 0 0 0 0.999999642488
0 0 0 0 1 0 0 0 0 0 0.999999604504
0 0 0 1 0 0 0 0 0 0 0.999999486804
selected_predicted_sentim
ent ...
0.796940851291
0.940876393428
0.5942241719
0.895606298305
0.984739056527
0.796940851291
0.796940851291
0.796940851291
0.347684052736
0.940876393428
[10 rows x 18 columns]



Credits Machine Learning Foundations: A Case Study Approach