源代码+数据集已上传至GitHub:https://github.com/zoe9698/Mojito_Douban_analysis
公众号博文:Python告诉你网友究竟如何看待周杰伦的《Mojito》
1.将数据集(包含我手动标注的情感倾向:{0:"不喜欢",1:"中立",2:"喜欢"})转为dataframe格式
#coding:utf-8
import pandas as pd
fpath = "D://数据集大荟萃/周杰伦新歌《Mojito》豆瓣短评数据集/mojito6931/1Mojito豆瓣短评数据6.12.csv"
df = pd.read_csv(fpath,engine='python')
2.情感倾向分析
- snownlp库:为python版的文本分析工具。
简单举例:用它区分两句话的情感倾向,看起来还可以。但是很慢。
应用到本数据集中:只有38%的正确率。。
- 常规情感分析的方法:分词->构造训练集测试集->构建词向量->朴素贝叶斯
正确率70%以上还是可以的。
3.matplotlib绘制数据可视化图
统计不同情感倾向的评论获赞总数:
绘制饼图:
分析最高赞的10条评论:
绘制柱状图:
# 绘制柱状图
labels = maxvote_dict.values()
votes = maxvote_dict.keys()
x = np.arange(10)
width = 0.35
fig3,ax3 = plt.subplots()
rects = ax3.bar(x,votes,width,label = '投票数')
ax3.set_ylabel('votes')
ax3.set_title('前十名高赞评论')
ax3.set_xticks(x)
ax3.set_xticklabels(labels)
ax3.legend()
# 在柱顶加具体投票数
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax3.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 2), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects)
fig3.tight_layout()
plt.savefig('D:/mojito/max10_zhu.jpg',dpi=300)
plt.show()
绘制词云图1---所有评论整合在一起:
allcontent = ""
for index,row in df.iterrows():
allcontent+=row['content']
# [所有content]绘制词云
from wordcloud import WordCloud as wc
text_cut = jieba.lcut(allcontent)
text_cut = ' '.join(text_cut)
stop_words_file = 'D:\文本数据预处理常用工具\stopwords-master\hit_stopwords.txt'
stop_words = open(stop_words_file,encoding="utf8").read().split("\n")
word_cloud = wc(font_path="simsun.ttc",
background_color="white", stopwords=stop_words,scale=4)
word_cloud.generate(text_cut)
plt.subplots(figsize=(72,64))
plt.imshow(word_cloud)
plt.axis("off")
plt.savefig('D:/all_wordcloud.jpg')
绘制词云图-----前十名高赞评论:
max10_content = ""
for index,row in maxvote_df_10.iterrows():
max10_content+=row["content"]
text_cut = jieba.lcut(max10_content)
text_cut = ' '.join(text_cut)
stop_words_file = 'D:\文本数据预处理常用工具\stopwords-master\hit_stopwords.txt'
stop_words = open(stop_words_file,encoding="utf8").read().split("\n")
word_cloud = wc(font_path="simsun.ttc",
background_color="white", stopwords=stop_words,scale=4)
word_cloud.generate(text_cut)
plt.subplots(figsize=(72,64))
plt.imshow(word_cloud)
plt.axis("off")
plt.savefig('D:/max10_wordcloud.jpg')
分析评星数据:
# 填充缺失值
df['rating_num'] = df['rating_num'].fillna(1)
绘制饼图:
# 绘制 星级 饼图
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
#解决中文显示问题
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
fig4, ax4 = plt.subplots()
ax4.pie(y,labels=x,autopct='%1.2f%%')
# x,y轴长度相同
ax4.axis('equal')
plt.savefig('D:/mojito/rating_count_bing.jpg',dpi=300)
plt.show()
# 绘制柱状图
labels = x
votes = y
x = np.arange(1,6)
width = 0.35
fig5,ax5 = plt.subplots()
rects = ax5.bar(x,y,width,label = '星级统计')
ax5.set_ylabel('count')
ax5.set_title('星级统计')
ax5.set_xticks(x)
ax5.set_xticklabels(labels)
ax5.legend()
# 在柱顶加具体投票数
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax5.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 2), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects)
fig5.tight_layout()
plt.savefig('D:/mojito/rating_count_zhu.jpg',dpi=300)
plt.show()