gpt4 book ai didi

python - 带有 generate_from_frequencies 的 Wordcloud Python

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

我正在尝试从 csv 文件创建文字云。例如,csv 文件具有以下结构:

a,1
b,2
c,4
j,20

它有更多行,大约 1800。第一列有字符串值(名称),第二列有它们各自的频率(整数)。然后,读取文件并将键值行存储在字典 (d) 中,因为稍后我们将使用它来绘制词云:

reader = csv.reader(open('namesDFtoCSV', 'r',newline='\n'))
d = {}
for k,v in reader:
d[k] = v

一旦我们有了充满值的字典,我就尝试绘制词云:

#Generating wordcloud. Relative scaling value is to adjust the importance of a frequency word.
#See documentation: https://github.com/amueller/word_cloud/blob/master/wordcloud/wordcloud.py
wordcloud = WordCloud(width=900,height=500, max_words=1628,relative_scaling=1,normalize_plurals=False).generate_from_frequencies(d)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
But an error is thrown:

Traceback (most recent call last):
File ".........../script.py", line 19, in <module>
wordcloud = WordCloud(width=900,height=500, max_words=1628,relative_scaling=1,normalize_plurals=False).generate_from_frequencies(d)
File "/usr/local/lib/python3.5/dist-packages/wordcloud/wordcloud.py", line 360, in generate_from_frequencies
for word, freq in frequencies]
File "/usr/local/lib/python3.5/dist-packages/wordcloud/wordcloud.py", line 360, in <listcomp>
for word, freq in frequencies]
TypeError: unsupported operand type(s) for /: 'str' and 'float

最后,文档说:

def generate_from_frequencies(self, frequencies, max_font_size=None):
"""Create a word_cloud from words and frequencies.
Parameters
----------
frequencies : dict from string to float
A contains words and associated frequency.
max_font_size : int
Use this font-size instead of self.max_font_size
Returns
-------
self
```python

So, I don't understand why is trowing me this error if I met the requirements of the function. I hope someone can help me, thanks.

**Note**

I work with worldcloud 1.3.1

最佳答案

这是因为字典中的值是字符串,但 wordcloud 需要整数或 float 。

在我运行你的代码之后检查你的字典 d 我得到以下内容。

In [12]: d

Out[12]: {'a': '1', 'b': '2', 'c': '4', 'j': '20'}

请注意数字周围的 ' ' 表示这些实际上是字符串。

解决此问题的一种 hacky 方法是在 FOR 循环中将 v 转换为 int,例如:

d[k] = int(v)

我说这是 hacky,因为它适用于整数,但如果你的输入中有 float ,那么它可能会导致问题。

此外,Python 错误可能难以阅读。您上面的错误可以解释为

script.py", line 19

TypeError: unsupported operand type(s) for /: 'str' and 'float

"There's a type error on or before line 19 of my file. Let me look at my data types to see if there is any mismatch between string and float..."

下面的代码对我有用:

import csv
from wordcloud import WordCloud
import matplotlib.pyplot as plt

reader = csv.reader(open('namesDFtoCSV', 'r',newline='\n'))
d = {}
for k,v in reader:
d[k] = int(v)

#Generating wordcloud. Relative scaling value is to adjust the importance of a frequency word.
#See documentation: https://github.com/amueller/word_cloud/blob/master/wordcloud/wordcloud.py
wordcloud = WordCloud(width=900,height=500, max_words=1628,relative_scaling=1,normalize_plurals=False).generate_from_frequencies(d)

plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

关于python - 带有 generate_from_frequencies 的 Wordcloud Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43043437/

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