-1

this code is for indexing and sorting on each cluster to identify which are the top n (I chose n=6) words that are nearest to the cluster centroid. Anyway, i found this kind of error: 'float' object has no attribute 'encode'

can anyone help me? the code is the following:

from __future__ import print_function

print("Top terms per cluster:")
print()
#sort cluster centers by proximity to centroid
order_centroids = km.cluster_centers_.argsort()[:, ::-1] 

for i in range(num_clusters):
    print("Cluster %d words:" % i, end='')

    for ind in order_centroids[i, :6]: #replace 6 with n words per cluster
        print(' %s' % vocab_frame.ix[terms[ind].split(' ')].values.tolist()[0][0].encode('utf-8', 'ignore'), end=',')
    print() #add whitespace
    print() #add whitespace

    print("Cluster %d titles:" % i, end='')
    for title in frame.ix[i]['title'].values.tolist():
        print(' %s,' % title, end='')
    print() #add whitespace
    print() #add whitespace

print()
print()

thank you in advance

Edoardo
  • 11
  • 1

1 Answers1

-1

The method encode can only be applied to strings, not to floats.

You could convert the float to a string, but obviously that does not make a lot of sense.

It appears you just have some copy&pasted code from the internet that you do not understand. Maybe you should first focus on understanding the code, then run it. Once you understand the code, it should be obvious where the error comes from, and how to correctly fix it.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194