簡體   English   中英

Python中Numpy Array的Wordcloud

[英]Wordcloud from Numpy Array in Python

我在使用numpy數組生成wordcloud時遇到麻煩,其中Column 1 =術語,Column 2 =頻率。

給定有關wordcloud的文檔,可在這里找到:要使用.generate_from_frequencies的Wordcloud文檔 ,您需要一個字典。

我試圖在下面的代碼中執行此操作,但是會導致:

TypeError:/:“numpy.string_”和“ float”不受支持的操作數類型

有誰知道我能克服這個問題嗎? 我已經堅持了好幾個小時,然后把頭發拉出來哈哈。

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

我修復了它,我很高興,只需要更改下面的代碼即可,因為第二部分將其變成字符串而不是浮點數:

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