<jupyter_start><jupyter_text>Load GrahpLab Create<jupyter_code>import graphlab<jupyter_output><empty_output><jupyter_text>Basic settings<jupyter_code>#limit number of worker processes to 4
graphlab.set_runtime_config('GRAPHLAB_DEFAULT_NUM_PYLAMBDA_WORKERS', 8)
#set canvas to open inline
graphlab.canvas.set_target('ipynb')<jupyter_output><empty_output><jupyter_text>Load the Amazon baby data<jupyter_code>products = graphlab.SFrame('amazon_baby.gl/')
products.head()
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']<jupyter_output><empty_output><jupyter_text>Feature engineering<jupyter_code>products['word_count'] = graphlab.text_analytics.count_words(products['review'])
products.head()<jupyter_output><empty_output><jupyter_text>1. Use .apply() to build a new feature with the counts for each of the selected_words<jupyter_code>for key in selected_words:
    products[key] = 0
products.head()
products[0]['word_count'].keys()
def key_count(dict, key):
    if key in dict:
        return dict[key]
    else:
        return 0
for key in selected_words:
    products[key] = products['word_count'].apply(lambda x: key_count(x, key))
products.head()
products.tail()
products['awesome'].show(view='Categorical')<jupyter_output><empty_output><jupyter_text>Using the .sum() method on each of the new columns you created, answer the following questionsOut of the selected_words, which one is most used in the dataset? Which one is least used? Save these results to answer the quiz at the end.<jupyter_code>for key in selected_words:
    print key + ' : ' + str(products[key].sum())
len(selected_words)
# ignore all 3* reviews
products = products[products['rating'] != 3]
# positive sentiment = 4* or 5* reviews
products['sentiment'] = products['rating'] >=4
for key in selected_words:
    products[key] = products['word_count'].apply(lambda x: key_count(x, key))
for key in selected_words:
    print key + ' : ' + str(products[key].sum())<jupyter_output>awesome : 2002
great : 42420
fantastic : 873
amazing : 1305
love : 40277
horrible : 659
bad : 3197
terrible : 673
awful : 345
wow : 131
hate : 1057<jupyter_text>2. Create a new sentiment analysis model using only the selected_words as features<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>CoefficientsUsing this approach, sort the learned coefficients according to the ‘value’ column using .sort(). Out of the 11 words in selected_words, which one got the most positive weight? Which one got the most negative weight? Do these values make sense for you? Save these results to answer the quiz at the end.<jupyter_code>selected_words_model['coefficients'].sort('value', ascending=False).print_rows(12,5)<jupyter_output>+-------------+-------+-------+------------------+------------------+
|     name    | index | class |      value       |      stderr      |
+-------------+-------+-------+------------------+------------------+
|     love    |  None |   1   |  1.39989834302   | 0.0287147460124  |
| (intercept) |  None |   1   |  1.36728315229   | 0.00861805467824 |
|   awesome   |  None |   1   |  1.05800888878   |  0.110865296265  |
|   amazing   |  None |   1   |  0.892802422508  |  0.127989503231  |
|  fantastic  |  None |   1   |  0.891303090304  |  0.154532343591  |
|    great    |  None |   1   |  0.883937894898  | 0.0217379527921  |
|     wow     |  None |   1   | -0.0541450123333 |  0.275616449416  |
|     bad     |  None |   1   | -0.985827369929  | 0.0433603009142  |
|     hate    |  None |   1   |  -1.40916406276  | 0.0771983993506  |
|    awful    |  None |   1   |  -1.76469955631  |  0.134679803365  |
|   horrible  |  None |   1   |  -1.99651800559  | 0.0973584169028  |
|   terrible  |  Non[...]<jupyter_text>3. Comparing the accuracy of different sentiment analysis model<jupyter_code>sentiment_model = graphlab.logistic_classifier.create(train_data,
                                                     target='sentiment',
                                                     features=['word_count'],
                                                     validation_set=test_data)
sentiment_model.evaluate(test_data, metric='roc_curve')
sentiment_model.show(view='Evaluation')
sentiment_model.evaluate(test_data)
selected_words_model.evaluate(test_data)
selected_words_model.show(view='Evaluation')<jupyter_output><empty_output><jupyter_text>What is the accuracy of the selected_words_model on the test_data? What was the accuracy of the sentiment_model that we learned using all the word counts in the IPython Notebook above from the lectures? What is the accuracy majority class classifier on this task? How do you compare the different learned models with the baseline approach where we are just predicting the majority class? Save these results to answer the quiz at the end.<jupyter_code>test_data['sentiment'].show(view='Categorical')<jupyter_output><empty_output><jupyter_text>4. Interpreting the difference in performance between the models<jupyter_code>diaper_champ_reviews = products[products['name'] == 'Baby Trend Diaper Champ']
len(diaper_champ_reviews)
diaper_champ_reviews.head()
diaper_champ_reviews['predicted_sentiment'] = sentiment_model.predict(diaper_champ_reviews, output_type='probability')<jupyter_output><empty_output><jupyter_text>What is the ‘predicted_sentiment’ for the most positive review for ‘Baby Trend Diaper Champ’ according to the sentiment_model from the IPython Notebook from lecture? Save this result to answer the quiz at the end.<jupyter_code>diaper_champ_reviews = diaper_champ_reviews.sort('predicted_sentiment', ascending=False)
diaper_champ_reviews.head()<jupyter_output><empty_output><jupyter_text>Now use the selected_words_model you learned using just the selected_words to predict the sentiment most positive review you found above. Save this result to answer the quiz at the end.<jupyter_code>selected_words_model.predict(diaper_champ_reviews[0:1], output_type='probability')<jupyter_output><empty_output><jupyter_text>Why is the predicted_sentiment for the most positive review found using the model with all word counts (sentiment_model) much more positive than the one using only the selected_words (selected_words_model)? Hint: examine the text of this review, the extracted word counts for all words, and the word counts for each of the selected_words, and you will see what each model used to make its prediction. Save this result to answer the quiz at the end.<jupyter_code>diaper_champ_reviews[0]['review']
diaper_champ_reviews[0]['word_count']
diaper_champ_reviews[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11020349670392662, 'fraction_numerical': 0.052450558899398106, 'mean_word_length': 3.8634146341463413, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '504818', 'license': 'MIT', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2141, 'n_words': 569, 'path': 'ml-foundations/week-3/Assignment - Week 3.ipynb', 'repo_name': 'zomansud/coursera', 'size': '6978'}