In [3]:
%matplotlib inline
import graphlab

Read sompe product data


In [4]:
products = graphlab.SFrame('amazon_baby.gl/')


This non-commercial license of GraphLab Create is assigned to neo20iitkgp@gmail.com and will expire on July 05, 2017. For commercial licensing options, visit https://turi.com/buy/.
[INFO] graphlab.cython.cy_server: GraphLab Create v2.0.1 started. Logging: /tmp/graphlab_server_1468522977.log

Lets Explore


In [5]:
products.head()


Out[5]:
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]

Build Word Count Vector


In [6]:
products['word_count'] = graphlab.text_analytics.count_words(products['review'])

In [7]:
products.head()


Out[7]:
name review rating word_count
Planetwise Flannel Wipes These flannel wipes are
OK, but in my opinion ...
3.0 {'and': 5, '6': 1,
'stink': 1, 'because' ...
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, 'this': 2,
'her': 1, 'help': 2, ...
A Tale of Baby's Days
with Peter Rabbit ...
Lovely book, it's bound
tightly so you may no ...
4.0 {'shop': 1, 'noble': 1,
'is': 1, 'it': 1, 'as': ...
Baby Tracker® - Daily
Childcare Journal, ...
Perfect for new parents.
We were able to keep ...
5.0 {'and': 2, 'all': 1,
'right': 1, 'when': 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, 'nanny':
1, 'standarad': 1, ...
[10 rows x 4 columns]


In [8]:
graphlab.canvas.set_target('ipynb')

In [9]:
products['name'].show()


Explore Vulli Sophie


In [10]:
giraff_review = products[products['name'] == 'Vulli Sophie the Giraffe Teether']

In [11]:
print len(giraff_review)


785

In [12]:
giraff_review['rating'].show(view = 'Categorical')


Build a sentiment Classifier


In [13]:
products = products[products['rating'] != 3]

In [14]:
products['sentiment'] = products['rating']>=4

In [15]:
products.tail()


Out[15]:
name review rating word_count sentiment
Airline Seat Belt
Extender - The Best ...
Up until recently I have
hated flying, putting on ...
5.0 {'and': 1, 'have': 1,
'being': 1, 'protrudes': ...
1
Airline Seat Belt
Extender - The Best ...
I bought this as a
father's day gift for my ...
5.0 {'and': 4, 'dad': 1,
'because': 1, 'being' ...
1
Squeasy Snacker 6oz
Silicone Reusable Food ...
I love that these can
hold purees OR liquids, ...
5.0 {'and': 5, 'smaller': 1,
'love': 2, 'clip': 1, ...
1
Squeasy Snacker 6oz
Silicone Reusable Food ...
I love this product, it
makes my life easier. ...
5.0 {'and': 6, 'love': 1,
'school': 1, 'just': 1, ...
1
Squeasy Snacker 6oz
Silicone Reusable Food ...
This reusable squeeze
bottle is the best I ...
5.0 {'-': 1, 'through': 1,
'go': 1, 'yet': 1, ...
1
Baby Teething Necklace
for Mom Pretty Donut ...
Such a great idea! very
handy to have and look ...
5.0 {'and': 1, 'help': 1,
'too,': 1, 'reduce': 1, ...
1
Baby Teething Necklace
for Mom Pretty Donut ...
This product rocks! It
is a great blend of ...
5.0 {'accessible': 1, 'and':
5, 'concept': 1, 'is' ...
1
Abstract 2 PK Baby /
Toddler Training Cup ...
This item looks great and
cool for my kids....I ...
5.0 {'and': 2, 'great': 2,
'kids....i': 1, 'for' ...
1
Baby Food Freezer Tray -
Bacteria Resistant, BPA ...
I am extremely happy with
this product. I have ...
5.0 {'just': 1, 'bimbi': 2,
'one.': 1, 'bright': 1, ...
1
Best 2 Pack Baby Car
Shade for Kids - Window ...
I love this product very
mush . I have bought ...
5.0 {'and': 1, 'love': 1,
'keeps': 1, 'shades': 1, ...
1
[10 rows x 5 columns]

Lets train the sentiment classifier


In [16]:
train_data, test_data = products.random_split(.8, seed=0)

In [17]:
sentiment_model = graphlab.logistic_classifier.create(train_data, 
                                                      target='sentiment', 
                                                      features=['word_count'],
                                                      validation_set = test_data)


WARNING: The number of feature dimensions in this problem is very large in comparison with the number of examples. Unless an appropriate regularization value is set, this model may not provide accurate predictions for a validation/test set.
Logistic regression:
--------------------------------------------------------
Number of examples          : 133448
Number of classes           : 2
Number of feature columns   : 1
Number of unpacked features : 219217
Number of coefficients    : 219218
Starting L-BFGS
--------------------------------------------------------
+-----------+----------+-----------+--------------+-------------------+---------------------+
| Iteration | Passes   | Step size | Elapsed Time | Training-accuracy | Validation-accuracy |
+-----------+----------+-----------+--------------+-------------------+---------------------+
| 1         | 5        | 0.000002  | 2.065740     | 0.841481          | 0.839989            |
| 2         | 9        | 3.000000  | 3.102573     | 0.947425          | 0.894877            |
| 3         | 10       | 3.000000  | 3.514833     | 0.923768          | 0.866232            |
| 4         | 11       | 3.000000  | 3.915877     | 0.971779          | 0.912743            |
| 5         | 12       | 3.000000  | 4.333984     | 0.975511          | 0.908900            |
| 6         | 13       | 3.000000  | 4.751852     | 0.899991          | 0.825967            |
| 10        | 18       | 1.000000  | 6.580463     | 0.988715          | 0.916256            |
+-----------+----------+-----------+--------------+-------------------+---------------------+
TERMINATED: Iteration limit reached.
This model may not be optimal. To improve it, consider increasing `max_iterations`.

Evaluate The model


In [18]:
sentiment_model.evaluate(test_data, metric = 'roc_curve')


Out[18]:
{'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.}

In [19]:
sentiment_model.show(view='Evaluation')


Apply Learned model to understand the sentiment


In [20]:
giraff_review['predicted_sentiment'] = sentiment_model.predict(giraff_review, output_type='probability')

In [21]:
giraff_review.head()


Out[21]:
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,
'would': 1, 'latex.': 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,
'months': 1, 'old': 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, ...
0.989190989536
Vulli Sophie the Giraffe
Teether ...
What a friendly face!
And those mesmerizing ...
5.0 {'and': 3, 'chew': 1,
'be': 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, 'seemed': 1,
'because': 1, 'about.': ...
0.970160542725
Vulli Sophie the Giraffe
Teether ...
My baby seems to like
this toy, but I could ...
3.0 {'and': 2, 'already': 1,
'some': 1, 'it': 3, ...
0.195367644588
[10 rows x 5 columns]

Sort the reviews


In [22]:
giraff_review = giraff_review.sort('predicted_sentiment', ascending=False)

In [23]:
giraff_review.head()


Out[23]:
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, 'bring': ...
1.0
Vulli Sophie the Giraffe
Teether ...
I'm not sure why Sophie
is such a hit with the ...
4.0 {'adoring': 1, 'find': 1,
'month': 1, 'bright': 1, ...
0.999999999703
Vulli Sophie the Giraffe
Teether ...
I'll be honest...I bought
this toy because all the ...
4.0 {'all': 2, 'discovered':
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]


In [24]:
giraff_review[0]['review']


Out[24]:
"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"

In [25]:
giraff_review[1]['review']


Out[25]:
"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."

In [26]:
giraff_review[-1]['review']


Out[26]:
"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!"

In [27]:
giraff_review[-2]['review']


Out[27]:
"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."

In [28]:
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']

In [29]:
type(products['word_count'][0])


Out[29]:
dict

In [30]:
def select_word_count(word_count, word):
    if word in word_count:
        return word_count[word]
    return 0

In [31]:
print select_word_count(products['word_count'][1], 'awesome')


0

In [34]:
for word in selected_words:
    products[word] = products['word_count'].apply(lambda x: select_word_count(x, word))

In [35]:
products.sort('awesome', ascending=False).head()


Out[35]:
name review rating word_count sentiment awesome
Rainy Day Indoor
Playground toddler swing ...
This is the #1 played
with toy everyday! Every ...
5.0 {'one!': 1, 'infant': 2,
'outdoor': 1, 'just': 1, ...
1 4
Chicco NextFit
Convertible Car Seat, ...
I purchased this car seat
online through BRUS a ...
4.0 {'all': 7, 'concept': 1,
'consider': 1, ...
1 3
Lascal BuggyBoard Maxi+ -
Black ...
bought it, attached it
and traveled with it ...
4.0 {'over': 1, 'and': 4,
'all': 2, 'set': 1, ...
1 3
Child Airplane Travel
Harness - Cares Safety ...
This contraption is
awesome. We recently ...
4.0 {'move': 1, 'go': 1,
'row': 1, 'had': 1, ' ...
1 3
Fisher-Price Rainforest
Open-Top Cradle Swing ...
We bought this swing for
our 3 week old when we ...
4.0 {'infant': 1, 'just': 3,
'-': 3, 'held': 1, ...
1 3
Munchkin 2 Pack Fresh
Food Feeder, Colors May ...
AWESOME MUST HAVE!!!!! ok
i got these at first b/c ...
5.0 {'all': 1, 'being': 1,
'able': 1, 'kids)': 1, ...
1 3
Hotslings Designer Pouch
Style Baby Carrier, Z ...
From newborn till
toddlerhood we used this ...
5.0 {'and': 2, 'very': 1,
'tired': 1, 'awesome' ...
1 2
Boon Flo Water Deflector
and Protective Faucet ...
Boons Let it Flo seemed
like an awesome ...
1.0 {'being': 1, 'held': 1,
'softer': 1, 'seemed' ...
0 2
Lamaze Dream Screen I LOVED this when I saw
it online (and loved the ...
4.0 {'help': 2, 'over': 1,
'installed': 1, 'four': ...
1 2
Cloud b Twilight
Constellation Night ...
This is an awesome
product. My son had ...
5.0 {'just': 1, 'show': 1,
'yellow': 2, 'thanks' ...
1 2
great fantastic amazing love horrible bad terrible awful wow hate
0 0 0 2 0 0 0 0 0 0
4 0 0 2 0 0 0 0 0 2
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 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 1 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
[10 rows x 16 columns]


In [38]:
words = dict()
for i in products['word_count']:
    for k,v in i.iteritems():
        if k in words:
            value = words[k]
            words[k] = value + v
        else:
            words[k] = v

In [43]:
for w in selected_words:
    print w, '=', words[w]


awesome = 2002
great = 42420
fantastic = 873
amazing = 1305
love = 40277
horrible = 659
bad = 3197
terrible = 673
awful = 345
wow = 131
hate = 1057

In [38]:
train_data_one, test_data_one = products.random_split(.8, seed=0)
selected_words_model = graphlab.logistic_classifier.create(train_data_one,
                                                           'selected_words_model', 
                                                           features=selected_words)


PROGRESS: Creating a validation set from 5 percent of training data. This may take a while.
          You can set ``validation_set=None`` to disable validation tracking.

---------------------------------------------------------------------------
ToolkitError                              Traceback (most recent call last)
<ipython-input-38-a21d7dea61e2> in <module>()
      1 train_data_one, test_data_one = products.random_split(.8, seed=0)
----> 2 selected_words_model = graphlab.logistic_classifier.create(train_data_one,'selected_words_model', features=selected_words)

/Users/z001h2s/Work/github/Machine-Learning-PlayGround/ml-playground-env/lib/python2.7/site-packages/graphlab/toolkits/classifier/logistic_classifier.pyc in create(dataset, target, features, l2_penalty, l1_penalty, solver, feature_rescaling, convergence_threshold, step_size, lbfgs_memory_level, max_iterations, class_weights, validation_set, verbose)
    306                         lbfgs_memory_level = lbfgs_memory_level,
    307                         max_iterations = max_iterations,
--> 308                         class_weights = class_weights)
    309 
    310     return LogisticClassifier(model.__proxy__)

/Users/z001h2s/Work/github/Machine-Learning-PlayGround/ml-playground-env/lib/python2.7/site-packages/graphlab/toolkits/_supervised_learning.pyc in create(dataset, target, model_name, features, validation_set, verbose, distributed, **kwargs)
    398 
    399     # Target
--> 400     target_sframe = _toolkits_select_columns(dataset, [target])
    401 
    402     # Features

/Users/z001h2s/Work/github/Machine-Learning-PlayGround/ml-playground-env/lib/python2.7/site-packages/graphlab/toolkits/_internal_utils.pyc in _toolkits_select_columns(dataset, columns)
    352         missing_features = list(set(columns).difference(set(dataset.column_names())))
    353         raise ToolkitError("The following columns were expected but are " +
--> 354                            "missing: {}".format(missing_features))
    355 
    356 def _raise_error_if_column_exists(dataset, column_name = 'dataset',

ToolkitError: The following columns were expected but are missing: ['selected_words_model']

In [ ]: