Text clustering using Scipy Hierarchy Clustering in Python

て烟熏妆下的殇ゞ 提交于 2019-12-18 18:27:11

问题


I have a text corpus that contains 1000+ articles each in a separate line. I am trying to use Hierarchy Clustering using Scipy in python to produce clusters of related articles. This is the code I used to do the clustering

# Agglomerative Clustering
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as hac
tree = hac.linkage(X.toarray(), method="complete",metric="euclidean")
plt.clf()
hac.dendrogram(tree)
plt.show() 

and I got this plot

Then I cut off the tree at the third level with fcluster()

from scipy.cluster.hierarchy import fcluster
clustering = fcluster(tree,3,'maxclust')
print(clustering)

and I got this output: [2 2 2 ..., 2 2 2]

My question is how can I find the top 10 frequent words in each cluster in order to suggest a topic for each cluster?


回答1:


You can do the following:

  1. Align your results (your clustering variable) with your input (the 1000+ articles).
  2. Using pandas library, you can use a groupby function with the cluster # as its key.
  3. Per group (using the get_group function), fill up a defaultdict of integers for every word you encounter.
  4. You can now sort the dictionary of word counts in descending order and get your desired number of most frequent words.

Good luck with what you're doing and please do accept my answer if it's what you're looking for.



来源:https://stackoverflow.com/questions/43707062/text-clustering-using-scipy-hierarchy-clustering-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!