Thomaz Lago Santana

Analyzing Product Review Sentiment

In [2]:
import graphlab
from IPython.display import HTML
graphlab.canvas.set_target('ipynb')
products = graphlab.SFrame('amazon_baby.gl/')
In [3]:
# Create a word count column
products['word_count'] = graphlab.text_analytics.count_words(products['review'])
In [4]:
# View data
products.head()
Out[4]:
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]
In [5]:
# rating of 3 is neutral, remove them
products = products[products['rating'] != 3]
In [6]:
# positive sentiment = 4* or 5* reviews
products['sentiment'] = products['rating'] >=4
In [7]:
# create training and testing data
train_data, test_data = products.random_split(.8, seed=0)
In [8]:
# Train the model
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  | 2.052362     | 0.841481          | 0.839989            |
PROGRESS: | 2         | 9        | 3.000000  | 3.012874     | 0.947425          | 0.894877            |
PROGRESS: | 3         | 10       | 3.000000  | 3.397419     | 0.923768          | 0.866232            |
PROGRESS: | 4         | 11       | 3.000000  | 3.784151     | 0.971779          | 0.912743            |
PROGRESS: | 5         | 12       | 3.000000  | 4.181241     | 0.975511          | 0.908900            |
PROGRESS: | 6         | 13       | 3.000000  | 4.558386     | 0.899991          | 0.825967            |
PROGRESS: | 10        | 18       | 1.000000  | 6.330410     | 0.988715          | 0.916256            |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
In [9]:
# View ROC Curve
sentiment_model.evaluate(test_data, metric='roc_curve')
sentiment_model.show(view='Evaluation')
In [10]:
# add a Probability of being positive column
test_data['predicted_sentiment'] = sentiment_model.predict(test_data, output_type='probability')
In [11]:
def see_predictions(review_id):
    text = test_data[review_id]['review']
    predicted_sentiment = test_data[review_id]['predicted_sentiment']
    return HTML('<b>Text:</b><br><i>%s</i><br><br><b>Probability of being positive:</b> %f %%<hr>' % (text, predicted_sentiment))

Sample Results

In [12]:
see_predictions(0)
Out[12]:
Text:
Space for monthly photos, info and a lot of useful stickers come with it. It is exactly what I wanted!!

Probability of being positive: 0.998882 %
In [13]:
see_predictions(50)
Out[13]:
Text:
We loved this pail at first. The mechanism seemed ingenius, and we appreciated that it took regular bags. But once our daughter started to stand up, that big white handle was irresistable to her, and before we even realized it, she started flipping it. That makes the heavy center portion slide back and forth FAST, and sure enough she caught her finger and started yowling.This was *not* an isolated incident -- if you play with the pail a little bit, you'll quickly see that the mechanism is a total finger trap for toddlers. Worse yet, they can slam a finger then hurt it seriously in their attempts to get it free by pushing the handle the wrong way. It's really pretty scary.You'll notice that the positive reviews here generally say "I've been using this for two months, and it's great!" But once you have older babies, I'd retire it pronto.

Probability of being positive: 0.167362 %
In [14]:
see_predictions(11)
Out[14]:
Text:
These do not stick to the wall. They start to peel as soon as you stick them to the wall.

Probability of being positive: 0.212396 %
In [15]:
see_predictions(28)
Out[15]:
Text:
This little toy is safe for infants, and offers great color, texture and even sound. Very Sweet, though a little pricey.

Probability of being positive: 0.989883 %
In [16]:
see_predictions(23)
Out[16]:
Text:
My daughter had this in her crib until she was 2 and loved it. When she got old enough she loved to turn it on and off by herself. I used this every night for her to go to sleep by. I used the place that you store the remote control in for extra pacifiers, so if she lost one during the night she would know where to find one. I never used the remote, but loved the idea another had about putting in the car for rear facing infants when they get fussy you could just turn it on with the remote. It's not too bright and the volume is adjustable. Someone had said it eat batteries quickly,but I found that they lasted a very LONG time and I used it EVERY night. I highly recommend this product!!

Probability of being positive: 0.999223 %
In [17]:
see_predictions(37)
Out[17]:
Text:
My LO is definitely teething and I've tried giving this to her but she can never get the massager to go at it on her own. I will have to put it in her mouth and press it at the same time. She looks at me and I imagine her thinking "what the heck is that" and she pulls her face away. I'm hoping she'll want to use it again before her teeth actually come in since I can't return it at this point. We're sticking to the fake keys and her little Sophie the Giraffe for now.

Probability of being positive: 0.045743 %
In [18]:
see_predictions(53)
Out[18]:
Text:
This is on my list of must haves. The thing has been in my bedroom for 6 months. Not once have I ever smelled an odor from this thing. I love that I can use any kind of bag so I just buy those super cheap super flimsy tall size garbage bags.The only drawback, as with all diaper pails, is that as the baby gets bigger and the diapers get bigger, you have to change the bag more often. Our baby is 6 months and we fill it about once every 36 hours.But still, I wouldn't trade it for the world.

Probability of being positive: 0.999418 %
In [19]:
see_predictions(73)
Out[19]:
Text:
I registered for the Diaper Champ after reading many reviews for several different diaper pails. This is by far the easiest product to use. There are no complicated bag systems and no special replacement bags. I have not had the problem of having any oppressive odors leak out into the room. The only odor was inside the actual diaper pail itself. There is no such thing as a completely odorless plastic diaper pail since plastic absorbs odors, but just drop a couple of dryer sheets (Bounce or something similar) into the bottom of the diaper champ (not in the bag) and the odor disappears. This product is easy to use and the bags aren't hard to change. I would recommend this to anyone.

Probability of being positive: 0.016919 %
In [ ]: