如何在plotly.figurefactory中设置create_dendrogram的distfun



plotly.figure_factory中用create_dendrogram绘制树状图时遇到了一些困难。

默认linkagefun(linkagefun)为complete,距离函数(distfun)的默认设置为scs.distance.pdist

但是我想要的设置对于distfunjaccard并且对于linkagefunaverage:我想要的设置显示如下:

import pandas as pd
import numpy as np
from scipy.spatial.distance import pdist
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as such
plt.figure(figsize = (10, 10))
disMat = sch.distance.pdist(df, metric='jaccard')
disMat1 = sch.distance.squareform(disMat)
Z=sch.linkage(disMat1,method='average')
Dend=sch.dendrogram(Z,orientation='right')
plt.tick_params(
axis='y',          
which='both',      
direction='in',   
left=False,      
right=False,         
labelleft=False)

我注意到linkagefun可以由linkagefun=lambda x: sch.linkage(x, 'average')设置,但distfun不能由distfun='jaccard'设置,我不知道如何设置这个功能。


fig = create_dendrogram(df, orientation='left',
labels=df.index,
distfun='jaccard',
linkagefun=lambda x: sch.linkage(x, 'average'))
fig.show()

下面的df集示例:

import pandas as pd
df = pd.DataFrame({'1-7':[0,0,1,1,0,1,1],'1-2':[1,0,1,0,0,1,1],'2-3':[1,0,0,0,1,1,0],'2-2':[0,1,0,1,0,1,1],'1-1':[1,0,0,1,0,1,0],'1-3':[0,1,1,1,0,0,0],'1-5':[0,1,0,1,1,0,1]},index=['a','b','c','d','e','f','g'])

由于我需要Dash在网页上绘制图形,所以我似乎不得不在绘图中使用create_dendrogram

您可以使用partialfunctools到"冻结";CCD_ 18的指定距离度量的参数。

from functools import partial
from scipy.spatial.distance import pdist
pw_jaccard_func = partial(pdist, metric='jaccard')

然后使用部分函数作为distfun:的输入

fig = create_dendrogram(df, orientation='left',
labels=df.index,
distfun=pw_jaccard_func ,
linkagefun=lambda x: sch.linkage(x, 'average'))

相关内容

  • 没有找到相关文章

最新更新