首页 > 解决方案 > 在 WordCloud 中使用 Include_Numbers 功能时出现类型错误

问题描述

我正在尝试在 python 中使用 WordCloud 包,并且在尝试使用 include_numbers 参数时遇到错误。我已经复制了包的 github 链接,具体的参数定义(我已经尝试了正确的拼写并注意到了不正确的拼写),我得到了以下错误

https://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html

incldue_numbers:bool, default=False 是否包含数字作为短语。

TypeError: init () got an unexpected keyword argument 'include_numbers'

我正在尝试运行的部分:

import numpy as np # linear algebra
import pandas as pd 
import matplotlib as mpl
import matplotlib.pyplot as plt
##%matplotlib inline

from subprocess import check_output
from wordcloud import WordCloud, STOPWORDS

#mpl.rcParams['figure.figsize']=(8.0,6.0)    #(6.0,4.0)
mpl.rcParams['font.size']=12                #10 
mpl.rcParams['savefig.dpi']=100             #72 
mpl.rcParams['figure.subplot.bottom']=.1 


stopwords = set(STOPWORDS)
data = pd.read_csv("C:\\Users\\chris\\Documents\\testing\\wc_ad_copy_test.csv")

##test below
#data['dupe_copy'] = data['dupe_copy'].astype(str)
##end test



wordcloud = WordCloud(
                          background_color='white',
                          stopwords=stopwords,
                          max_words=200,
                          max_font_size=40, 
                          random_state=42,
                          include_numbers=True,
                          #collocations=True,
                          normalize_plurals=False
                         ).generate(str(data['scored_copy']))



print(wordcloud)
fig = plt.figure(1)
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
fig.savefig("ad_copy_cloud_image.png", dpi=900)


wc = WordCloud(
                          background_color='white',
                          stopwords=stopwords,
                          max_words=200,
                          max_font_size=40, 
                          random_state=42,
                          include_numbers=True,
                          #collocations=True,
                          normalize_plurals=False
                         )

word_dict = wc.process_text(str(data['scored_copy']))

df = pd.DataFrame.from_dict(word_dict, orient='index')
df = df.reset_index()
df.columns = ['word', 'word_count']
df = df.sort_values(by='word_count', ascending=False)
df.to_csv("word_count_list.csv", index=False)

include_numbers 在运行为“False”时会引发相同的错误

我希望这可以运行并将数字输出到 wordcloud

标签: pythonpandasword-cloud

解决方案


我查看了wordcloud源代码,问题似乎是github上的代码和pip安装的pypi包不一样。pip安装时得到的版本不包含include_numbers参数。

我在这里在 github 上提交了这个问题:https ://github.com/amueller/word_cloud/issues/482如果你想关注并看看开发人员怎么说。


推荐阅读