如何遍历 sklearn AgglomerativeClustering 生成的树?

我有一个 numpy 文本文件数组,位于:https://github.com/alvations/anythingyouwant/blob/master/WN_food.matrix

这是一个术语之间的距离矩阵,我的术语列表如下:http://pastebin.com/2xGt7Xjh

我使用以下代码生成层次聚类:

import numpy as npfrom sklearn.cluster import AgglomerativeClusteringmatrix = np.loadtxt('WN_food.matrix')n_clusters = 518model = AgglomerativeClustering(n_clusters=n_clusters,                                linkage="average", affinity="cosine")model.fit(matrix)

为了获取每个术语的聚类,我可以这样做:

for term, clusterid in enumerate(model.labels_):    print term, clusterid

但是,我如何遍历 AgglomerativeClustering 输出的树呢?

是否可以将其转换为 scipy 的树状图(http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.cluster.hierarchy.dendrogram.html)?然后如何遍历这个树状图?


回答:

我已经回答了一个关于 sklearn.cluster.ward_tree 的类似问题:如何可视化 sklearn.cluster.ward_tree 生成的 ward 树?

AgglomerativeClustering 以相同的方式输出树,保存在 children_ 属性中。以下是针对 AgglomerativeClustering 调整的 ward 树问题的代码。它以 (node_id, left_child, right_child) 的形式输出树的结构,每个节点一个。

import numpy as npfrom sklearn.cluster import AgglomerativeClusteringimport itertoolsX = np.concatenate([np.random.randn(3, 10), np.random.randn(2, 10) + 100])model = AgglomerativeClustering(linkage="average", affinity="cosine")model.fit(X)ii = itertools.count(X.shape[0])[{'node_id': next(ii), 'left': x[0], 'right':x[1]} for x in model.children_]

https://stackoverflow.com/a/26152118

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注