当前位置:网站首页>在Python中创建文字云或标签云
在Python中创建文字云或标签云
2020-11-09 14:55:00 【人工智能遇见磐创】
作者|ISHA5 编译|Flin 来源|analyticsvidhya
介绍
从开始从事数据可视化工作的那一天起,我就爱上它了。我总是喜欢从数据中获得有用的见解。
在此之前,我只了解基本图表,例如条形图,散点图,直方图等,这些基本图表内置在tableau中,而Power BI则用于数据可视化。通过每天完成此任务,我遇到了许多新图表,例如径向仪表盘,华夫图等。
因此,出于好奇,最近我正在搜索数据可视化中使用的所有图表类型,这些词云引起了我的注意,我发现它非常有趣。直到现在,看到这个词云图像迫使我认为这些只是随机排列的图像,这些单词是随机排列的,但是我错了,而且一切都从这里开始。之后,我尝试使用Tableau和Power BI中的少量数据制作词云。在成功尝试之后,我想通过编写条形图,饼图和其他图表的代码来尝试使用它。
词云是什么?
定义:词云是一个简单但功能强大的可视化表示对象,用于文本处理,它以更大,更粗的字母和不同的颜色显示最常用的词。单词的大小越小,重要性就越小。
标签云的用途
1)社交媒体上的热门标签(Instagram,Twitter): 全世界,社交媒体都在寻找最新更新的趋势,因此,我们可以获取人们在其帖子中使用最多的标签。
2)媒体中的热门话题: 分析新闻报道,我们可以在头条新闻中找到关键字,并提取出前n个需求较高的主题,并获得所需的结果,即前n个热门媒体主题。
3)电子商务中的搜索词: 在电子商务购物网站中,网站所有者可以制作被搜索次数最多的购物商品的词云。这样,他就可以了解在特定时期内哪些商品需求量很大。
让我们开始在python中编码以实现这种词云
首先,我们需要在jupyter notebook中安装所有库。
在python中,我们将安装一个内置库wordcloud。在Anaconda命令提示符下,输入以下代码:
pip install wordcloud
如果你的anaconda环境支持conda,请输入:
conda install wordcloud
虽然,这可以直接在notebook本身中实现,只需在代码的开头添加“!”即可。
像这样:
!pip install wordcloud
现在,在这里,我将生成拥有任何主题的维基百科文本的词云。因此,我将需要一个Wikipedia库来访问Wikipedia API,可以通过在anaconda命令提示符下安装Wikipedia来完成,如下所示:
pip install wikipedia
现在我们还需要其他一些库,它们是numpy,matplotlib和pandas。
截至目前,我们需要的库就安装好了
import wikipedia
result= wikipedia.page("MachineLearning")
final_result = result.content
print(final_result)
机器学习维基百科页面的输出:
上图是我们通过检索Wikipedia的机器学习页面获得的输出的图像。在那里,我们还可以看到它可以向下滚动,这表示将检索整个页面。
在这里,我们还可以通过摘要方法得到页面的摘要,如:
result= wikipedia.summary("MachineLearning", sentences=5)
print(result)
这里我们有句子的参数,因此我们可以用它来检索特定的行数。
输出5个句子
让我们一起创建wordcloud
from wordcloud import WordCloud, StopWords
import matplotlib.pyplot as plt
def plot_cloud(wordcloud):
plt.figure(figsize=(10, 10))
plt.imshow(wordcloud)
plt.axis("off");
wordcloud = WordCloud(width = 500, height = 500, background_color='pink', random_state=10).generate(final_result)
plot_cloud(wordcloud)
停用词是没有任何含义的单词,例如‘is’, ‘are’, ‘an’, ‘I’ 等。
Wordcloud带有内置停用词库,该库将自动从文本中删除停用词。
有趣的是,我们可以通过stopwords.add()函数在python中添加停用词的选择。
Wordcloud方法将设置宽度和高度,我将它们都设置为500,背景色设置为粉红色。如果不添加随机状态,则每次运行代码时,词云都会看起来不同。应该将其设置为任何int值。
从上面的代码中,我们将获得这样的词云:
通过查看上图,我们可以看到机器学习是最常用的词,还有一些其他经常使用的词是模型,任务,训练和数据。因此,我们可以得出结论,机器学习是训练数据模型的任务。
我们还可以在这里通过背景颜色方法更改背景颜色,并通过colormap方法更改字体颜色,还可以在背景颜色中添加颜色的哈希码,但是mapcolor带有内置的特定颜色。
让我们通过使用哈希码将背景色更改为蓝绿色,将字体颜色更改为蓝色:
from wordcloud import WordCloud, StopWords
import matplotlib.pyplot as plt
def plot_cloud(wordcloud):
plt.figure(figsize=(10, 10))
plt.imshow(wordcloud)
plt.axis("off");
wordcloud = WordCloud(width = 500, height = 500, background_color='#40E0D0', colormap="ocean", random_state=10).generate(final_result)
plot_cloud(wordcloud)
在这里,我指定了ocean,如果我添加了一些错误的颜色图,jupyter将抛出一个值错误,并向我显示颜色图的可用选项,如下所示:
还可以使用PIL库在任何图像中实现词云。
尾注
在本文中,我们讨论了词云,词云的定义,应用领域以及使用jupyter notebook的python示例。
原文链接:https://www.analyticsvidhya.com/blog/2020/10/word-cloud-or-tag-cloud-in-python/
欢迎关注磐创AI博客站: http://panchuang.net/
sklearn机器学习中文官方文档: http://sklearn123.com/
欢迎关注磐创博客资源汇总站: http://docs.panchuang.net/
版权声明
本文为[人工智能遇见磐创]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4253699/blog/4708309
边栏推荐
- Lazy to write a document, swagger document does not smell
- Use treeview tree menu bar (recursively call database to create menu automatically)
- 【分布式】分布式锁都有哪些实现方案?
- Arthas install quick installation document
- What can DNS do besides resolving domain names?
- arthas无网络环境下离线安装方法
- Notes for csp-j / s 2020
- Rongyun has completed several hundred million RMB round D financing, and will continue to build global cloud communication capability
- A letter to myself
- 除了解析域名,DNS还能干吗?
猜你喜欢
Interface tests how to pass files in post requests
Decision tree algorithm theory
A certification and authorization solution based on. Net core - hulutong 1.0 open source
多线程真的比单线程快?
C语言—————三子棋游戏
Spark Learning (3) -- memory management and performance tuning
Position promotion | intelligent multimedia group of Microsoft Asia research institute recruits computer vision algorithm Intern
Rainbow sorting | Dutch flag problem
彩虹排序 | 荷兰旗问题
Why I strongly recommend custom development of small programs, these benefits you need to understand
随机推荐
瞧瞧,这样的『函数』才叫 Pythonic
arthas无网络环境下离线安装方法
Several methods of initializing singleton variable in go language
Some common types of error exception in Python
Technology and beauty are so expensive, it's better to find consultants | aalab enterprise consulting business
Application and practice of native map and web fusion technology
Learn with me. NETCORE EF core practical introduction, a look will
Position promotion | intelligent multimedia group of Microsoft Asia research institute recruits computer vision algorithm Intern
Chinese programmer vs Japanese programmer, full screen shame!
AutoCAD2020 完整版安装图文教程、注册激活破解方法
移动安全加固助力 App 实现全面、有效的安全防护
Viewing PDB files from the angle of assembly
Full link stress testing of moral integrity -- the evolution of corpus intelligence
块级元素和行内元素
MES系统在行业应用里区别于传统式管理
Guest interview: Wang Jian
International top journal radiology published the latest joint results of Huawei cloud, AI assisted detection of cerebral aneurysms
Explain three different authentication protocols in detail
百万年薪架构师之路:谈应用系统架构设计
Offline installation method of Arthas without network environment