<jupyter_start><jupyter_text>Analyze Product Sentiment<jupyter_code>import turicreate
import os<jupyter_output><empty_output><jupyter_text>Read product review data<jupyter_code>d = os.getcwd() #Gets the current working directory
os.chdir("..")
products = turicreate.SFrame('./data/amazon_baby.sframe/m_bfaa91c17752f745.frame_idx')<jupyter_output><empty_output><jupyter_text>Explore data<jupyter_code>products
products.groupby('name',operations={'count':turicreate.aggregate.COUNT()}).sort('count',ascending=False)<jupyter_output><empty_output><jupyter_text>Examine the reivews for the most-reviewed product<jupyter_code>giraffe_reviews = products[products['name']=='<NAME> the Giraffe Teether']
giraffe_reviews
len(giraffe_reviews)
giraffe_reviews['rating'].show()<jupyter_output><empty_output><jupyter_text>Building a sentiment classifier  Build word count vectors<jupyter_code>products['word_count'] = turicreate.text_analytics.count_words(products['review'])
products<jupyter_output><empty_output><jupyter_text>Define what is positive and negative sentiment<jupyter_code>products['rating'].show()
#ignore all 3*  reviews
products = products[products['rating']!= 3]
#positive sentiment = 4-star or 5-star reviews
products['sentiment'] = products['rating'] >= 4
products
products['sentiment'].show()<jupyter_output><empty_output><jupyter_text>Train our sentiment classifier<jupyter_code>train_data,test_data = products.random_split(.8,seed=0)
sentiment_model = turicreate.logistic_classifier.create(train_data,target='sentiment', features=['word_count'], validation_set=test_data)<jupyter_output><empty_output><jupyter_text>Apply the sentiment classifier to better understand the Giraffe reviews<jupyter_code>products['predicted_sentiment'] = sentiment_model.predict(products, output_type = 'probability')
products
giraffe_reviews = products[products['name']== '<NAME> the Giraffe Teether']
giraffe_reviews<jupyter_output><empty_output><jupyter_text>Sort the Giraffe reviews according to predicted sentiment<jupyter_code>giraffe_reviews = giraffe_reviews.sort('predicted_sentiment', ascending=False)
giraffe_reviews
giraffe_reviews.tail()<jupyter_output><empty_output><jupyter_text>Show the most positive reviews<jupyter_code>giraffe_reviews[0]['review']
giraffe_reviews[1]['review']<jupyter_output><empty_output><jupyter_text>Most negative reivews<jupyter_code>giraffe_reviews[-1]['review']
giraffe_reviews[-2]['review']
# some selected words to measure the data
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
# count how many times did the customers mentioned the selected words
def getWordCount(data, word):
    return int(data[word]) if word in data else 0
# create new column with every selected words and count
for word in selected_words:
    products[word] = products['word_count'].apply(lambda x:getWordCount(x,word))
dicts = {}
for word in selected_words:
    if word not in dicts:
        dicts[word] = products[word].sum()
dicts
print('Max:', max(dicts, key=dicts.get))
print('Min:', min(dicts, key=dicts.get))
train_data,test_data = products.random_split(.8, seed=0)
features=selected_words
selected_words_model = turicreate.logistic_classifier.create(train_data,target='sentiment', features=features, validation_set=test_data)
selected_words_model
products['predicted_selected_words'] = selected_words_model.predict(products, output_type = 'probability')
products.print_rows(num_rows=30)
products[products['name']== 'Baby Trend Diaper Champ'].sort('predicted_sentiment', ascending=False)[0]
selected_words_model.evaluate(test_data)
sentiment_model.evaluate(test_data)
test_data[test_data['rating'] >3].num_rows()/test_data.num_rows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.125965379494008, 'fraction_numerical': 0.0074567243675099865, 'mean_word_length': 11.155339805825243, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '32401', 'license': 'Apache-2.0', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1234, 'n_words': 213, 'path': 'University of Washington - Machine Learning Specialization/University of Washington - Machine Learning Foundations A Case Study Approach/week3/FND03-NB01.ipynb', 'repo_name': 'swathyh/Courses-', 'size': '3767'}