super __str__ isnt getting called
我继承了
1 2 3 | class my_rf(RandomForestClassifier): def __str__(self): return"foo_" + RandomForestClassifier.__str__(self) |
给
我也尝试过:
1 2 3 | class my_rf(RandomForestClassifier): def __str__(self): return"foo_" + super(RandomForestClassifier, self).__str__() |
结果相同。预期类似于sklearn默认行为:
1 2 3 4 5 6 7 8 9 | >>> a = RandomForestClassifier() >>> print a 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) >>> |
这也是我使用
我错过了什么?谢谢。
与如何更改Python类的字符串表示形式相关?
在两个
这是知识的更新,你可以得到你的所需的输出,虽然I Don’t get,为什么你想要什么。有一个原因,为什么
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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你有没有更新你
1 2 3 4 5 6 7 8 9 10 | 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. |