阿里云:数据分析达人赛1:用户情感可视化分析Python

赛题背景

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

赛题任务

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

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


import pandas as pd  # 读取csv文件
import matplotlib.pyplot as plt  # 画图
import seaborn as sns
from wordcloud import WordCloud

# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文标签  # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
df=pd.read_csv('earphone_sentiment.csv')

#绘制词云图
list1=df['content'][df['sentiment_value']==1].values.tolist()
ComName_str=' '.join(list1)
stopwords=[',','了','楼主','?','。','啊','哈哈','呵呵','S','s','另外','当然','但是']
wc=WordCloud(scale=3,background_color="white",max_words=1000,width=800,height=500,font_path='msyh.ttc',stopwords=stopwords)
wc.generate_from_text(ComName_str)
wc.to_file('评论关键词(优).png')
wc.to_image()
#绘制词云图
list2=df['content'][df['sentiment_value']==0].values.tolist()
ComName_str=' '.join(list2)
wc=WordCloud(scale=3,background_color="white",max_words=1000,width=800,height=500,font_path='msyh.ttc',stopwords=stopwords)
wc.generate_from_text(ComName_str)
wc.to_file('评论关键词(良).png')
wc.to_image()
#绘制词云图
list3=df['content'][df['sentiment_value']==-1].values.tolist()
ComName_str=' '.join(list3)
wc=WordCloud(scale=3,background_color="white",max_words=1000,width=800,height=500,font_path='msyh.ttc',stopwords=stopwords)
wc.generate_from_text(ComName_str)
wc.to_file('评论关键词(差).png')
wc.to_image()
dict={}
for i in df['subject']:
    if i not in dict:
        dict[i]=1
    else:
        dict[i]=dict[i]+1

x=[]
y=[]
for key,value in dict.items():
     x.append(key)
     y.append(value)
plt.bar(x, y)
plt.xlabel("subject")
plt.ylabel("数量")
plt.title("主题-柱形图")
for a, b in zip(x, y):
 plt.text(a, b, '%d' % b, ha='center', va='bottom', fontsize=11)
plt.legend()
plt.show()
dict={}
for i in df['sentiment_word'][~df['sentiment_word'].isna()]:
    if i not in dict:
        dict[i]=1
    else:
        dict[i]=dict[i]+1
x=[]
y=[]
for key,value in dict.items():
     x.append(key)
     y.append(value)
plt.bar(x, y)
plt.xlabel("sentiment_word")
plt.ylabel("数量")
plt.title("情感词-柱形图")
for a, b in zip(x, y):
 plt.text(a, b, '%d' % b, ha='center', va='bottom', fontsize=11)
plt.legend()
plt.show()
#绘制热力图
df_copy=df.copy()
data = pd.DataFrame(df_copy)
inp = data.pivot_table(index='subject',columns='sentiment_value',values='sentiment_word',aggfunc='count')
with sns.axes_style('white'):
    sns.heatmap(inp,square=True,annot=True,cmap='hot')
plt.title('不同情感、不同主题情感词数热力图')
plt.xlabel('情感')
plt.ylabel('主题')
plt.show()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值