在 scikit-learn Pipeline 中插入或删除一个步骤 [英] Insert or delete a step in scikit-learn Pipeline
问题描述
是否可以在 sklearn.pipeline.Pipeline
对象中删除或插入步骤?
Is it possible to delete or insert a step in a sklearn.pipeline.Pipeline
object?
我正在尝试使用或不使用 Pipeline 对象中的一个步骤进行网格搜索.想知道我是否可以在管道中插入或删除一个步骤.我在 Pipeline
源代码中看到,有一个 self.steps
对象保存所有步骤.我们可以通过 named_steps()
获取步骤.在修改它之前,我想确保,我不会造成意外的影响.
I am trying to do a grid search with or without one step in the Pipeline object. And wondering whether I can insert or delete a step in the pipeline. I saw in the Pipeline
source code, there is a self.steps
object holding all the steps. We can get the steps by named_steps()
. Before modifying it, I want to make sure, I do not cause unexpected effects.
这是一个示例代码:
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
estimators = [('reduce_dim', PCA()), ('svm', SVC())]
clf = Pipeline(estimators)
clf
我们是否有可能执行类似steps = clf.named_steps()
的操作,然后在此列表中插入或删除?这是否会对 clf 对象造成不良影响?
Is it possible that we do something like steps = clf.named_steps()
, then insert or delete in this list? Does this cause undesired effect on the clf object?
推荐答案
我看大家只提到了删除步骤.如果您还想在管道中插入一个步骤:
I see that everyone mentioned only the delete step. In case you want to also insert a step in the pipeline:
pipe.steps.append(['step name',transformer()])
pipe.steps
与列表的工作方式相同,因此您也可以将项目插入到特定位置:
pipe.steps
works in the same way as lists do, so you can also insert an item into a specific location:
pipe.steps.insert(1,['estimator',transformer()]) #insert as second step
这篇关于在 scikit-learn Pipeline 中插入或删除一个步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!