ward_tree#

sklearn.cluster.ward_tree(X, *, connectivity=None, n_clusters=None, return_distance=False)[源代码]#

基于特征矩阵的病房集群。

以最小限度地增加群内方差的方式合并这对集群。

The inertia matrix uses a Heapq-based representation.

这是结构化版本,它考虑了样本之间的一些布局结构。

阅读更多的 User Guide .

参数:
X形状类似阵列(n_samples,n_features)

特征矩阵代表 n_samples 待聚集的样本。

connectivity{类数组,稀疏矩阵},默认=无

连接矩阵。按照给定的数据结构为每个样本定义邻近样本。假设矩阵是对称的,并且仅使用上三角形的一半。默认值为无,即Ward算法是非结构化的。

n_clustersint,默认=无

n_clusters should be less than n_samples. Stop early the construction of the tree at n_clusters. This is useful to decrease computation time if the number of clusters is not small compared to the number of samples. In this case, the complete tree is not computed, thus the 'children' output is of limited use, and the 'parents' output should rather be used. This option is valid only when specifying a connectivity matrix.

return_distance布尔,默认=假

如果 True ,返回集群之间的距离。

返回:
children形状的nd数组(n_nodes-1,2)

The children of each non-leaf node. Values less than n_samples correspond to leaves of the tree which are the original samples. A node i greater than or equal to n_samples is a non-leaf node and has children children_[i - n_samples]. Alternatively at the i-th iteration, children[i][0] and children[i][1] are merged to form node n_samples + i.

n_connected_componentsint

图表中连接组件的数量。

n_leavesint

树中叶子的数量。

parents形状的nd数组(n_nodes,)或无

每个节点的父节点。仅在指定连接矩阵时返回,其他地方返回“None”。

distances形状的nd数组(n_nodes-1,)

只有在以下情况下才返回 return_distance 设置为 True (for兼容性)。节点中心之间的距离。 distances[i] 对应于节点之间的加权欧几里得距离 children[i, 1]children[i, 2] .如果节点指的是树叶,那么 distances[i] 是它们的未加权欧几里得距离。距离以以下方式更新(来自scipy. hierge. link):

新条目 \(d(u,v)\) 计算如下,

\[d(u,v) = \sqrt{\frac{|v|+|s|} {T}d(v,s)^2 + \frac{|v|+|t|} {T}d(v,t)^2 - \frac{|v|} {T}d(s,t)^2}\]

哪里 \(u\) 新加入的集群是由集群组成的 \(s\)\(t\) , \(v\) 是森林中未使用的集群, \(T=|v|+|s|+|t|\) ,而且 \(|*|\) 是其论点的核心性。这也称为增量算法。

示例

>>> import numpy as np
>>> from sklearn.cluster import ward_tree
>>> X = np.array([[1, 2], [1, 4], [1, 0],
...               [4, 2], [4, 4], [4, 0]])
>>> children, n_connected_components, n_leaves, parents = ward_tree(X)
>>> children
array([[0, 1],
       [3, 5],
       [2, 6],
       [4, 7],
       [8, 9]])
>>> n_connected_components
1
>>> n_leaves
6