Wordcloud from Numpy Array in Python -
i'm having trouble generating wordcloud using numpy array column 1 = terms , column 2 = frequency.
given documentation on wordcloud available here: wordcloud documentation use .generate_from_frequencies need dictionary.
i've attempted in code below, results in:
typeerror: unsupported operand type(s) /: 'numpy.string_' , 'float'
does know how can overcome this? have been stuck on hours , pulling hair out haha.
from wordcloud import wordcloud, stopwords # create array documents classifed "0" cluster best performing kmeans cluster_1 = np.empty((0,4613)) cluster_1_fw = terms n in range (0,737): if euclidean_best[n] == 0: cluster_1 = np.vstack([cluster_1,x[n,:]]) # sum frequencies of 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 = {} 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 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 happy, had change code below second part making string rather float:
d = {} a, q in cluster_1_fw: d[a] = float(q)
Comments
Post a Comment