gpt4 book ai didi

Python WordCloud 遇到 AttributeError : 'list' object has no attribute 'items'

转载 作者:行者123 更新时间:2023-11-28 22:24:55 28 4
gpt4 key购买 nike

当我使用 WordCloud 函数 .fit_words() 时,我遇到了 AttributeError: 'list' object has no attribute 'items'。当我使用 .generate_from_frequencies() 时它也不起作用。如何弄清楚?所有建议将不胜感激。代码:

wordcloud = WordCloud(font_path="simhei.ttf", background_color="white", max_font_size=80)
word_frequence = {x[0]: x[1] for x in words_stat.head(1000).values}
word_frequence_list = []
for key in word_frequence:
temp = (key, word_frequence[key])
word_frequence_list.append(temp)
wordcloud.fit_words(word_frequence_list)
plt.imshow(wordcloud)

word_frequence_list 是

[('诺兰', 89), ('电影', 48), ('战争', 43), ('一个', 43), ('配乐', 40), ('故事', 38), ('人', 36), ('拍', 31), ('时间', 30), ('叙事', 28).....

完整代码

修改

当我使用 WordCloud.fit_word() 时,它显示 AttributeError: 'list' object has no attribute 'items'。当我使用 WordCloud.generate_from_frequencies() 时它也不起作用如何解决?谢谢 :)代码:frequency = [(u'知乎',5),(u'小段同学',4),(u'曲小花',3),(u'中文分词',2),(u'样例', 1)]wordcloud = WordCloud().fit_words(频率)错误

追溯(最近的调用最后):文件“D:/VS_Project/Python/WordCloudSample/WCSample.py”,第 19 行,位于wordcloud = WordCloud().fit_words(频率)文件“C:\Users\AlphaGoMK\AppData\Local\Programs\Python\Python36-32\lib\site-packages\wordcloud\wordcloud.py”,第 331 行,在 fit_words 中返回 self.generate_from_frequencies(频率)文件“C:\Users\AlphaGoMK\AppData\Local\Programs\Python\Python36-32\lib\site-packages\wordcloud\wordcloud.py”,第 350 行,在 generate_from_frequencies频率=排序(频率。项目(),关键=项目1,反向=真)AttributeError: 'list' 对象没有属性 'items'

https://github.com/amueller/word_cloud/issues/291

issues on github

最佳答案

fit_words want's your dictionary ,而不是键/值对列表。您可以直接传递 word_frequence

wordcloud.fit_words(word_frequence)

关于Python WordCloud 遇到 AttributeError : 'list' object has no attribute 'items' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45994551/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com