1、加载数据
import graghlab as gl
products=gl.SFrame('amazon_baby.gl')
2、将每个评论向量化
products['word_count']=gl.text_analytics.count_words(products['review'])
查看前几列元素
products.head
显示数据
products['name'].show()
3、浏览Vulli Sophie数据
giraffe_reviews=products[products['name']=="Vulli Sophie the Giraffe Teether"]
len(giraffe_reviews)
giraffe_reviews['rating'].show(view='Categorical')
、
定义一个正负极性
#ignore all 3*reviews
products=products[products['rating']!=3]
#positive sentiment =4* or 5* reviews
products['sentiment']=products['rating']>=4
4、训练极性分类器
train_data,test_data=products.random_split(.8,seed=0)
sentiment_model=gl.logistic_classifier.create(train_data,
target='sentiment',
features=['word_count'],
validation_set=test_data)
5、评估模型
sentiment_model.evaluate(test_data,metric='roc_curve')
sentiment_model.show(view='Evaluation')
6、应用这个模型
giraffe_reviews['predicted_sentiment']=sentiment_model.predict(giraffe_reviews,output_type='probability')
giraffe_reviews.head()
基于predicted_sentiment排序
giraffe_reviews=giraffe_reviews.sort('predicted_sentiment',ascending=False)
giraffe_reviews.head()
giraffe_reviews[-1]['review']