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']>=44、训练极性分类器
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']
本文通过使用GraphLab库对亚马逊产品的评论数据进行加载与预处理,并实现了一个基于评论的情感分类模型。该模型能够预测评论的情感倾向,即正面或负面,并通过实例展示了如何评估和应用此模型。
847

被折叠的 条评论
为什么被折叠?



