【发布时间】:2021-05-05 18:09:49
【问题描述】:
Sklearn 有一个很好的但未知的可视化,可以通过sklearn.set_config(display='diagram') 激活。我正在尝试自定义可视化的输出,但无法弄清楚 html 输出是如何生成的。我知道 python 的魔术方法 __str__ 和 __repr__ 可用于创建某些对象的文本表示。我预计 __repr__ 将用于创建 html 输出。为了测试这个假设,我重写了输出字符串“repr”的方法。如以下代码及其输出所示,调用了 __repr__ 方法,但显然它不用作 html 生成的入口点,因为这将导致单个输出:“repr”。
import sklearn
from sklearn.base import BaseEstimator
from sklearn.pipeline import Pipeline
sklearn.set_config(display='diagram')
class DummyPipeline(Pipeline):
def __repr__(self, *args):
print("repr")
return "__repr__"
def __str__(self, *args):
print("str")
return ("__str__")
class DummyEstimator(BaseEstimator):
def fit(self, X, y=None):
pass
def transform(self, X, y=None):
pass
DummyPipeline(steps=[('first_estimator', DummyEstimator()), ('second_estimator', DummyEstimator())])
【问题讨论】:
标签: python scikit-learn visualization pipeline