【问题标题】:How to modify sklearn's pipeline visualization (what it used instead of __repr__ and __str__?)如何修改 sklearn 的管道可视化(它用什么代替了 __repr__ 和 __str__?)
【发布时间】: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())])

返回:

因此,问题是:我需要哪种方法来更改 html 表示?

【问题讨论】:

    标签: python scikit-learn visualization pipeline


    【解决方案1】:

    主要方法是sklearn.utils.estimator_html_repr;查看它的API docsUser Guidethis file 中的代码。

    该函数调用str(estimator),因此这是大多数估算器为 html 生成输出的地方(如您在示例中看到的,DummyPipeline 打印 "__str__")。元估计器(如管道)在代码的this section 中进行检查,Pipeline 本身在其_sk_visual_block_ 方法here 中得到一些特殊处理。

    因此,根据您要更改的具体内容,您可能需要在很多地方进行更改。我之前已经从estimator_html_repr.py 文件中对_STYLE 常量进行了猴子修补;由于没有太多的复合估计器,这可以很好地工作。

    【讨论】:

      猜你喜欢
      • 2011-04-11
      • 2020-02-07
      • 2019-04-05
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 2013-08-25
      • 2018-11-23
      相关资源
      最近更新 更多