当前位置:网站首页>在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
边栏推荐
猜你喜欢

CAD2020下载AutoCAD2020下载安装教程AutoCAD2020中文下载安装方法

I interviewed a 33 year old Android programmer, who could only program for Baidu, but wanted 25K, which was met by me

Use treeview tree menu bar (recursively call database to create menu automatically)

Why I strongly recommend custom development of small programs, these benefits you need to understand

Depth analysis based on synchronized lock

Android byte beat one side, was hanged by the interviewer! Fortunately, we got the offer from three sides

Do programmers pay too much to work overtime? Should programmer's salary be reduced? Netizen: let go of other hard pressed programmers

EMQ X 在中国建设银行物联网平台中的应用EMQ X 在中国建设银行物联网平台中的应用

Full link stress testing of moral integrity -- the evolution of corpus intelligence

ImmutableMap的put方法问题
随机推荐
C语言—————三子棋游戏
MES system is different from traditional management in industry application
国际顶刊Radiology发表华为云最新联合成果,AI辅助检测脑动脉瘤
ImmutableMap的put方法问题
毕业一年后接私活赚了10w,还拿了几家大厂offer!
Is multithreading really faster than single threading?
Online course of tutorial system processing is in progress
Android 集成支付的四部曲
Technology and beauty are so expensive, it's better to find consultants | aalab enterprise consulting business
How can you be a big data worker with an annual salary of 40W if you don't work hard?
c语言小白学习历程第六篇
7-10倍写入性能提升:剖析WiredTiger数据页无锁及压缩黑科技
IDEA rest-client,会了它我还没打开过postman
Android studio import customized framework classess.jar As 4.0.1 version is valid for pro test
Efficient estimation of word representations in vector space
Android NDK development and actual combat WeChat official account 2-D code detection
Tutorial system unity online course double 11 preferential registration is in progress
Android byte beat one side, was hanged by the interviewer! Fortunately, we got the offer from three sides
Several methods of initializing singleton variable in go language
从汇编的角度看pdb文件