在Python中使用Scipy层次结构聚类的文本聚类 [英] Text clustering using Scipy Hierarchy Clustering in Python

查看:347
本文介绍了在Python中使用Scipy层次结构聚类的文本聚类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本语料库,每行包含1000多篇文章。我正在尝试在python中使用通过Scipy使用的层次结构聚类来生成相关文章的聚类。
这是我用来进行聚类的代码

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

然后我用scipy.cluster.hierarchy import fcluster
群集的fcluster()

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)

和我得到了这样的输出:
[2 2 2 ...,2 2 2]

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

我的问题是如何找到每个群集中的前10个常用词为了为每个群集建议一个主题?

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. 调整结果(您的聚类变量)与您的输入(1000篇以上的文章)。

  2. 使用熊猫库,您可以使用 groupby函数,其中群集号为键。

  3. 每个组(使用 get_group函数) ,为每个遇到的
    单词填充一个 defaultdict 整数。

  4. 您现在可以对字数字典进行排序

  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.

这篇关于在Python中使用Scipy层次结构聚类的文本聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆