gpt4 book ai didi

python - matplotlib 中的并排词云

转载 作者:太空狗 更新时间:2023-10-30 02:58:22 25 4
gpt4 key购买 nike

我正在使用包 WordCloud显示由 scikit LDA 生成的单词(潜在狄利克雷分配)。对于 LDA 生成的每个主题,我都会有一个图表。我希望能够在网格中绘制所有图表,以便并排可视化。本质上,我有一个将 LDA 模型作为输入的函数,以及我想要可视化的 LDA 主题,然后绘制一个词云:

from wordcloud import WordCloud
import matplotlib.pyplot as plt
SEED=0

def topicWordCloud(model, topicNumber, WCmaxWords,WCwidth, WCheight):
topic = model.components_[topicNumber]
tupleList = [(tf_feature_names[i],int(topic[i]/topic.sum()*10000)) for i in range(len(topic))]
wordcloud = WordCloud(width=WCwidth, height=WCheight, max_words=WCmaxWords, random_state=42).generate_from_frequencies(tupleList)
plt.figure( figsize=(20,10) )
plt.imshow(wordcloud)
plt.axis("off")

topicWordCloud(model=lda, topicNumber=2, WCmaxWords=100,WCwidth=800, WCheight=600)

如何遍历所有主题 (n_topics) 以可视化网格中的所有图表?我在想一些事情:

fig = plt.figure()
for i in range(n_topics):
plt.subplot(2,1,i+1)
#something here

最佳答案

从您的函数返回 wordcloud,然后从您的 for 循环中调用 topicWordCloud。然后,在使用 fig.add_subplot 创建的 Axes 上使用 imshow。例如,像这样:

def topicWordCloud(model, topicNumber, WCmaxWords,WCwidth, WCheight):
topic = model.components_[topicNumber]
tupleList = [(tf_feature_names[i],int(topic[i]/topic.sum()*10000)) for i in range(len(topic))]
wordcloud = WordCloud(width=WCwidth, height=WCheight, max_words=WCmaxWords, random_state=42).generate_from_frequencies(tupleList)
return wordcloud

fig = plt.figure()
for i in range(n_topics):
ax = fig.add_subplot(2,1,i+1)
wordcloud = topicWordCloud(...)

ax.imshow(wordcloud)
ax.axis('off')

关于python - matplotlib 中的并排词云,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34165130/

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