我在使用numpy数组生成wordcloud时遇到了困难,其中列1= terms,列2=频度。
考虑到可以在这里获得的wordcloud文档:使用Wordcloud文档来使用.generate_from_frequencies,您需要一个字典。
我尝试在下面的代码中这样做,但是结果是:
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()发布于 2017-08-20 12:59:04
我修正了它,我很高兴,只需要修改下面的代码,因为第二部分是将它变成字符串而不是浮点:
d = {}
for a, q in Cluster_1_FW:
d[a] = float(q)https://stackoverflow.com/questions/45782188
复制相似问题