简体   繁体   English

Python中Numpy Array的Wordcloud

[英]Wordcloud from Numpy Array in Python

I'm having trouble generating a wordcloud using numpy array where Column 1 = terms and Column 2 = frequency. 我在使用numpy数组生成wordcloud时遇到麻烦,其中Column 1 =术语,Column 2 =频率。

Given the documentation on wordcloud available here: Wordcloud Documentation to use .generate_from_frequencies you need a dictionary. 给定有关wordcloud的文档,可在这里找到:要使用.generate_from_frequencies的Wordcloud文档 ,您需要一个字典。

I've attempted to do this in the code below, however it results in: 我试图在下面的代码中执行此操作,但是会导致:

TypeError: unsupported operand type(s) for /: 'numpy.string_' and 'float' TypeError:/:“numpy.string_”和“ float”不受支持的操作数类型

Does anyone know how I can overcome this? 有谁知道我能克服这个问题吗? I have been stuck on this for hours now and pulling my hair out haha. 我已经坚持了好几个小时,然后把头发拉出来哈哈。

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()

I fixed it, i'm so happy, simply had to change the code below as second part was making it into a string rather than a float: 我修复了它,我很高兴,只需要更改下面的代码即可,因为第二部分将其变成字符串而不是浮点数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM