In [3]:
import graphlab
In [ ]:
products = graphlab.SFrame('amazon_baby.gl/')
In [5]:
products.head()
Out[5]:
In [6]:
products['word_count'] = graphlab.text_analytics.count_words(products['review'])
In [7]:
products.head()
Out[7]:
In [8]:
graphlab.canvas.set_target('ipynb')
In [9]:
products['name'].show()
In [11]:
giraffe_reviews = products[products['name'] == 'Vulli Sophie the Giraffe Teether']
In [12]:
len(giraffe_reviews)
Out[12]:
In [13]:
giraffe_reviews['rating'].show(view='Categorical')
In [14]:
products['rating'].show(view='Categorical')
In [15]:
#ignore all 3* reviews
products = products[products['rating'] != 3]
In [18]:
#positive sentiment = 4* or 5* reviews
products['sentiment'] = products['rating'] >=4
In [19]:
products.head()
Out[19]:
In [20]:
train_data,test_data = products.random_split(.8, seed=0)
In [21]:
sentiment_model = graphlab.logistic_classifier.create(train_data,
target='sentiment',
features=['word_count'],
validation_set=test_data)
In [22]:
sentiment_model.evaluate(test_data, metric='roc_curve')
Out[22]:
In [23]:
sentiment_model.show(view='Evaluation')
In [24]:
giraffe_reviews['predicted_sentiment'] = sentiment_model.predict(giraffe_reviews, output_type='probability')
In [25]:
giraffe_reviews.head()
Out[25]:
In [26]:
giraffe_reviews = giraffe_reviews.sort('predicted_sentiment', ascending=False)
In [24]:
giraffe_reviews.head()
Out[24]:
In [27]:
giraffe_reviews[0]['review']
Out[27]:
In [26]:
giraffe_reviews[1]['review']
Out[26]:
In [27]:
giraffe_reviews[-1]['review']
Out[27]:
In [28]:
giraffe_reviews[-2]['review']
Out[28]:
In [28]:
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
In [70]:
def sent_word_count(word_counts):
if 'hate' in word_counts:
return word_counts['hate']
else:
return 0
In [72]:
products['hate'] = products['word_count'].apply(sent_word_count)
In [79]:
word_dict = {}
for word in selected_words:
word_dict[word] = products[word].sum()
In [81]:
train_data, test_data = products.random_split(.8, seed=0)
In [82]:
selected_words_model = graphlab.logistic_classifier.create(train_data,
target='sentiment',
features=selected_words,
validation_set=test_data)
In [83]:
selected_words_model['coefficients']
Out[83]:
In [87]:
swm_coefficients = selected_words_model['coefficients']
In [ ]: