
Word Cloud (词云) - Python
在使用 wordcloud 模块的时候曾发现某些词语的频率(或者权重)是一样的,但是在生成的图形中字体大小却不一样。
1. [Word Cloud (词云) - JavaScript](https://www.cnblogs.com/yukiwu/p/10968816.html)2. [Word Cloud (词云) - R](https://www.cnblogs.com/yukiwu/p/10969250.html)3. [Word Cloud (词云) - Matlab](https://www.cnblogs.com/yukiwu/p/10971998.html)
发布日期:2021-05-09 09:08:19
浏览次数:16
分类:博客文章
本文共 1911 字,大约阅读时间需要 6 分钟。
##### >>What's Word Cloud词云 (Word Cloud)是对文本中出现频率较高的词语给予视觉化展示的图形, 是一种常见的文本挖掘的方法。目前已有多种数据分析工具支持这种图形,如Matlab, SPSS, SAS, R 和 Python 等等,也有很多在线网页能生成 word cloud, 例如
##### >> Create Word Cloud via PythonPython 可以使用 模块来生成词云。
安装 wordcloud, matplotlib 及其依赖模块。
准备文本。
我从维基百科中找到一段关于 的文字,以下将以这段文字为例。复制这段文字到 NotePad,并将其保存为 .*txt 文本格式。
- 运行 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 imagewordcloud = 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 pltplt.imshow(wordcloud, interpolation='bilinear')plt.axis("off")plt.show()
- 生成 word cloud。
在使用 wordcloud 模块的时候曾发现某些词语的频率(或者权重)是一样的,但是在生成的图形中字体大小却不一样。
Google 后找到开发作者的回答:
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.
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>> Related Blogs
1. [Word Cloud (词云) - JavaScript](https://www.cnblogs.com/yukiwu/p/10968816.html)2. [Word Cloud (词云) - R](https://www.cnblogs.com/yukiwu/p/10969250.html)3. [Word Cloud (词云) - Matlab](https://www.cnblogs.com/yukiwu/p/10971998.html)
发表评论
最新留言
很好
[***.229.124.182]2025年04月08日 00时21分28秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
权值初始化和与损失函数
2019-03-11
案例讨论
2019-03-11
传输层基本功能
2019-03-11
问题的计算复杂度:排序问题
2019-03-11
算法的伪码表示
2019-03-11
递推方程与算法分析
2019-03-11
主定理的应用
2019-03-11
动态规划算法的迭代实现
2019-03-11
最优装载问题
2019-03-11
最大团问题
2019-03-11
圆排列问题
2019-03-11
课程总结
2019-03-11
认识CMake及应用
2019-03-11
CMake的主体框架
2019-03-11
微积分(三)
2019-03-11
Oracle
2019-03-11
软件工程应用
2019-03-11
数据科学
2019-03-11
函数与高级变量
2019-03-11
键盘事件
2019-03-11