酷代码 AI
菜单
服务商

天问一号:B站评论情感分析与可视化全揭秘!

```python import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from snownlp import SnowNLP # 收集整理评论数据,并保存为CSV comments = [...] # 填入收集到的评论数据 df = pd.DataFrame(comments, columns=['comment']) df.to_csv('comments.csv', index=False) # 加载评论数据 df = pd.read_csv('comments.csv') # 对评论进行情感分析 def get_sentiment(text): s = SnowNLP(text) return s.sentiments df['sentiment'] = df['comment'].apply(get_sentiment) # 绘制不同情感类型评论的数量分布饼状图 sentiment_count = df['sentiment'].value_counts() sentiment_labels = ['Positive', 'Neutral', 'Negative'] plt.figure(figsize=(6, 6)) plt.pie(sentiment_count, labels=sentiment_labels, autopct='%1.1f%%') plt.title('Sentiment Distribution of Comments') plt.show() # 按月统计评论量,并绘制曲线图 df['timestamp'] = pd.to_datetime(df['timestamp']) df['month'] = df['timestamp'].dt.to_period('M') monthly_comments = df['month'].value_counts().sort_index() plt.figure(figsize=(10, 6)) sns.lineplot(x=monthly_comments.index, y=monthly_comments.values) plt.title('Monthly Comment Volume') plt.xlabel('Month') plt.ylabel('Number of Comments') plt.xticks(rotation=45) plt.show() # 统计评论获赞数排名前十,并绘制柱状图 top_10_comments = df.nlargest(10, 'likes') plt.figure(figsize=(10, 6)) sns.barplot(x=top_10_comments['comment'], y=top_10_comments['likes']) plt.title('Top 10 Comments by Likes') plt.xlabel('Comment') plt.ylabel('Number of Likes') plt.xticks(rotation=90) plt.show() ``` 注意:以上代码只提供了基本的框架,需要根据实际数据和需求进行相应的调整和修改。 ######[AI写代码神器 | 1057点数解答 | 2024-06-20 10:32:54]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]