赛题1:数据分析达人赛1:汽车用户情感分析可视化
收藏阿里云天池2026-06-10 更新2024-03-07 收录
下载链接:
https://tianchi.aliyun.com/dataset/133083
下载链接
链接失效反馈官方服务:
资源简介:
# %load 赛题1.py
"""
赛题1:
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import font_manager
font_manager.fontManager.addfont('./SimHei.ttf')
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置汉字字体,优先使用黑体
plt.rcParams['font.size'] = 12 # 设置字体大小
plt.rcParams['axes.unicode_minus'] = False # 设置正常显示负号
# Step1: 数据加载
df = pd.read_csv('./earphone_sentiment.csv')
pd.set_option('max_columns',None)
# df.to_excel('arphone_sentiment1.xlsx',index=False) 也可转化为Excel格式
# Step2: 数据探索
print(df)
print(df.info())
print("***************************以下为可视化部分******************************************")
# Task2 绘制不同主题柱状图,分别使用matplotlib,seaborn绘制
plt.figure(figsize=(20, 20)) #创建画布,并设置画布大小
plt.subplot(1,2,1) #设置plt子图位置
bar_subject=df['subject'].value_counts() #统计各主题评论数量以绘制不同主题柱状图
df_subject=pd.DataFrame(bar_subject) #使用dataframe创建字典,其中索引列为横轴,值列为纵轴
print(df_subject)
df_subject_x=list(df_subject.index) #获取索引列作为X轴
df_subject_y=bar_subject #值列作为Y轴
# print(df_subject_x)
# print(df_subject_y)
plt.bar(df_subject_x,df_subject_y) #绘制plt柱状图
for a, b in zip(df_subject_x, df_subject_y): #设置数据标签可见
plt.text(a, b + 0.005, str(b), ha='center', va='bottom', fontsize=10)
plt.xlabel('不同主题') #设置X轴标签
plt.ylabel('评价数量') #设置Y轴标签
plt.title('不同主题柱状图_plt绘制') #为柱状图添加标题
plt.subplot(1,2,2) #设置sns子图位置
barchart_sns_subject=sns.barplot(x=df_subject_x,y=df_subject_y,data=df_subject) #绘制sns柱状图
barchart_sns_subject.set(xlabel='不同主题',ylabel='评价数量',title='不同主题柱状图_sns绘制') #设置X轴Y轴标签和标题
barchart_sns_subject.get_figure().savefig('barchart_subject.png') #单独保存png格式图片
plt.show()
# Task2 绘制不同情感柱状图,使用matplotlib绘制
bar_sentiment=df['sentiment_value'].value_counts() #统计各情感评论数量以绘制不同情感柱状图
df_sentiment=bar_sentiment.to_dict() #使用to_dict创建字典,其中键的列表为横轴,值的列表为纵轴
print(df_sentiment)
df_sentiment_x=list(df_sentiment.keys()) #获取包含所有键的列表作为X轴
df_sentiment_y=list(df_sentiment.values()) #获取包含所有值的列表作为X轴
df_sentiment_x1={0:'一般的情感(0)',1:'好的情感(1)',-1:'不好的情感(-1)'}
df_sentiment_x=[df_sentiment_x1[i] if i in df_sentiment_x1 else i for i in df_sentiment_x] #将X轴的0,1,-1分别用文字替换,更加直观
# print(df_sentiment_x)
# print(df_sentiment_y)
plt.bar(df_sentiment_x,df_sentiment_y) #绘制plt柱状图
for a, b in zip(df_sentiment_x,df_sentiment_y): #设置数据标签可见
plt.text(a, b + 0.005, str(b), ha='center', va='bottom', fontsize=10)
plt.xlabel('不同情感',fontsize=14) #设置X轴标签
plt.ylabel('评价数量',fontsize=14) #设置Y轴标签
plt.title('不同情感柱状图',fontsize=18) #为柱状图添加标题
plt.savefig("barchart_sentiment.png") #单独保存png格式图片
plt.show()
# Task2 绘制不同情感词柱状图,使用matplotlib绘制
bar_sentiment_word=df['sentiment_word'].value_counts() #统计各情感词评论数量以绘制不同情感词柱状图
df_sentiment_word=bar_sentiment_word.to_dict() #使用to_dict创建字典,其中键的列表为横轴,值的列表为纵轴
print(df_sentiment_word)
df_sentiment_word_x=list(df_sentiment_word.keys()) #获取包含所有键的列表作为X轴
df_sentiment_word_y=list(df_sentiment_word.values()) #获取包含所有值的列表作为X轴
# print(df_sentiment_word_x)
# print(df_sentiment_word_y)
plt.bar(df_sentiment_word_x,df_sentiment_word_y) #绘制plt柱状图
for a, b in zip(df_sentiment_word_x,df_sentiment_word_y): #设置数据标签可见
plt.text(a, b + 0.005, str(b), ha='center', va='bottom', fontsize=10)
plt.xticks(rotation=90,fontsize=10) #设置X轴内容竖排显示
plt.xlabel('不同情感词',fontsize=14) #设置X轴标签
plt.ylabel('评价数量',fontsize=14) #设置Y轴标签
plt.title('不同情感词柱状图',fontsize=18) #为柱状图添加标题
plt.savefig("barchart_sentiment_word.png") #单独保存png格式图片
plt.show()
# Task3 绘制不同主题 不同情感 不同情感词的相关性系数热力图
df['sentiment_value']=df['sentiment_value'].replace({0:'一般的情感(0)',-1:'不好的情感(-1)',1:'好的情感(1)'}) #将原表中0,1,-1分别用文字替换更加直观
df=df.drop('content',1)
df=df.drop('content_id',1)
df_encoded=pd.get_dummies(df)
# print(df_encoded)
print(df_encoded.info())
plt.figure(figsize=(30,30))
sns.heatmap(df_encoded.corr(),annot=False,cmap='coolwarm')
plt.xlabel('不同主题、情感和情感词',fontsize=14) #设置X轴标签
plt.ylabel('不同主题、情感和情感词',fontsize=14) #设置Y轴标签
plt.title('不同主题、不同情感和不同情感词热力图',fontsize=18) #为热力图添加标题
plt.show()
# Task3 绘制不同主题 不同情感的相关性系数热力图
df=df.drop('sentiment_word',1)
df_encoded=pd.get_dummies(df)
print(df_encoded)
print(df_encoded.info())
plt.figure(figsize=(30,30))
sns.heatmap(df_encoded.corr(),annot=True,cmap='coolwarm')
plt.xlabel('不同主题和情感',fontsize=14) #设置X轴标签
plt.ylabel('不同主题和情感',fontsize=14) #设置Y轴标签
plt.title('不同主题和不同情感热力图',fontsize=18) #为热力图添加标题
plt.show()
# %load 赛题1.py
"""
竞赛题目1:
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import font_manager
font_manager.fontManager.addfont('./SimHei.ttf')
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置汉字字体,优先使用黑体
plt.rcParams['font.size'] = 12 # 设置字体大小
plt.rcParams['axes.unicode_minus'] = False # 配置正常显示负号
# 步骤1:数据加载
df = pd.read_csv('./earphone_sentiment.csv')
pd.set_option('max_columns',None)
# df.to_excel('arphone_sentiment1.xlsx',index=False) 亦可导出为Excel格式
# 步骤2:数据探索
print(df)
print(df.info())
print("*************************** 以下为可视化部分 ******************************************")
# 任务2:分别使用matplotlib、seaborn绘制不同主题的柱状图
plt.figure(figsize=(20, 20)) # 创建画布并设置画布尺寸
plt.subplot(1,2,1) # 配置matplotlib子图布局位置
bar_subject=df['subject'].value_counts() # 统计各主题的评论数量,用于绘制主题分布柱状图
df_subject=pd.DataFrame(bar_subject) # 将统计结果转换为DataFrame,索引列为横轴数据,值列为纵轴数据
df_subject_x=list(df_subject.index) # 提取索引列作为柱状图X轴数据
df_subject_y=bar_subject # 提取统计值作为柱状图Y轴数据
# print(df_subject_x)
# print(df_subject_y)
plt.bar(df_subject_x,df_subject_y) # 绘制matplotlib柱状图
for a, b in zip(df_subject_x, df_subject_y): # 添加柱状图数据标签
plt.text(a, b + 0.005, str(b), ha='center', va='bottom', fontsize=10)
plt.xlabel('不同主题') # 设置X轴轴标签
plt.ylabel('评论数量') # 设置Y轴轴标签
plt.title('不同主题柱状图_matplotlib绘制') # 为柱状图添加标题
plt.subplot(1,2,2) # 配置seaborn子图布局位置
barchart_sns_subject=sns.barplot(x=df_subject_x,y=df_subject_y,data=df_subject) # 绘制seaborn柱状图
barchart_sns_subject.set(xlabel='不同主题',ylabel='评论数量',title='不同主题柱状图_seaborn绘制') # 设置轴标签与图表标题
barchart_sns_subject.get_figure().savefig('barchart_subject.png') # 将图表导出为PNG格式文件
plt.show()
# 任务2:使用matplotlib绘制不同情感分布的柱状图
bar_sentiment=df['sentiment_value'].value_counts() # 统计各情感类别的评论数量,用于绘制情感分布柱状图
df_sentiment=bar_sentiment.to_dict() # 将统计结果转换为字典,键列表作为X轴数据,值列表作为Y轴数据
print(df_sentiment)
df_sentiment_x=list(df_sentiment.keys()) # 提取字典键作为X轴数据
df_sentiment_y=list(df_sentiment.values()) # 提取字典值作为Y轴数据
df_sentiment_x1={0:'一般的情感(0)',1:'好的情感(1)',-1:'不好的情感(-1)'}
df_sentiment_x=[df_sentiment_x1[i] if i in df_sentiment_x1 else i for i in df_sentiment_x] # 将X轴的数值标签替换为直观的文本描述
# print(df_sentiment_x)
# print(df_sentiment_y)
plt.bar(df_sentiment_x,df_sentiment_y) # 绘制matplotlib柱状图
for a, b in zip(df_sentiment_x,df_sentiment_y): # 添加柱状图数据标签
plt.text(a, b + 0.005, str(b), ha='center', va='bottom', fontsize=10)
plt.xlabel('情感类别',fontsize=14) # 设置X轴轴标签
plt.ylabel('评论数量',fontsize=14) # 设置Y轴轴标签
plt.title('不同情感分布柱状图',fontsize=18) # 为图表添加标题
plt.savefig("barchart_sentiment.png") # 将图表导出为PNG格式文件
plt.show()
# 任务2:使用matplotlib绘制不同情感词的分布柱状图
bar_sentiment_word=df['sentiment_word'].value_counts() # 统计各情感词的出现次数,用于绘制情感词分布柱状图
df_sentiment_word=bar_sentiment_word.to_dict() # 将统计结果转换为字典
print(df_sentiment_word)
df_sentiment_word_x=list(df_sentiment_word.keys()) # 提取字典键作为X轴数据
df_sentiment_word_y=list(df_sentiment_word.values()) # 提取字典值作为Y轴数据
# print(df_sentiment_word_x)
# print(df_sentiment_word_y)
plt.bar(df_sentiment_word_x,df_sentiment_word_y) # 绘制matplotlib柱状图
for a, b in zip(df_sentiment_word_x,df_sentiment_word_y): # 添加柱状图数据标签
plt.text(a, b + 0.005, str(b), ha='center', va='bottom', fontsize=10)
plt.xticks(rotation=90,fontsize=10) # 将X轴标签旋转90度以避免重叠
plt.xlabel('情感词',fontsize=14) # 设置X轴轴标签
plt.ylabel('出现次数',fontsize=14) # 设置Y轴轴标签
plt.title('不同情感词分布柱状图',fontsize=18) # 为图表添加标题
plt.savefig("barchart_sentiment_word.png") # 将图表导出为PNG格式文件
plt.show()
# 任务3:绘制包含主题、情感、情感词的相关性系数热力图
df['sentiment_value']=df['sentiment_value'].replace({0:'一般的情感(0)',-1:'不好的情感(-1)',1:'好的情感(1)'}) # 将情感数值替换为直观的文本描述
df=df.drop('content',1)
df=df.drop('content_id',1)
df_encoded=pd.get_dummies(df) # 对分类特征进行独热编码
# print(df_encoded)
print(df_encoded.info())
plt.figure(figsize=(30,30))
sns.heatmap(df_encoded.corr(),annot=False,cmap='coolwarm') # 绘制相关性热力图,不显示标注文本
plt.xlabel('主题、情感与情感词',fontsize=14) # 设置X轴轴标签
plt.ylabel('主题、情感与情感词',fontsize=14) # 设置Y轴轴标签
plt.title('主题、情感与情感词相关性热力图',fontsize=18) # 为热力图添加标题
plt.show()
# 任务3:绘制主题与情感的相关性系数热力图
df=df.drop('sentiment_word',1)
df_encoded=pd.get_dummies(df) # 对分类特征进行独热编码
print(df_encoded)
print(df_encoded.info())
plt.figure(figsize=(30,30))
sns.heatmap(df_encoded.corr(),annot=True,cmap='coolwarm') # 绘制相关性热力图,显示标注文本
plt.xlabel('主题与情感类别',fontsize=14) # 设置X轴轴标签
plt.ylabel('主题与情感类别',fontsize=14) # 设置Y轴轴标签
plt.title('主题与情感相关性热力图',fontsize=18) # 为热力图添加标题
plt.show()
提供机构:
阿里云天池
创建时间:
2022-06-21
搜集汇总
数据集介绍

背景与挑战
背景概述
该数据集是用于汽车用户情感分析竞赛的数据,包含用户评论的主题分类、情感值(正面、负面、中性)和情感词等字段,适用于情感分析和可视化研究。
以上内容由遇见数据集搜集并总结生成



