gpt4 book ai didi

python - 如何使用python增加Word-cloud中的max_words?

转载 作者:行者123 更新时间:2023-11-28 18:34:29 25 4
gpt4 key购买 nike

我正在使用使用 word cloud generator 的词云实现.

这是我写的代码,和他们提供的例子类似。

from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt
import random

from wordcloud import WordCloud, STOPWORDS


def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)

d = path.dirname(__file__)


with open("sample.csv") as f:
lines = f.readlines()
text = "".join(lines)
f.close()

stopwords = STOPWORDS.copy()



wc = WordCloud(max_words=15156633, stopwords=stopwords, margin=10,random_state=1).generate(text)
# generate_from_frequencies()
default_colors = wc.to_array()
plt.title("Custom colors")
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3))
wc.to_file("wordcloud_figure.png")
plt.axis("off")
plt.figure()
plt.title("Default colors")
plt.imshow(default_colors)
plt.axis("off")
plt.show()

我得到的输出如下:

Word cloud generated

无论 WordCloud() 函数的“max_words”(无论我是增加还是减少),我发现对最终输入几乎没有影响。

有什么问题吗?

此外,如何使用 API reference 中提到的 generate_from_frequencies() 方法.

最佳答案

您看不到更多字词的原因是它们太小了。您使用的是哪个版本的 wordcloud 模块?在 git 版本中,默认情况下 (relative_scaling=0),每个单词只有在不再适合时才应该减小字体大小。

关于python - 如何使用python增加Word-cloud中的max_words?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33774832/

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