【问题标题】:Changing a method of a class of an imported module in python在python中更改导入模块的类的方法
【发布时间】:2022-09-27 15:25:44
【问题描述】:

我正在使用 Seaborn 库在我的程序中生成一个集群图。它使用 Scipy 来执行此操作,但 Scipy 中有一个名为 optimal_leaf_ordering 的特殊参数,Seaborn 没有选择。 我想我可以在我的程序中使用 Seaborn 中的类创建一个继承类,该类绘制集群图,并确保它将optimal_ordering=True 传递给 Scipy,如下所示:

from scipy.cluster import hierarchy
from seaborn.matrix import _DendrogramPlotter

class _DendrogramPlotter(_DendrogramPlotter):
  def _calculate_linkage_scipy(self):
        linkage = hierarchy.linkage(self.array, method=self.method,
                                    metric=self.metric, optimal_ordering=True)
        return linkage

但这似乎并没有改变我的程序中的任何内容。有人可以解释我哪里出错了吗?

    标签: python scipy seaborn


    【解决方案1】:

    您需要使其成为静态方法而不是实例方法,只需从中删除 self 参数:

    from scipy.cluster import hierarchy
    from seaborn.matrix import _DendrogramPlotter
    
    class _DendrogramPlotter(_DendrogramPlotter):
      def _calculate_linkage_scipy():
            linkage = hierarchy.linkage(self.array, method=self.method,
                                        metric=self.metric, optimal_ordering=True)
            return linkage
    

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 2020-07-23
      • 2011-12-17
      • 2016-03-20
      • 1970-01-01
      相关资源
      最近更新 更多