我如何访问“每个估计器"?在 scikit-learn 管道中? [英] How can I access "each estimater " in scikit-learn pipelines?

查看:78
本文介绍了我如何访问“每个估计器"?在 scikit-learn 管道中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在管道中访问Log"?

pipelines = {
    "Log": Pipeline(
        [("scl", StandardScaler()), ("est", LogisticRegression(random_state=1))]
    ),
    "Rf": Pipeline([("est", RandomForestClassifier(random_state=1))]),
    "Rf_Pipeline": Pipeline(
        [
            ("scl", StandardScaler()),
            ("reduct", PCA(n_components=10, random_state=1)),
            ("est", RandomForestClassifier(random_state=1)),
        ]
    ),
}


Pipelines.item(Log)

目前我得到:

NameError: name 'Log' is not defined

推荐答案

管道对象可以看作是字典.在您的情况下,您已将多个管道存储到字典中.要访问不同的密钥(管道),您只需使用 dict['key']dict.get['key'].

The pipeline objects can be viewed as dictionaries. In your case, you have stored multiple pipelines into a dictionary. To access the different keys (pipelines) you can simply use dict['key'] or dict.get['key'].

  1. 对于第一级(子管道),只需使用 dict['key']
  2. 对于第二级(子管道内的步骤),您可以再次使用 named_steps 获取带有步骤的 dict,然后以相同的方式引用每个步骤.
  1. For the first level (sub pipelines), simply use dict['key']
  2. For the second level, (steps inside sub pipelines), again, you can fetch the dict with steps using named_steps and then refer to each step the same way.

这是代码-

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifier

pipelines = {
    "Log": Pipeline(
        [("scl", StandardScaler()), ("est", LogisticRegression(random_state=1))]
    ),
    "Rf": Pipeline([("est", RandomForestClassifier(random_state=1))]),
    "Rf_Pipeline": Pipeline(
        [
            ("scl", StandardScaler()),
            ("reduct", PCA(n_components=10, random_state=1)),
            ("est", RandomForestClassifier(random_state=1)),
        ]
    ),
}

first_subpipeline = pipelines['Log']
second_step_first_subpipeline =  pipelines['Log'].named_steps['est']

print(first_subpipeline)
print(second_step_first_subpipeline)

Pipeline(steps=[('scl', StandardScaler()),
                ('est', LogisticRegression(random_state=1))])

LogisticRegression(random_state=1)

这篇关于我如何访问“每个估计器"?在 scikit-learn 管道中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆