使用sklearn绘制树状图。 [英] Plot dendrogram using sklearn.AgglomerativeClustering
问题描述
我正在尝试使用 AgglomerativeClustering
提供的 children _
属性构建树状图,但到目前为止,我已经我很不幸。我不能使用 scipy.cluster
,因为 scipy
中提供的聚集群集缺少一些对我来说很重要的选项(例如作为指定群集数量的选项)。如果有任何建议,我将非常感谢。
I'm trying to build a dendrogram using the children_
attribute provided by AgglomerativeClustering
, but so far I'm out of luck. I can't use scipy.cluster
since agglomerative clustering provided in scipy
lacks some options that are important to me (such as the option to specify the amount of clusters). I would be really grateful for a any advice out there.
import sklearn.cluster
clstr = cluster.AgglomerativeClustering(n_clusters=2)
clusterer.children_
推荐答案
这是简单的函数,用于从分层结构中提取 树形图
函数。似乎在sklearn中通常不直接支持图形功能。您可以找到与此 plot_dendrogram
代码段的拉取请求相关的有趣讨论此处。
Here is a simple function for taking a hierarchical clustering model from sklearn and plotting it using the scipy dendrogram
function. Seems like graphing functions are often not directly supported in sklearn. You can find an interesting discussion of that related to the pull request for this plot_dendrogram
code snippet here.
我要澄清一下,您描述的用例(定义集群数)是可在scipy中使用:使用scipy的 linkage
执行层次结构聚类后,您可以使用 fcluster 将层次结构切成任意数量的集群code>,并在
t
参数和 criterion ='maxclust'
参数中指定了簇的数量。
I'd clarify that the use case you describe (defining number of clusters) is available in scipy: after you've performed the hierarchical clustering using scipy's linkage
you can cut the hierarchy to whatever number of clusters you want using fcluster
with number of clusters specified in the t
argument and criterion='maxclust'
argument.
这篇关于使用sklearn绘制树状图。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!