在距离高度从 SciPy 切割树状图/聚类树

     2023-02-19     111

关键词:

【中文标题】在距离高度从 SciPy 切割树状图/聚类树【英文标题】:Cutting Dendrogram/Clustering Tree from SciPy at distance height 【发布时间】:2016-07-31 03:28:52 【问题描述】:

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

如何在特定距离“砍”树?

在这个例子中,我只想在距离1.6 处切割它

我在https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/#Inconsistency-Method 上查找了一个教程,但是这家伙使用**kwargs 做了一些非常令人困惑的包装函数(他称他的阈值max_d

这是我的代码和下面的情节;为了重现性,我尝试尽可能地对其进行注释:

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:How to compute cluster assignments from linkage/distance matrices in scipy in Python?

【问题讨论】:

Related question... @SaulloCastro 对此表示感谢。是的,肯定有关系。一种有趣的方式,只通过水平移动树木。看看实际的图表是如何绘制的也很酷。 【参考方案1】:

color_threshold 是我正在寻找的方法。当color_palette 对于生成的集群数量来说太小时,它并没有真正的帮助。如果有人可以提供帮助,请将下一步迁移到 Bigger color-palette in matplotlib for SciPy's dendrogram (Python)。

【讨论】:

【参考方案2】:

对于更大的调色板,这应该可以:

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.

把所有这些放在你的代码前面,它应该可以工作

【讨论】:

在 scipy 中修剪树状图(分层聚类)

...-05-0512:21:14【问题描述】:我有一个包含大约5000个条目的距离矩阵,并使用scipy的层次聚类方法对矩阵进行聚类。我用于此的代码如下sn-p:Y=fastcluster.linkage(D,method=\'centroid\')#D-distancem 查看详情

从 R 中的切割树状图中提取标签成员资格/分类(即:树状图的 cutree 函数)

...:我正在尝试从R中的树状图中提取一个分类,我在某个高度有cut。在hclustobject上使用cu 查看详情

r语言层次聚类(hierarchicalclustering):使用scale函数进行特征缩放hclust包层次聚类(创建距离矩阵聚类绘制树状图dendrogram,在树状图上绘制红色矩形框)

R语言层次聚类(hierarchicalclustering):使用scale函数进行特征缩放、hclust包层次聚类(创建距离矩阵、聚类、绘制树状图dendrogram,在树状图上绘制红色矩形框)目录 查看详情

如何在 scipy 创建的树状图中获得与颜色簇相对应的平面聚类

】如何在scipy创建的树状图中获得与颜色簇相对应的平面聚类【英文标题】:Howtogetflatclusteringcorrespondingtocolorclustersinthedendrogramcreatedbyscipy【发布时间】:2011-12-0115:57:00【问题描述】:使用here发布的代码,我创建了一个不错的层... 查看详情

在 Scipy 错误中为大型数据集绘制树状图

】在Scipy错误中为大型数据集绘制树状图【英文标题】:PlottingdendrograminScipyerrorforlargedataset【发布时间】:2012-04-2913:21:30【问题描述】:我正在使用Scipy进行层次聚类。我确实设法使用fcluster在阈值上获得平坦的集群。但我需要可... 查看详情

有词篇矩阵和共现矩阵怎么在spss生成聚类树图

参考技术A1、首先,在‘进阶方法’版块艳包胆中点击‘分层聚类’按钮。2、然后,将数据拖拽到右侧分析框中,可填写聚类个数昆敏,点击开始分析。3、最后,可以看到分层侧随聚类的聚类树状图。 查看详情

带有名称的 Scipy 树状图

...。我已将数据名称记录为names的代码编辑如下,并希望在距离矩阵可视化的底部和右侧打印出名称。我尝试在对dendrogram的调用中添加labels=names,但这没有帮助。有谁知道如何给这个添加标签? 查看详情

如何调整树状图的y轴大小

】如何调整树状图的y轴大小【英文标题】:Howtoresizeyaxisofadendogram【发布时间】:2013-10-0523:31:36【问题描述】:我在进行层次聚类后使用scipy.cluster.hierarchyassch绘制树状图。问题是聚类发生在树状图的顶部,介于0.8和1.0之间,这是... 查看详情

使用树状图可视化聚类(代码片段)

...们代表具有相似特征的观察组。分支的高度或节点之间的距离表示组之间的不同或相似程度。也就是说分支越长或节点之间的距离越大,组就越不相似。分支越短或节点之间的距离越小,组越相似。树状图对于可视化复... 查看详情

距离矩阵的树状图或其他图

】距离矩阵的树状图或其他图【英文标题】:DendrogramorOtherPlotfromDistanceMatrix【发布时间】:2017-05-1522:49:08【问题描述】:我要比较三个矩阵。他们每个人都是5x6。我最初想使用层次聚类来对矩阵进行聚类,以便对最相似的矩阵进... 查看详情

在 Scipy/Matplotlib 中注释树状图节点

】在Scipy/Matplotlib中注释树状图节点【英文标题】:AnnotatingDendrogramnodesinScipy/Matplotlib【发布时间】:2015-07-3019:30:17【问题描述】:我正在尝试在scipy.cluster.hierarchy.dendrogram生成的树状图中标记节点。我正在使用augmenteddendrogramsuggeste... 查看详情

从树中的聚类对象逐步绘制树状图的算法

...。每个叶子包含一个对象,每个节点包含其子节点之间的距离。现在,我想逐步绘制树状图,因此 查看详情

绘制聚类热图(带树状图)/Python

】绘制聚类热图(带树状图)/Python【英文标题】:Plotlyclusteredheatmap(withdendrogram)/Python【发布时间】:2021-06-0709:11:21【问题描述】:我正在尝试在Python中使用plotly创建一个聚集的热图(带有树状图)。他们在他们的网站上制作的... 查看详情

利用kmeans聚类分析两类问题

...以群分)正式一点的:聚类是对点集进行考察并按照某种距离测度将他们聚成多个“簇”的过程。聚类的目标是使得同一簇内的点之间的距离较短,而不同簇中点之间的距离较大。两种方法对比:在K-means聚类中,是预先规定出... 查看详情

如何在python中提取树状图中点之间的距离?

】如何在python中提取树状图中点之间的距离?【英文标题】:HowcanIextractthedistancesbetweenpointswithinadendograminpython?【发布时间】:2021-06-2706:25:59【问题描述】:我在python中执行层次聚类并获得了树状图。我想知道是否有一种方法可... 查看详情

层次聚类

...步骤:初始化:每个样本归为一类,并计算每个类之间的距离寻找距离最近的两个类,合并为 查看详情

spss聚类树状图怎么分析

...。如果还是不知道的话,请重新发图吧。追问我知道这个切割法不过想找人帮我分析下对不对还有就是那个虚线怎么变成实线呢追答图形设置里都有线形,我没设置过,是默认这样的。如果设置也不行,那就是显示原因了。另外... 查看详情

在python中层次聚类的每一步打印所有聚类和样本

...2:04:07【问题描述】:我使用Scipy库来执行层次聚类并创建树状图。这是简单的代码和生成的树状图:importnumpyasnpfromscipy.cluster.hierarchyimportd 查看详情