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