【天池学习赛】数据分析达人赛1:用户情感可视化分析

赛题简介

赛题以网络舆情分析为背景,要求选手根据用户的评论来对品牌的议题进行数据分析与可视化。通过这道赛题来引导常用的数据可视化图表,以及数据分析方法,对感兴趣的内容进行探索性数据分析。

赛题数据

数据源: earphone_sentiment.csv,为10000+条行业用户关于耳机的评论
使用天池实验室打比赛即可直接在notebook中挂载数据源

字段名称类型描述说明
content_idInt数据ID/
contentString文本内容/
subjectString主题提取或依据上下文归纳出来的主题
sentiment_valueInt情感分析分析出的情感
sentiment_wordString情感词情感词

赛题任务

1)词云可视化(评论中的关键词,不同情感的词云)
2)柱状图(不同主题,不同情感,不同情感词)
3)相关性系数热力图(不同主题,不同情感,不同情感词)

在天池实验室中用notebook完成下面至少一种可视化分析任务,并分享到比赛论坛(越多越好,还可以进行其他的可视化探索,发挥你的想象力)

代码

‘’’
目标:完成至少一项分析
1)词云可视化(评论中的关键词,不同情感的词云)
2)柱状图(不同主题,不同情感,不同情感词)
3)相关性系数热力图(不同主题,不同情感,不同情感词)
‘’’
jieba分词指导手册:https://github.com/fxsjy/jieba

import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import font_manager
import collections
import jieba
from wordcloud import WordCloud

# plt.rcParams['font.family']='sans-serif' 
# font_manager.fontManager.addfont('simhei.ttf')  #传入字体文件路径临时注册一个字体
# plt.rcParams['font.sans-serif'] = ['simhei'] #用来正常显示中文标签
# plt.rcParams['axes.unicode_minus']=False     #用来正常显示负号

# mac系统下自带的中文字体,还有一部分字体放在/Library/Fonts/下  
# mac可通过command+space ->字体册->查看本机自带字体->右键“在访达中显示”可查看字体路径
# font = font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Light.ttc') 
# font_manager.fontManager.addfont('/System/Library/Fonts/STHeiti Light.ttc') #传入字体文件路径临时注册一个字体

plt.rcParams['font.sans-serif'] = ['STHeiti Light']
plt.rcParams['axes.unicode_minus']=False     #用来正常显示负号
font_manager._rebuild()
# 有中文出现的情况需要u'内容'
# 设置了上述代码,还是没有用。 mmp..........

#为了界面美观,忽略不影响运行的警告
import warnings
warnings.filterwarnings('ignore')
# 1 加载数据
os.getcwd()
'/Users/weiyi/Applications/jupyter_work/ml/tianchi_match'
path = '/Users/weiyi/Applications/jupyter_work/ml/datasets/'
data = pd.read_csv(path+"earphone_sentiment.csv")
data.tail(20)

在这里插入图片描述

# 数据分析
data['subject'].value_counts()  #[其他,配置,音质,价格,外形,功能,舒适 ]
data['sentiment_word'].value_counts()
data['sentiment_value'].value_counts()   # 0,1,-1
 0    12210
 1     4376
-1      590
Name: sentiment_value, dtype: int64
data[(data['sentiment_word'].notna())]
data[(data['sentiment_value']==0)] # 12210 rows × 5 columns
data[(data['sentiment_word'].isnull())] # 12210 rows × 5 columns
# 说明data['sentiment_word']值为nan的行,data['sentiment_value']=0

在这里插入图片描述

# 柱状图(不同主题,不同情感,不同情感词)
#柱状图-情感词分布 
tem = data['sentiment_word'].value_counts()
plt.figure(figsize=(12,7))
plt.bar(tem.index,tem.values,color="peachpuff")
plt.xticks(rotation=45)
plt.show()
# font = font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Light.ttc') # mac系统下自带的中文字体,还有一部分字体放在/Library/Fonts/下
# plt.title('情感词分布柱状图', fontproperties=font, fontsize=16)  # 此效果可行

# plt.bar([u'好', '不错', '差', '强', '牛', '垃圾', '高级', '追求', '呵呵', '难听', '骗', '用心', '舒适',
#        '噪音', '疼', '音染', '水准', '轰', '精致', '惊艳', '良心', '无语', '无奈', '不舒服', '小巧',
#        '充足', '上当', '辣鸡', '模糊', '混浊'],tem.values,color="peachpuff") # 中文乱码

在这里插入图片描述

#柱状图-评论主题分布
plt.figure(figsize=(12,7))
tem_sub = data['subject'].value_counts()
sns.countplot(x=data['subject'],order=tem_sub.index)  # x:x轴上的条形图,直接为series数据  order:排序
plt.show()

在这里插入图片描述

#柱状图-不同情感类型的评论分布
value={0:"中评",1:"好评",-1:"差评"}
temp=data.copy()
temp["sentiment_value"]=temp["sentiment_value"].map(value)
tem_1=temp[temp["subject"].isin(["配置","音质","价格"])]
tem_2=tem_1.pivot_table(values="content_id",index="subject",columns="sentiment_value",aggfunc="count")
plt.figure(figsize=(10,6))
tem_2.plot.bar()
plt.xticks(fontsize=15,rotation=360)
plt.yticks(fontsize=15)
plt.show()
<Figure size 720x432 with 0 Axes>

在这里插入图片描述

#情感词词云分布
#评论分类
comment_neg = data[data['sentiment_value']==-1]#差评
comment_pos = data[data['sentiment_value']==1]#好评
comment_mid = data[data['sentiment_value']==0]#中评
comment_pos = comment_pos['content'].apply(lambda x : list(jieba.cut(x)))
comment_neg = comment_neg['content'].apply(lambda x : list(jieba.cut(x)))
comment_mid = comment_mid['content'].apply(lambda x : list(jieba.cut(x)))
Building prefix dict from the default dictionary ...
Loading model from cache /var/folders/gc/_mjrb89j5pn11ptg108mlwpm0000gn/T/jieba.cache
Loading model cost 0.480 seconds.
Prefix dict has been built successfully.
comment_pos
0        [Silent,  , Angel, 期待, 您, 的, 光临, ,, 共赏, 美好, 的,...
2          [达音科,  , 17, 周年,  , 倒, 是, 数据, 最, 好看, ,, 而且, 便宜]
4                                              [不错, 的, 数据]
10       [我, 觉得, 器材, 到, 一定, 级别, ,, 搭配, 好, 了, ,, 达到, 听, ...
17       [网页, 的, 手机, 版, 总是, 无法, 退到, 上, 一层, ,, 希望, 这个, 能...
                               ...                        
17159    [今年, 买, 了, 个, 乾龙盛, 的, qa390, ,, 听, 了, 2, 个, 月,...
17160    [t1, 确实, 不大好, 搞, ,, 当时, 原声带, 听过, 几次, a2, 推, 99...
17162    [这个, 人, 说话, 好, 直接, 我, 喜欢, ,, 好, 芯片, 不, 等于, 成品,...
17163    [AK, 的, 芯片, 值得, 肯定, ,, 至少, 听过, 的, 4399,  , 449...
17171         [3000, 价位, 推, hd650, 有, 比, S7, 更好, 的, 耳放, 么]
Name: content, Length: 4376, dtype: object
#去掉停用词 返回词频字典
def RemoveStop(comment):
    remove_words = ['没有','一个','啊','吗','不是','过','什么','是不是','如果','这么','楼主','万','好像',
                    'NAN','~','“','”',',','%','的','。','了',' ','是','我','也','都','?','不','就',
                    '有','听','和','\xa0','很','可以','你','用','在','没','这个','还是','这','吧','还',
                    '就是','说','上','、','...','会','觉得','!','+','那','人','个','到','-','下','其实',
                    '.','[',']','去','s',':','…',',','~','!','(',')''2','把','650','啥','得','一下',
                    '还有','不会','能','感觉','要','多','呢','但','知道','耳机','可能','耳朵','但是','非常',
                    '应该','区别','比如','—','着',]
    comment_list = []
    for i in comment:
        for j in i:
            if j not in remove_words:
                comment_list.append(j)
    # 统计“可迭代序列”中每个元素的出现的次数
    word_counts = collections.Counter(comment_list)
    return (word_counts)
#统计词频
comment_pos_list = RemoveStop(comment_pos)
comment_neg_list = RemoveStop(comment_neg)
comment_mid_list = RemoveStop(comment_mid)
comment_pos_list

在这里插入图片描述

#生成词云对象  加上蒙版(图片没有,就注释掉了)
# image = np.array(Image.open('./daxiang.jpg'))
wc = WordCloud(
#     font_path='./SimHei.ttf',
    font_path='/System/Library/Fonts/STHeiti Light.ttc',
#     mask=image,
    max_words=150,
    max_font_size=60,
    min_font_size=5,
    background_color='white'
)
#好评
wc.generate_from_frequencies(comment_pos_list)
plt.figure(figsize=(12,12))
plt.imshow(wc)
plt.axis('off')
plt.show()
# 保存词云图
# wc.to_file('comment_pos.jpg')

在这里插入图片描述

#中评
wc.generate_from_frequencies(comment_mid_list)
plt.figure(figsize=(12,12))
plt.imshow(wc)
plt.axis('off')
plt.show()

在这里插入图片描述

#差评
wc.generate_from_frequencies(comment_neg_list)
plt.figure(figsize=(12,12))
plt.imshow(wc)
plt.axis('off')
plt.show()

在这里插入图片描述

# 生成热力图
data_corr=temp.pivot_table(columns='sentiment_value',index='subject',values='content',aggfunc="count").drop("其他")
ax=sns.heatmap(data_corr,cmap='rainbow', vmax=500,vmin=0,annot=False,linewidths=0.5)
font = font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Light.ttc') # mac系统下自带的中文字体,还有一部分字体放在/Library/Fonts/下
ax.set_title('耳机情感分析热力图', fontsize = 20, fontproperties=font)
Text(0.5, 1.0, '耳机情感分析热力图')

在这里插入图片描述

  • 5
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值