您如何从sklearn.cluster.ward_tree可视化病房树? [英] How do you visualize a ward tree from sklearn.cluster.ward_tree?

查看:152
本文介绍了您如何从sklearn.cluster.ward_tree可视化病房树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sklearn中,实现了一种聚集聚类算法,其中,ward方法可最大程度地减少方差.通常sklearn有很多不错的用法示例,但我找不到如何使用此功能的示例.

In sklearn there is one agglomerative clustering algorithm implemented, the ward method minimizing variance. Usually sklearn is documented with lots of nice usage examples, but I couldn't find examples of how to use this function.

基本上,我的问题是根据数据的聚类绘制树状图,但是我不理解该函数的输出. 文档表示它返回子级,组件数,数量叶子和每个节点的父母.

Basically my problem is to draw a dendrogram according to the clustering of my data, but I don't understand the output from the function. The documentation says that it returns the children, the number of components, the number of leaves and the parents of each node.

对于我的数据样本,结果没有任何意义.对于已与连接性矩阵聚类的(32,542)个矩阵,其输出为:

Yet for my data samples, the results don't give any meaning. For a (32,542) matrix that has been clustered with a connectivity matrix this is the output:

>>> wt = ward_tree(mymat, connectivity=connectivity, n_clusters=2)

>>> mymat.shape
(32, 542)
>>> wt
(array([[16,  0],
       [17,  1],
       [18,  2],
       [19,  3],
       [20,  4],
       [21,  5],
       [22,  6],
       [23,  7],
       [24,  8],
       [25,  9],
       [26, 10],
       [27, 11],
       [28, 12],
       [29, 13],
       [30, 14],
       [31, 15],
       [34, 33],
       [47, 46],
       [41, 40],
       [36, 35],
       [45, 44],
       [48, 32],
       [50, 42],
       [38, 37],
       [52, 43],
       [54, 39],
       [53, 51],
       [58, 55],
       [56, 49],
       [60, 57]]), 1, 32, array([32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,     45, 46, 47, 32,
       33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 53, 48,
   48, 51, 51, 55, 55, 57, 50, 50, 54, 56, 52, 52, 49, 49, 53, 60, 54,
   58, 56, 58, 57, 59, 60, 61, 59, 59, 61, 61]))

在这种情况下,我要求两个聚类,包含32个包含特征的向量.但是,这两个群集在数据中如何显示?他们在哪里?孩子们在这里真的是什么意思,孩子们怎么可能比样本总数高?

In this case I asked for two clusters, with 32 vectors containing features. But how are the two clusters visible in the data? Where are they? And what do the children really mean here, how can the children be higher numbers than the total number of samples?

推荐答案

文档说,关于输出的第一个参数

About the first argument of output, the documentation says

每个非叶节点的子级.小于n_samples的值是指 到树上的叶子. i越大,表示节点 儿童儿童[i-n_samples].

The children of each non-leaf node. Values less than n_samples refer to leaves of the tree. A greater value i indicates a node with children children[i - n_samples].

我在弄清楚这意味着什么时遇到了一些麻烦,但是随后这段代码有所帮助.我们生成具有两个簇"的正态分布数据,一个包含3个数据点,均值为0,一个包含2个数据点,均值为100.因此,我们期望第3个数据点最终会出现在输出树的一个分支中,并且另一个2个.

I had some trouble figuring what this means, but then this code helped. We generate normally distributed data with two "clusters", one with 3 data points with mean 0, and one with 2 data points with mean 100. So we expect that the 3 first data point will end up in one branch of the output tree and the the other 2 in another.

from sklearn.cluster import ward_tree
import numpy as np
import itertools
X = np.concatenate([np.random.randn(3, 10), np.random.randn(2, 10) + 100])
w = ward_tree(X)
ii = itertools.count(w[2])
[{'node_id': next(ii), 'left': x[0], 'right':x[1]} for x in w[0]]

哪个会产生树:

[{'node_id': 5, 'right': 2, 'left': 1},
 {'node_id': 6, 'right': 4, 'left': 3}, 
 {'node_id': 7, 'right': 5, 'left': 0}, 
 {'node_id': 8, 'right': 7, 'left': 6}]

,其中数字是节点ID.如果node_id< 5(样本数),则它是数据点(或叶节点)的索引.如果node_id> = 5,则它是一个内部节点.我们看到数据集群按预期进行:

where the numbers are node id's. If node_id < 5 (the number of samples) then it's an index to a data point (or leaf node). If node_id >= 5 then it's an internal node. We see that the data clusters as expected:

         8
     /       \
    7         \  
   / \         \
  5   \         6
 / \   \       / \
1   2   0     3   4

这篇关于您如何从sklearn.cluster.ward_tree可视化病房树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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