【问题标题】:Wordcloud from Numpy Array in PythonPython 中 Numpy 数组中的 Wordcloud
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-11-09
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      相关资源
      最近更新 更多