I am trying to print out the top 10 words from each cluster and I am struggling to save it to a file not sure how to save the content to the file without just built-in function. Here is my code. Could anyone please give suggestions. Thanks
Code:
l=['0','1','2','3','4']
for i in range(best_K):
print(l[i],"Cluster top words:", end='')
for ind in order_centroids[i, :10]:
word=vocab_frame.ix[terms[ind].split(' ')].values.tolist()[0][0]
print(' %s' % word, end=',')
print()
Tried 1:
In the text file i get <built-in function print>
l=['0','1','2','3','4']
order_centroids = model.cluster_centers_.argsort()[:, ::-1]
for i in range(best_K):
f = open("output.txt", "w")
print(l[i],"Cluster top words:", end='')
for ind in order_centroids[i, :10]:
word=vocab_frame.loc[terms[ind].split(' ')].values.tolist()[0][0]
print(' %s' % word, end=',')
my_top=str(print)
f.write(my_top)