【问题标题】:Applying a plotly dendrogram to text data将绘图树状图应用于文本数据
【发布时间】:2020-01-13 13:29:02
【问题描述】:

我正在尝试使用文本数据而不是数字来创建一个树状图,如 here 所示。

这个简单的例子有效:

from plotly import figure_factory as ff

ff.create_dendrogram(
    X=np.array([ [1], [2], [3] ]),
)

同样,我尝试使用具有定义距离函数的文本数据:

from difflib import ndiff

ff.create_dendrogram(
    X=np.array([ ['foo'], ['bar'], ['baz'] ]),
    distfun=lambda x, y: len(list(ndiff(x, y))),    # e.g. len(list(ndiff('bar', 'baz'))) --> 4
)

但它会引发以下错误:

TypeError: <lambda>() missing 1 required positional argument: 'y'

【问题讨论】:

    标签: python text plotly dendrogram plotly-python


    【解决方案1】:

    我发现了问题。

    create_dendrogram 需要一个距离函数 (distfun),其签名类似于 pdist

    所以这修复了它:

    from functools import partial
    from scipy.spatial.distance import pdist
    
    ff.create_dendrogram(
        X=np.array([ ['foo'], ['bar'], ['baz'] ]),
        distfun=partial(pdist, metric=lambda x, y: len(list(ndiff(x, y)))),
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-06
      • 2020-11-05
      • 2013-07-25
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      相关资源
      最近更新 更多