[英]How do I remove words from my wordcloud? (Python 3)
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import wordcloud
from wordcloud import WordCloud,STOPWORDS
# Read the whole text.
remarks = open(r'C:\Users\marmar\Documents\Remarks.txt').read()
#Create words over an image
mask = np.array(Image.open(r'C:\users\marmar\Documents\cloud.png'))
#set the stopwords list
stopwords= set(STOPWORDS)
#append new words to the stopwords list
new_words =open(r'C:\Users\marmar\comments.txt').read()
new_stopwords=stopwords.union(new_words)
#generate the word cloud with parameters
wc = WordCloud(background_color="white",
max_words=2000,
mask=mask,
min_font_size =12,
max_font_size=20,
relative_scaling = 0.5,
stopwords=new_stopwords,
normalize_plurals= True)
wc.generate(remarks)
plt.figure(figsize=(25,25))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
#Show the wordcloud
plt.show()
基本上,我使用Python 3(Jupyter Notebook)创建一个带有实际云图的wordcloud。 WordCloud包实际上有自己的停用词功能。 但是,我想在停用词列表中包含一些我不希望在云中看到的单词。 我试图在该文本文件中包含一些单词,但我可以看到我的云中的单词。 例如,文本文件如下所示:客户,CSR客户,满意,项目已完成
如何在列表中添加更多单词。 我尝试添加,追加这两个功能,但它们无法正常工作。
先感谢您。
啊哈! 这是因为我在文本文件中用逗号分隔了我的单词。
对于那些构建wordcloud的人来说,只需用空格分隔单词即可。 不需要标点符号。 @RagingRoosevelt使用“拆分”功能是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.