【问题标题】:super __str__ isnt getting calledsuper __str__ 没有被调用
【发布时间】:2016-08-19 22:48:04
【问题描述】:

我正在继承 from sklearn.ensemble import RandomForestClassifier,我正在尝试打印我的新估算器:

class my_rf(RandomForestClassifier):
    def __str__(self):
        return "foo_" + RandomForestClassifier.__str__(self) 

foo_my_rf()

我也试过了:

class my_rf(RandomForestClassifier):
    def __str__(self):
        return "foo_" + super(RandomForestClassifier, self).__str__() 

结果相同。预期的东西很像 sklearn 默认行为:

>>> a = RandomForestClassifier() >>> 打印一个 RandomForestClassifier(bootstrap=True,class_weight=None,criteria='gini', max_depth=None, max_features='auto', max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0,n_estimators=10,n_jobs=1, oob_score=False,random_state=None,详细=0, 暖启动=假) >>>

这也是我使用print a.__str__()时的结果。

我错过了什么? 谢谢。

How do I change the string representation of a Python class?相关

【问题讨论】:

  • 显然父类__str__实现是类的名称。你说得对。
  • @jonrsharpe - 哎呀,编辑了问题以明确我在寻找什么。
  • 您是否尝试过查看__repr__
  • @jonrsharpe - 是的,结果相同。我必须在对象与类方面缺少一些东西,在 python 中我总是这样做:)
  • 不应该是super(my_rf, self).__str__吗?通过指定super(RandomForestClassifier, self),您实际上是跳过 RandomForestClassifier__str__ 的实现。

标签: python oop scikit-learn


【解决方案1】:

RandomForestClassifier 中,__repr____str__ 都查找调用它们的实例的类的名称 (self)。您应该直接引用超类的名称。

更新这就是你如何获得你想要的输出,虽然我不明白,你为什么想要这样的东西。 RandomForestClassifier__str____repr__ 返回类的实际名称是有原因的。这样你就可以eval 来恢复对象。总之,

In [1]: from sklearn.ensemble import RandomForestClassifier
In [2]: class my_rf(RandomForestClassifier):
    def __str__(self):
        superclass_name = RandomForestClassifier.__name__
        return "foo_" + superclass_name + "(" + RandomForestClassifier.__str__(self).split("(", 1)[1]

In [3]: forest = my_rf()
In [4]: print forest
foo_RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini', max_depth=None,
   max_features='auto', max_leaf_nodes=None, min_samples_leaf=1,
   min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=10,
   n_jobs=1, oob_score=False, random_state=None, verbose=0,
   warm_start=False)

更新 2 覆盖 __init__ 时不会获得任何参数,因为在超类中实现了 __str____repr__ 以扫描传递给 __init__ 的参数列表。运行这段代码就可以清楚的看到:

In [5]: class my_rf(RandomForestClassifier):
    def __init__(self, *args, **kwargs):
        RandomForestClassifier.__init__(self, *args, **kwargs)
    def __str__(self):
        superclass_name = RandomForestClassifier.__name__
        return "foo_" + superclass_name + "(" + RandomForestClassifier.__str__(self).split("(", 1)[1]
In [6]: forest = my_rf()
In [7]: print forest
...
RuntimeError: scikit-learn estimators should always specify their parameters in the signature of their __init__ (no varargs). <class '__main__.my_rf'> with constructor (<self>, *args, **kwargs) doesn't  follow this convention.

【讨论】:

  • 虽然这不是 OP 所要求的,但这可能是他们真正想要的。
  • 哎呀,编辑了问题以明确我在寻找什么。
  • @EliKorvigo,抱歉 - 这给出了 foo_RandomForestClassifier() 而不是所有估计器参数的正确表示。也许这是sklearn特有的东西...
  • @ihadanny 它显然确实为您提供了所有估计器参数的正确表示,如我的示例所示。
  • @EliKorvigo - 你是对的。就我而言,它不起作用,因为我做了另一件事,我也覆盖了 init :` def __init__(self): RandomForestClassifier.__init__(self, n_estimators=25, n_jobs=12, oob_score=False, max_features='sqrt', min_samples_leaf=1)`
猜你喜欢
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多