从scikit-learn管道获取模型属性

lma*_*999 33 python scikit-learn neuraxle

我通常得到这样的PCA负载:

pca = PCA(n_components=2)
X_t = pca.fit(X).transform(X)
loadings = pca.components_
Run Code Online (Sandbox Code Playgroud)

如果我PCA使用scikit-learnpipline 运行...

from sklearn.pipeline import Pipeline
pipeline = Pipeline(steps=[    
('scaling',StandardScaler()),
('pca',PCA(n_components=2))
])
X_t=pipeline.fit_transform(X)
Run Code Online (Sandbox Code Playgroud)

......有可能获得负荷吗?

只是尝试loadings = pipeline.components_失败:

AttributeError: 'Pipeline' object has no attribute 'components_'
Run Code Online (Sandbox Code Playgroud)

谢谢!

(也有兴趣coef_从学习管道中提取属性.)

And*_*ler 62

您是否看过文档:http://scikit-learn.org/dev/modules/pipeline.html 我觉得很清楚.

有两种方法可以使用索引或使用您给出的字符串名称来获取管道中的步骤:

pipeline['pca']
Run Code Online (Sandbox Code Playgroud)

这将为您提供PCA对象,您可以在其上获取组件.

  • 我想通过添加以下内容来劫持这个答案,如果您的管道上有一个“regr = TransformedTargetRegressor”,那么语法是不一样的,相反,您必须在访问命名步骤之前使用“regressor_”访问回归器,即“ regr.regressor_.named_steps['pca'].components_`。 (2认同)

Gui*_*ier 5

使用Neuraxle

使用Neuraxle可以更轻松地处理管道。例如,您可以执行以下操作:

from neuraxle.pipeline import Pipeline

# Create and fit the pipeline: 
pipeline = Pipeline([
    StandardScaler(),
    PCA(n_components=2)
])
pipeline, X_t = pipeline.fit_transform(X)

# Get the components: 
pca = pipeline[-1]
components = pca.components_
Run Code Online (Sandbox Code Playgroud)

您可以按照以下三种方式访问​​PCA:

  • pipeline['PCA']
  • pipeline[-1]
  • pipeline[1]

Neuraxle是基于scikit-learn构建的流水线库,可将流水线带入新的水平。它允许轻松管理超参数分布,嵌套管道,保存和重新加载,REST API服务等的空间。整个过程还使用了深度学习算法并允许并行计算。

嵌套管道:

您可以在管道中包含管道,如下所示。

# Create and fit the pipeline: 
pipeline = Pipeline([
    StandardScaler(),
    Identity(),
    Pipeline([
        Identity(),  # Note: an Identity step is a step that does nothing. 
        Identity(),  # We use it here for demonstration purposes. 
        Identity(),
        Pipeline([
            Identity(),
            PCA(n_components=2)
        ])
    ])
])
pipeline, X_t = pipeline.fit_transform(X)
Run Code Online (Sandbox Code Playgroud)

然后,您需要执行以下操作:

# Get the components: 
pca = pipeline["Pipeline"]["Pipeline"][-1]
components = pca.components_
Run Code Online (Sandbox Code Playgroud)