【发布时间】:2017-08-20 12:34:47
【问题描述】:
我在使用 numpy 数组生成 wordcloud 时遇到问题,其中第 1 列 = 术语,第 2 列 = 频率。
鉴于此处提供的 wordcloud 文档:Wordcloud Documentation 要使用 .generate_from_frequencies 你需要一本字典。
我尝试在下面的代码中执行此操作,但结果是:
TypeError: 不支持的操作数类型 /: 'numpy.string_' 和 '浮动'
有谁知道我该如何克服这个问题?我已经坚持了好几个小时了,把头发拉出来哈哈。
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()
【问题讨论】:
标签: python arrays numpy data-science word-cloud