在远处从SciPy切割树状图/聚类树 [英] Cutting Dendrogram/Clustering Tree from SciPy at distance height

查看:173
本文介绍了在远处从SciPy切割树状图/聚类树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SciPy来学习如何在Python中使用dendrograms.我想获得集群并能够对其进行可视化;我听说hierarchical clusteringdendrograms是最好的方法.

I'm trying to learn how to use dendrograms in Python using SciPy . I want to get clusters and be able to visualize them; I heard hierarchical clustering and dendrograms are the best way.

如何在特定距离处砍"树?

在此示例中,我只想将其剪切到距离1.6

In this example, I just want to cut it at distance 1.6

我在 https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/#Inconsistency-Method ,但是这个家伙确实使用了一些令人困惑的包装器功能**kwargs(他称其阈值为max_d)

I looked up a tutorial on https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/#Inconsistency-Method but the guy did some really confusing wrapper function using **kwargs (he calls his threshold max_d)

这是我的代码和下面的图;为了重现性,我尝试过尽其最大的注解:

Here is my code and plot below; I tried annotating it as best as I could for reproducibility:

from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.cluster.hierarchy import dendrogram,linkage,fcluster
from scipy.spatial import distance
np.random.seed(424173239) #43984

#Dims
n,m = 20,7

#DataFrame: rows = Samples, cols = Attributes
attributes = ["a" + str(j) for j in range(m)]
DF_data = pd.DataFrame(np.random.random((n, m)), columns = attributes)

A_dist = distance.cdist(DF_data.as_matrix().T, DF_data.as_matrix().T)

#(i) . Do the labels stay in place from DF_data for me to do this? 
DF_dist = pd.DataFrame(A_dist, index = attributes, columns = attributes)

#Create dendrogram
fig, ax = plt.subplots()
Z = linkage(distance.squareform(DF_dist.as_matrix()), method="average")
D_dendro = dendrogram(Z, labels = attributes, ax=ax) #create dendrogram dictionary
threshold = 1.6 #for hline
ax.axhline(y=threshold, c='k')
plt.show()

#(ii) How can I "cut" the tree by giving it a distance threshold?
#i.e. If I cut at 1.6 it would make (a5 : cluster_1 or not in a cluster), (a2,a3 : cluster_2), (a0,a1 : cluster_3), and (a4,a6 : cluster_4)

#link_1 says use fcluster
#This -> fcluster(Z, t=1.5, criterion='inconsistent', depth=2, R=None, monocrit=None)
#gives me -> array([1, 1, 1, 1, 1, 1, 1], dtype=int32)

print(
     len(set(D_dendro["color_list"])), "^ # of colors from dendrogram",
     len(D_dendro["ivl"]), "^ # of labels",sep="\n")
#3 
#^ # of colors from dendrogram it should be 4 since clearly (a6, a4) and a5 are in different clusers
#7
#^ # of labels

link_1:如何从Python中的scipy中的链接/距离矩阵计算聚类分配?

推荐答案

对于更大的调色板,它应该可以工作:

For a bigger color palette this should work:

from scipy.cluster import hierarchy as hc
import matplotlib.cm as cm
import matplotlib.colors as col

#get a color spectrum "gist_ncar" from matplotlib cm. 
#When you have a spectrum it begins with 0 and ends with 1. 
#make tinier steps if you need more than 10 colors

colors = cm.gist_ncar(np.arange(0, 1, 0.1)) 

colorlst=[]# empty list where you will put your colors
for i in range(len(colors)): #get for your color hex instead of rgb
    colorlst.append(col.to_hex(colors[i]))

hc.set_link_color_palette(colorlst) #sets the color to use.

将所有代码放在前面,它应该可以工作

Put all of that infront of your code and it should work

这篇关于在远处从SciPy切割树状图/聚类树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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