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

发布于 2025-01-22 21:41:09 字数 1854 浏览 6 评论 0 原文

我在 create_dendrogram in plotly.figure_factory 中绘制树状图时遇到一些困难。

默认的linkageFun( linkageFun )是完整,距离函数的默认设置( distfun )为 scs.distance.pdist 代码>

但是我想要的设置是 jaccard distfun for for linke> linking> linking> linke> linkagefun : 我想要的设置以下显示:

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 可以通过 linkefun = lambda x:sch.linkage(x,'faluse')设置,但是 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在网页上绘制图形,因此我似乎必须在Plotly中使用 create_dendrogram

I am meeting some difficulties when drawing a dendrogram by create_dendrogram in plotly.figure_factory.

the default linkagefun (linkagefun) is complete and the default setting of distance function (distfun) is scs.distance.pdist

but the setting I want is jaccard for distfun, and average for linkagefun:
the setting I want shows below:

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)

I noticed that the linkagefun could be set by linkagefun=lambda x: sch.linkage(x, 'average'), but the distfun can't be set by distfun='jaccard', and I have no idea of how to set this function.


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

the example of the df set below:

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'])

since I need Dash to plot the figure on the web page, it seems I have to use create_dendrogram in plotly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会发光的星星闪亮亮i 2025-01-29 21:41:09

您可以从 functions 中使用 partial 来“冻结” scipy.spatial.distance.pdist.pdist 指定距离度量的参数。

    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'))

You can use partial from functools to "freeze" the parameter of scipy.spatial.distance.pdist that specifies the distance metric.

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

Then use the partial function as the input for distfun:

fig = create_dendrogram(df, orientation='left',
                        labels=df.index,
                        distfun=pw_jaccard_func ,
                        linkagefun=lambda x: sch.linkage(x, 'average'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文