鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 不二如是

【最新唐探三的数据】谁能做成最帅数据可视化??

[复制链接]
发表于 2021-2-22 00:32:39 | 显示全部楼层
本帖最后由 孤独的嫖客 于 2021-2-23 12:54 编辑

先上图:
图片1.png
申明:采用图像仅用作美化词云展示效果,评论内容与图像无关!
更新2021年2月23日
  • 修改了目录结构,使其看起来更加清晰明了
  • 取消了jieba分词,让词云还原评论本质
  • 尝试使用pyecharts作图(TODO)未完成
功能实现:
  • 获取程序运行路径,方便服务器部署
  • pandas 读取csv指定列保存到txt
  • 使用中文分词 jieba 对评论做语言处理
  • 使用停用词排除干扰
  • 使用 Worldcloud 创建词云
  • 使用自定义画像美化词云


注意:
worldcloud 使用pip install 无法直接安装成功(macos,linux 未测试)
安装worldcloud 需要whl文件,imgs文件夹中附带了适配win10 64位 python3.9版本的whl文件,相同环境可以直接使用 pip install 绝对路径+wordcloud-1.8.1-cp39-cp39-win_amd64.whl安装
其他python版本的可以到这里下载whl文件
  1. https://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud
复制代码
最后建议脚本运行环境为python 3.8以上 因为脚本使用了3.8版本的新特性
代码展示:
  1. from wordcloud import WordCloud
  2. from imageio import imread
  3. import pandas as pd
  4. import os
  5. import logging as log

  6. # 设置停用词
  7. stopword = ['什么', '没有', '真的', '这种', '那里',
  8.             '就是', '可以', '一个', '还是', '最后',
  9.             '这个', '但是', '总之', '不要', '这部',
  10.             '一部', '故事', '这么', '出来', '应该',
  11.             '电影', '不是', '为了', '还有', '你们',
  12.             '系列', '觉得', '自己', '已经', '整个',
  13.             '一直', '还是', '出来', '完全', '看到'
  14.             '那个', '之后', '除了', '其中', '长泽雅美',
  15.             '陈思诚', '妻夫木聪', '浅野忠信', '铃木保奈美',
  16.             'the','world','大过年的','三浦友和']

  17. # def fenci():
  18. #     # 设置用户字典
  19. #     jieba.load_userdict(parent_dir+"\\imgs\\自定义字典.txt")
  20. #     with open(filepath, "r", encoding="utf8") as f:
  21. #         file = f.read()
  22. #
  23. #     # 数据清洗
  24. #     wordslist = jieba.lcut(file)
  25. #     with open(parent_dir + "\\tmp\\分词结果.txt", "w", encoding="utf8") as f:
  26. #         f.write(" ".join(wordslist))
  27. #     log.info("数据分词完毕,开始清洗...")
  28. #     regworld = list()
  29. #     for i in wordslist:
  30. #         if len(i) > 1:
  31. #             regworld.append(i)
  32. #
  33. #
  34. #     outworld = list()
  35. #     for i in regworld:
  36. #         if i not in stopword:
  37. #             outworld.append(i)
  38. #     with open(parent_dir+"\\tmp\\清洗结果.txt", "w", encoding="utf8") as f:
  39. #         f.write(" ".join(outworld))
  40. #     words = " ".join(outworld)
  41. #     return words


  42. def creatCiYun(words):
  43.     log.info("数据清洗完毕,开始画图...")
  44.     fontpath = parent_dir + "\\imgs\\Alibaba-PuHuiTi-Regular.ttf"
  45.     bgpic = imread(parent_dir + "\\imgs\\3.png")
  46.     wc = WordCloud(font_path=fontpath,stopwords=stopword,
  47.                    mask=bgpic,scale=3,
  48.                    background_color='white', )
  49.     wc = wc.generate(words)
  50.     wc.to_file(parent_dir + "\\tmp\\唐探3.png")


  51. if __name__ == '__main__':
  52.     # 日志初始化
  53.     log.basicConfig(level=log.INFO, format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
  54.     # 获取相对路径
  55.     parent_dir = os.path.dirname(os.path.abspath(__file__))
  56.     log.debug(f"程序运行路径{parent_dir}")
  57.     filepath = parent_dir + "\\tmp\\tempdata.txt"
  58.     # 判断程序是否首次运行,是则读取csv,非首次运行不需要读取csv
  59.     try:
  60.         f = open(filepath, "r")
  61.         f.close()
  62.     except IOError:
  63.         log.info("初始化中....")
  64.         f = open(filepath, "w", encoding="utf8")
  65.         f.close()
  66.         # 读取评论
  67.         df = pd.read_csv(parent_dir + '\\imgs\\Ori.csv', usecols=[3])
  68.         # 提取所有评论写入txt,为分词做准备工作
  69.         df.to_csv(filepath, sep="\t", index=False, header=None)
  70.     # data = fenci()
  71.     with open(parent_dir+"\\tmp\\tempdata.txt","r",encoding="utf8") as f:
  72.         data = f.read()
  73.     creatCiYun(data)
复制代码

补充一下项目地址,不然没有图像,字体资源,程序无法直接运行
  1. https://gitee.com/Rt_hum/three-words-of-tang-tan
复制代码

感谢论坛的活动,学习了词云可视化





评分

参与人数 1荣誉 +6 鱼币 +20 贡献 +6 收起 理由
不二如是 + 6 + 20 + 6 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-24 23:49:27 | 显示全部楼层
看我的!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-19 18:59:48 | 显示全部楼层
看我的!……
半天没相应
咋至今也没有最佳答案呢。?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-4-9 17:55:53 | 显示全部楼层
这是一个很有趣的挑战!在这里,我将演示如何使用Python库Pandas和Matplotlib来处理和可视化唐探3口碑数据。

首先,让我们导入必要的库并加载数据:

```python
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('Ori.csv')
```

接下来,我们可以检查一下数据的前几行和概览:

```python
print(df.head())
print(df.info())
```

根据数据概览,我们可以看到数据集包含4个列:电影名称、评分、评论和时间戳。其中,时间戳列没有用,可以删除。

```python
df.drop('timestamp', axis=1, inplace=True)
```

接下来,让我们来看一下唐探3的整体评分分布:

```python
plt.hist(df['rating'], bins=10, edgecolor='black')
plt.title('唐探3评分分布')
plt.xlabel('评分')
plt.ylabel('数量')
plt.show()
```

这将绘制唐探3的评分直方图。

我们也可以使用箱形图来显示唐探3的评分分布:

```python
plt.boxplot(df['rating'], vert=False)
plt.title('唐探3评分分布')
plt.xlabel('评分')
plt.yticks([])
plt.show()
```

下一步,我们将使用词云来显示唐探3评论中最常用的词汇。我们将使用Python库wordcloud和jieba来实现。

```python
from wordcloud import WordCloud
import jieba

# 将所有评论拼接成一个字符串
comments = ' '.join(df['comment'])

# 使用jieba进行中文分词
words = ' '.join(jieba.cut(comments))

# 生成词云
wc = WordCloud(background_color='white', width=800, height=400).generate(words)

# 显示词云
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()
```

最后,我们可以使用柱形图来显示唐探3的评分随时间的变化趋势:

```python
# 将时间戳转换为datetime类型
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M:%S')

# 按照日期对评分进行平均
df = df.groupby('date')['rating'].mean().reset_index()

# 绘制柱形图
plt.bar(df['date'], df['rating'])
plt.title('唐探3评分变化趋势')
plt.xlabel('日期')
plt.ylabel('评分')
plt.show()
```

这将绘制唐探3的评分随时间的变化趋势图。

综上,这些代码可以让我们从不同角度分析唐探3的口碑数据。你可以进一步加工这些代码,制作出更漂亮的图表。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 10:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表