首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python中Numpy Array中的Wordcloud

Python中Numpy Array中的Wordcloud
EN

Stack Overflow用户
提问于 2017-08-20 12:34:47
回答 1查看 1K关注 0票数 0

我在使用numpy数组生成wordcloud时遇到了困难,其中列1= terms,列2=频度。

考虑到可以在这里获得的wordcloud文档:使用Wordcloud文档来使用.generate_from_frequencies,您需要一个字典。

我尝试在下面的代码中这样做,但是结果是:

TypeError:不支持/的操作数类型:“numpy.string_”和“float”

有人知道我怎么能克服这一切吗?我被困在这上面已经好几个小时了,把我的头发拔出来。

代码语言:javascript
复制
from wordcloud import WordCloud, STOPWORDS

# Create array with all documents classifed as "0" cluster from best performing Kmeans

Cluster_1 = np.empty((0,4613))
Cluster_1_FW = terms

for n in range (0,737): 
    if Euclidean_best[n] == 0:
        Cluster_1 = np.vstack([Cluster_1,X[n,:]])

# Sum frequencies of all words in cluster
Cluster_1_f = np.sum(Cluster_1,axis=0)

print(Cluster_1_f.shape)

Cluster_1_FW = np.vstack([Cluster_1_FW,Cluster_1_f])
Cluster_1_FW = np.transpose(Cluster_1_FW)

d = {}
for a, q in Cluster_1_FW:
    d[a] = q



print(Cluster_1_FW.dtype)

print(np.max(Cluster_1_f))
print(Cluster_1_FW.shape)
print(Cluster_1_FW[0:5,:])
# Create word cloud from word-frequency table stored in Cluster_1_FW
wcCluster1 = WordCloud(stopwords=STOPWORDS,background_color='white', width=1200,
                          height=1000).generate_from_frequencies(d)
fig = plt.figure()
plt.imshow(wcCluster1)
fig.show()
EN

回答 1

Stack Overflow用户

发布于 2017-08-20 12:59:04

我修正了它,我很高兴,只需要修改下面的代码,因为第二部分是将它变成字符串而不是浮点:

代码语言:javascript
复制
d = {}
for a, q in Cluster_1_FW:
    d[a] = float(q)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45782188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档