企业官方网站的作用,搜索引擎网站,谷歌网页翻译,自己建设小城市网站得多少钱-------------词云图集合-------------
WordCloud去掉停用词#xff08;fit_wordsgenerate#xff09;的2种用法
通过词频来绘制词云图#xff08;jiebaWordCloud#xff09;
Python教程95#xff1a;去掉停用词词频统计jieba.tokenize示例用法
将进酒—李白process_t…-------------词云图集合-------------
WordCloud去掉停用词fit_wordsgenerate的2种用法
通过词频来绘制词云图jiebaWordCloud
Python教程95去掉停用词词频统计jieba.tokenize示例用法
将进酒—李白process_text词频统计及词频可视化分析
使用wordcloud模块绘制一个自定义的词云图形状
使用WordCloud模块中repeat参数做一个关键字重复的词云图
关于词云图显示异常出现乱码的解决办法
盘点WordCloud模块词云图的相关知识点
Python源码05使用Pyecharts画词云图图
在WordCloud中去掉停用词可以通过设置stopwords参数来实现也可以通过下面字典数据的过滤方法来去掉不想要的词语。停用词是指在文本中频繁出现但对文本含义贡献很小的词语如虚词、助词、介词、连词等。下面写的2个例子可以给大家参考一下。
1.W.generate通常结合stopwords过滤停用词的方法运行代码就会生成如下图。可以看到原文本杨过是欧阳锋的义子他的黯然销魂掌可以和郭靖的降龙十八掌媲美文本里面的 是和的它等需要都被去掉了。
# -*- coding: utf-8 -*-
# Author : 小红牛
# 微信公众号WdPython
import jieba
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt# 1.读取文本
text 杨过是欧阳锋的义子他的黯然销魂掌可以和郭靖的降龙十八掌媲美。
print(1.原文本:.center(50, -))
print(text)
# 2.分词
cut_word jieba.cut(text, cut_allFalse)
cut_word .join(cut_word)
print(2.jieba分词后的内容:.center(50, -))
print(cut_word)
# 3.准备停用词表
stopwords set(STOPWORDS)
print(3.WordCloud自带的英文停用词表:.center(50, -))
print(stopwords)
# 自定义添加停用词
stopwords.add(是)
stopwords.add(的)
stopwords.add(和)
stopwords.add(他)
stopwords.add(可以)# 4.创建WordCloud对象并生成词云stopwordsstopwords,
w WordCloud(background_colorWHITE, stopwordsstopwords, height400,width700, font_pathsimkai.ttf)w.generate(cut_word)
# 5.保存词云图图片
w.to_file(cloud.png)
# 显示词云图
plt.imshow(w)
plt.axis(off)
plt.show()
print(词云图生成完成。)输出内容
----------------------1.原文本:----------------------
杨过是欧阳锋的义子他的黯然销魂掌可以和郭靖的降龙十八掌媲美。
------------------2.jieba分词后的内容:------------------
杨过 是 欧阳锋 的 义子 他 的 黯然销魂 掌 可以 和 郭靖 的 降龙十八掌 媲美 。
--------------3.WordCloud自带的英文停用词表:---------------
{youll, doesnt, yourself, after, by, were, other, and, just, this, whens, hence, own, ours, myself, wont, otherwise, what, on, can, else, well, k, him, havent, theyll, shes, id, there, itself, of, youve, theres, herself, off, his, once, thats, very, was, ever, would, until, why, me, had, wheres, does, over, shall, if, since, whats, a, some, the, shouldnt, we, where, any, an, she, them, hadnt, hell, hes, who, its, he, ourselves, also, you, most, were, ill, r, with, at, yourselves, have, when, www, mustnt, about, up, did, lets, are, few, be, being, too, their, cannot, more, to, heres, whys, having, didnt, hers, no, in, under, how, again, hed, her, so, theyre, shell, against, however, my, couldnt, im, wouldnt, shed, arent, been, above, before, those, wed, such, hasnt, isnt, not, only, http, theyd, nor, because, i, youd, which, they, whos, it, could, into, cant, out, while, down, hows, ought, am, dont, each, himself, through, ive, whom, from, is, therefore, themselves, our, all, but, same, shant, youre, as, between, has, then, weve, both, its, these, should, like, or, theyve, do, theirs, werent, below, yours, com, for, than, here, get, that, wasnt, your, further, doing, during}2.通过w.fit_words参数为字典类型Counter参数可以是字符串也可以是可迭代的对象返回字典类型jieba参数是字符串返回是generator类型。下面代码中会按照关键字词频的阈值去自定义过滤不符合条件的词频和字典数据知识点的用法一样以上2种处理过滤的结果是一样。个人觉得使用w.fit_words比W.generate生成词云图要更直观一点因为它可以根据词频的大小在词云图上反应出字体的大小。简单说某个词频非常高那么它的字号在词云图上显示的就越大。 # -*- coding: utf-8 -*-
# Author : 小红牛
# 微信公众号WdPython
import re
import jieba
from collections import Counter
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt# 1.读取文本
text 杨过是欧阳锋的义子他的黯然销魂掌可以和郭靖的降龙十八掌媲美。
# 使用正则只取中文的字符过滤英文数字各种标点符号等等
text re.findall([\u4e00-\u9fff], text)
text .join(text)
print(1.只取中文文本:.center(50, -))
print(text)
# 2.分词
cut_word jieba.cut(text, cut_allFalse)
word_freq Counter(cut_word)
print(2.过滤前的分词词频:.center(50, -))
print(word_freq)# 自定义定义过滤条件
min_freq 0 # 最小词频阈值
exclude_words {是, 的, 和, 可以, 他} # 要排除的词列表# 过滤词频字典
filtered_word_freq {word: freq for word, freq in word_freq.items()if freq min_freq and word not in exclude_words}print(3.过滤后词频:.center(50, -))
print(filtered_word_freq)# 4.创建WordCloud对象并生成词云
w WordCloud(background_colorWHITE, height400, width700,font_pathsimkai.ttf)w.fit_words(filtered_word_freq)
# 5.保存词云图图片
w.to_file(cloud.png)
# 显示词云图
plt.imshow(w)
plt.axis(off)
plt.show()
print(词云图生成完成。)输出内容
--------------------1.只取中文文本:---------------------
杨过是欧阳锋的义子他的黯然销魂掌可以和郭靖的降龙十八掌媲美
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\Ms-xiao\AppData\Local\Temp\jieba.cache
Loading model cost 1.151 seconds.
Prefix dict has been built successfully.
--------------------2.过滤前的词频:---------------------
Counter({的: 3, 杨过: 1, 是: 1, 欧阳锋: 1, 义子: 1, 他: 1, 黯然销魂: 1, 掌: 1, 可以: 1, 和: 1, 郭靖: 1, 降龙十八掌: 1, 媲美: 1})
---------------------3.过滤后词频:---------------------
{杨过: 1, 欧阳锋: 1, 义子: 1, 黯然销魂: 1, 掌: 1, 郭靖: 1, 降龙十八掌: 1, 媲美: 1}
词云图生成完成。完毕感谢您的收看
----------★★历史博文集合★★----------
我的零基础Python教程Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具 NumPy Pygame