广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Word Cloud (词云) - Py
  • 758
分享到

Word Cloud (词云) - Py

CloudWordPy 2023-01-31 00:01:02 758人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

>>What's Word Cloud 词云 (Word Cloud)是对文本中出现频率较高的词语给予视觉化展示的图形, 是一种常见的文本挖掘的方法。目前已有多种数据分析工具支持这种图形,如Matlab, SPSS, SA


>>What's Word Cloud

词云 (Word Cloud)是对文本中出现频率较高的词语给予视觉化展示的图形, 是一种常见的文本挖掘的方法。目前已有多种数据分析工具支持这种图形,如Matlab, SPSS, SAS, R 和 python 等等,也有很多在线网页能生成 word cloud, 例如wordclouds.com

Word Cloud Example


>> Create Word Cloud via Python

Python 可以使用 wordcloud 模块来生成词云。

1) 安装 wordcloud, matplotlib 及其依赖模块。

2) 准备文本。

我从维基百科中找到一段关于 Word Cloud History 的文字,以下将以这段文字为例。复制这段文字到 NotePad,并将其保存为 .*txt 文本格式。

3) 运行 Python script。

"""
Python Example
===============
Generating a wordcloud from the txt file using Python.
"""

from wordcloud import WordCloud

# Read the whole text from txt.
fp = "C:/Users/yuki/Desktop/WordCloudHistory.txt"
text = open(fp).read()

# Generate a word cloud image
wordcloud = WordCloud(
font_path = "C:/windows/Fonts/BROADW.TTF", 
width = 600, #width of the canvas.
height = 400, #height of the canvas.
max_font_size = 60,
font_step = 1,
background_color = "white",
random_state = 1,
margin = 2,
colORMap = "tab20" #matplotlib colormap
).generate(text)

# Display the generated image in matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

4) 生成 word cloud。
Word Cloud Python


>> Notes

在使用 wordcloud 模块的时候曾发现某些词语的频率(或者权重)是一样的,但是在生成的图形中字体大小却不一样。

Google 后找到开发作者的回答:

wordcloud document


The algorithm might give more weight to the ranking of the words than their actual frequencies, depending on the max_font_size and the scaling heuristic.

GitHub issues


The scaling is relative to the size of the figure and the frequency of the words. The frequencies are normalized against the max frequency, so the absolute values are irrelevant.

大概是为了将词语尽可能地填满画布,wordcloud 算法会自动根据 max_font_size 和 scale 自动调整词语的权重。那么 wordcloud 生成的图形词语大小和他的词频(或者权重)的绝对值并不是一一对应的关系。

我觉得嘛:虽然这样画出的图形比较好看,但还是觉得有点奇怪,毕竟按词频大小展示词语应该是 word cloud 这种图形的精髓。


>> Sample Code

download here

--结束END--

本文标题: Word Cloud (词云) - Py

本文链接: https://www.lsjlt.com/news/182564.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作