Python 如何在scipy分层聚类中获取非单例集群ID
根据这一点,我们可以得到非单态集群的标签 我试着用一个简单的例子Python 如何在scipy分层聚类中获取非单例集群ID,python,scipy,cluster-analysis,hierarchical-clustering,Python,Scipy,Cluster Analysis,Hierarchical Clustering,根据这一点,我们可以得到非单态集群的标签 我试着用一个简单的例子 import numpy as np import scipy.cluster.hierarchy import matplotlib.pyplot as plt from scipy.cluster.hierarchy import dendrogram, linkage mat = np.array([[ 0. , 1. , 3. ,0. ,2. ,3. ,1.], [ 1. , 0. , 3. , 1., 1.
import numpy as np
import scipy.cluster.hierarchy
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
mat = np.array([[ 0. , 1. , 3. ,0. ,2. ,3. ,1.],
[ 1. , 0. , 3. , 1., 1. , 2. , 2.],
[ 3., 3. , 0., 3. , 3., 3. , 4.],
[ 0. , 1. , 3., 0. , 2. , 3., 1.],
[ 2. , 1., 3. , 2., 0. , 1., 3.],
[ 3. , 2., 3. , 3. , 1. , 0. , 3.],
[ 1. , 2., 4. , 1. , 3., 3. , 0.]])
def llf(id):
if id < n:
return str(id)
else:
return '[%d %d %1.2f]' % (id, count, R[n-id,3])
linkage_matrix = linkage(mat, "complete")
dendrogram(linkage_matrix,
p=4,
leaf_label_func=llf,
color_threshold=1,
truncate_mode='lastp',
distance_sort='ascending')
plt.show()
将numpy导入为np
导入scipy.cluster.hierarchy
将matplotlib.pyplot作为plt导入
从scipy.cluster.hierarchy导入树状图,链接
mat=np.数组([[0,1,3,0,2,3,1.]),
[ 1. , 0. , 3. , 1., 1. , 2. , 2.],
[ 3., 3. , 0., 3. , 3., 3. , 4.],
[ 0. , 1. , 3., 0. , 2. , 3., 1.],
[ 2. , 1., 3. , 2., 0. , 1., 3.],
[ 3. , 2., 3. , 3. , 1. , 0. , 3.],
[ 1. , 2., 4. , 1. , 3., 3. , 0.]])
def llf(id):
如果id
什么是n,这里算什么?在下面这样的图表中,我需要知道(3)和(2)下面列出了谁?我认为文档在这一部分不是很清楚,其中的示例代码甚至不可操作。但很明显,1表示第二次观察,(3)表示该节点中有3次观察 如果你想知道3个OB是什么。在第二个节点中,如果这是您的问题:
In [51]:
D4=dendrogram(linkage_matrix,
color_threshold=1,
p=4,
truncate_mode='lastp',
distance_sort='ascending')
D7=dendrogram(linkage_matrix,
color_list=['g',]*7,
p=7,
truncate_mode='lastp',
distance_sort='ascending', no_plot=True)
from itertools import groupby
[list(group) for key, group in groupby(D7['ivl'],lambda x: x in D4['ivl'])]
Out[51]:
[['1'], ['6', '0', '3'], ['2'], ['4', '5']]
第二个节点包含obs。第7、第1和第4个节点,第2个节点包含第5和第6个观测值