我是绘图新手,需要绘制具有组平均链接的树状图。
我知道 create_dendrogram()
中有一个 distfun
参数,但我不知道要传递给该参数以获取组平均链接。 distfun 参数显然必须是可调用的。我应该传递什么函数给它?
作为旁注,我有一个样本成对距离矩阵
0
13 0
2 14 0
17 1 18 0
当我传递给 create_dendrogram() 方法时,它似乎产生了错误的结果。我在这里做错了什么?
代码:
import plotly.figure_factory as ff
import numpy as np
X = np.matrix([[0,0,0,0],[13,0,0,0],[2,14,0,0],[17,1,18,0]])
names = list("0123")
fig = ff.create_dendrogram(X, orientation='left', labels=names)
fig.update_layout(width=800, height=800)
fig.show()
代码是从plotly网站上复制过来的,我不知道我应该做什么。 本网站:https://plotly.com/python/v3/dendrogram/
最佳答案
您可以使用 scipy.cluster.hierarchy.linkage()
选择链接方法
通过 create_dendrogram()
函数中的 linkagefun
参数。
例如,使用 UPGMA (Unweighted Pair Group Method with Arithmetic mean) algorithm :
import plotly.figure_factory as ff
import scipy.cluster.hierarchy as sch
import numpy as np
X = np.matrix([[0,0,0,0],[13,0,0,0],[2,14,0,0],[17,1,18,0]])
names = "0123"
fig = ff.create_dendrogram(X,
orientation='left',
labels=names,
linkagefun=lambda x: sch.linkage(x, "average"),)
fig.update_layout(width=800, height=800)
fig.show()
请注意,X
必须是数据样本矩阵。
关于python - 带绘图的树状图 - 如何为层次聚类设置自定义链接方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61039989/