首页 > 大数据 > 可视化 > 【数据可视化】周杰伦新歌《Mojito》豆瓣短评数据
# 导入包import numpy as np import pandas as pd import jieba import time import requests from pyecharts.charts import Pie, Bar, Line, Pagefrom pyecharts import options as opts from pyecharts.globals import SymbolType# 读入数据df_douban = pd.read_csv('/home/kesci/input/mojito6931/Mojito豆瓣短评数据6.12.csv')df_douban.head()# 重复值df_douban.duplicated().sum()df_douban.info()# 提取星级df_douban['star'] = df_douban.rating_num.str.extract(r'(\d)')# 删除列df_douban = df_douban.drop(['rating_num', 'user_url', 'comment_time'], axis=1) df_douban.head(3)# 异常值df_douban['content'] = df_douban.content.replace('🤨', '微笑')# 输入API Key和Secret Keyak = 'iBrqRI4BQunrDH7Bi1060bBG'sk = 'IkBdZFZQ2kBKVp3i1iXlDVcZzPQdGNmP'host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(ak, sk)# 发起请求r = requests.post(host) # 获取tokentoken = r.json()['access_token']def get_sentiment_score(text):
""" 输入文本,返回情感倾向得分 """
url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?charset=UTF-8&access_token={}'.format(token)
data = {
'text': text
}
data = json.dumps(data) #字典-字符串
# 发起请求
try:
res = requests.post(url, data=data, timeout=3)
items_score = res.json()['items']
except Exception as e:
time.sleep(3)
res = requests.post(url, data=data, timeout=3)
items_score = res.json()['items']
return items_scorescore_list = [] step = 0for i in df_douban['content']:
score = get_sentiment_score(i)
# 打印进度
step += 1
print('我正在获取第{}个评分'.format(step), end='\r')
score_list.append(score)# 提取正负概率positive_prob = [i[0]['positive_prob'] for i in score_list]negative_prob = [i[0]['negative_prob'] for i in score_list]df_douban['positive_prob'] = positive_probdf_douban['negative_prob'] = negative_prob# 正负向df_douban['label'] = ['正向' if i >0.5 else '负向' for i in df_douban.positive_prob]df_douban.head()# 计数star_num = df_douban.star.value_counts()star_num = star_num.sort_index()star_num# 数据对data_pair = [list(z) for z in zip([i+'星' for i in star_num.index], star_num.values.tolist())]# 饼图pie1 = Pie(init_opts=opts.InitOpts(width='1350px', height='750px'))pie1.add('', data_pair, radius=['35%', '60%'])pie1.set_global_opts(title_opts=opts.TitleOpts(title='豆瓣短评评分占比'),
legend_opts=opts.LegendOpts(orient='vertical', pos_top='15%', pos_left='2%')
) pie1.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{d}%'))pie1.render_notebook()bins = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0] score_num = pd.cut(df_douban.positive_prob, bins=bins)score_num = score_num.value_counts().sort_index()score_num# 柱形图bar1 = Bar(init_opts=opts.InitOpts(width='1350px', height='750px'))bar1.add_xaxis(score_num.index.astype('str').tolist())bar1.add_yaxis('', score_num.values.tolist(), category_gap='0%')bar1.set_global_opts(title_opts=opts.TitleOpts(title='基于百度AI评论情感得分'),
visualmap_opts=opts.VisualMapOpts(max_=200))bar1.render_notebook()def get_cut_words(content_series):
# 读入停用词表
stop_words = []
with open(r"/home/kesci/input/stop6931/哈工大停用词表.txt", 'r', encoding='gb18030') as f:
lines = f.readlines()
for line in lines:
stop_words.append(line.strip())
# 添加关键词
my_words = ['周杰伦', '一首歌']
for i in my_words:
jieba.add_word(i) # 自定义停用词
my_stop_words = ['歌有', '真的', '这首', '一首', '一点',
'反正', '一段', '一句', '首歌']
stop_words.extend(my_stop_words)
# 分词
word_num = jieba.lcut(content_series.str.cat(sep='。'), cut_all=False)
# 条件筛选
word_num_selected = [i for i in word_num if i not in stop_words and len(i)>=2]
return word_num_selectedtext1 = get_cut_words(content_series=df_douban[(df_douban.star=='4')|(df_douban.star=='5')]['content'])text1[:5]! pip install stylecloudimport stylecloudfrom IPython.display import Image # 用于在jupyter lab中显示本地图片# 绘制词云图stylecloud.gen_stylecloud(text=' '.join(text1),
max_words=1000,
collocations=False,
font_path=r'/home/kesci/input/经典综艺体简.TTF',
icon_name='fas fa-thumbs-up',
size=612,
output_name='豆瓣正向评分词云图.png')Image(filename='豆瓣正向评分词云图.png')text2 = get_cut_words(content_series=df_douban[(df_douban.star=='1')|(df_douban.star=='2')]['content'])text2[:5]# 绘制词云图stylecloud.gen_stylecloud(text=' '.join(text2),
max_words=1000,
collocations=False,
font_path=r'/home/kesci/input/经典综艺体简.TTF',
icon_name='fas fa-thumbs-down',
size=612,
output_name='豆瓣负向评分词云图.png')Image(filename='豆瓣负向评分词云图.png')page = Page()page = page.add(pie1, bar1)page.render('Mojito豆瓣数据分析.html')
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69977871/viewspace-2700670/ ,如需转载,请注明出处,否则将追究法律责任。