Python WordCloud没有删除自定义停止字
我正在尝试添加要从我的单词云中删除的停止词。似乎突然间,我额外的停止语没有被添加。它以前工作过 我已经将问题归结为这里显示的内容以及循环中的第一个单词cloud。您可以在顶部看到,尽管我将“产品”添加到了stopwords列表中,但它仍然存在。另外两个停止词被恰当地删除了。我将搭配设置为False 我尝试了1.5.0和1.6.0版本Python WordCloud没有删除自定义停止字,python,word-cloud,stop-words,Python,Word Cloud,Stop Words,我正在尝试添加要从我的单词云中删除的停止词。似乎突然间,我额外的停止语没有被添加。它以前工作过 我已经将问题归结为这里显示的内容以及循环中的第一个单词cloud。您可以在顶部看到,尽管我将“产品”添加到了stopwords列表中,但它仍然存在。另外两个停止词被恰当地删除了。我将搭配设置为False 我尝试了1.5.0和1.6.0版本 from wordcloud import WordCloud, STOPWORDS import pandas as pd import collections
from wordcloud import WordCloud, STOPWORDS
import pandas as pd
import collections
import matplotlib.pyplot as plt
for i in range(20):
print(i)
wordcloud = WordCloud(stopwords=["product", "and", "the"], background_color='white', collocations=False).generate(clusterStrings[i])
# Display the generated image:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
您正在每个循环上创建一个新实例,并且您正在替换而不是添加额外的停止字。尝试创建wc并将stopwords添加到循环外部的已知stopwords中
from wordcloud import WordCloud, STOPWORDS
import pandas as pd
import collections
import matplotlib.pyplot as plt
...
# create the instance only once and add stopwords
stopwords = set(STOPWORDS)
stopwords.add(["product", "and", "the"])
wordcloud = WordCloud(stopwords=stopwords, background_color='white', collocations=False)
for i in range(20):
print(i)
wordcloud.generate(clusterStrings[i])
# Display the generated image:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
尝试此功能:
(参考)
最初我是在添加原始的stopwords。我只是将代码改为这个,以显示三个显式stopwords中的一个仍在显示。我尝试了你的代码,但只是在集合中添加了“产品”。但“产品”仍然出现,它们非常奇怪。尝试在clusterString中搜索“product”,并检查是否存在隐藏空间,例如“product”。另外,使用product获取clusterString,并在clusterString[x]中选中
“product”,尝试此代码,但在将其添加到stopwords后仍会显示“product”一词。此外,请确保标记文件,使其逐字分解。使用拆分()
和“”。join()
因此它将删除中间任何不必要的空格
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
def plot_wordcloud(text, mask=None, max_words=200, max_font_size=100, figure_size=(24.0,16.0),
title = None, title_size=40, image_color=False):
stopwords = set(STOPWORDS)
more_stopwords = {'one', 'br', 'Po', 'th', 'sayi', 'fo', 'Unknown'}
stopwords = stopwords.union(more_stopwords)
wordcloud = WordCloud(background_color='black',
stopwords = stopwords,
max_words = max_words,
max_font_size = max_font_size,
random_state = 42,
width=800,
height=400,
mask = mask)
wordcloud.generate(str(text))
plt.figure(figsize=figure_size)
if image_color:
image_colors = ImageColorGenerator(mask);
plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation="bilinear");
plt.title(title, fontdict={'size': title_size,
'verticalalignment': 'bottom'})
else:
plt.imshow(wordcloud);
plt.title(title, fontdict={'size': title_size, 'color': 'black',
'verticalalignment': 'bottom'})
plt.axis('off');
plt.tight_layout()
plot_wordcloud(train_df["Col_name"], title="Word Cloud of ...")