在ScikitLearn的管道里发生了什么?

2024-04-24 15:09:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读Scikit Learn的Pipeline部分,来自“使用Scikit Learn和Tensorflow进行实际操作的机器学习”。本书对管道的解释如下

When you call the pipeline’s fit() method, it calls fit_transform() sequentially on all transformers, passing the output of each call as the parameter to the next call, until it reaches the final estimator, for which it just calls the fit() method

我试图从文本中推断管道在Python中是如何工作的。在

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

num_pipeline = Pipeline([
    ('imputer', Imputer(strategy="median")),
    ('std_scaler', StandardScaler()),
])

result = num_pipeline.fit(data)
result

一开始,我以为这就是Python内部的情况:

  1. 创建Imputer(strategy="median")的实例,它是imputer
  2. 执行imputer.fit_transform(data)并将对象返回到std_scaler.fit方法的输入,其中std_scaler是{}的一个实例
  3. 返回self(在本例中为std_标量),因为fit方法返回self

但结果是

^{pr2}$

管道里发生了什么?在


Tags: the管道pipelinetransformitscikitcalllearn