【问题标题】:How can I access "each estimater " in scikit-learn pipelines? [closed]如何访问 scikit-learn 管道中的“每个估计器”? [关闭]
【发布时间】:2021-07-19 02:27:03
【问题描述】:

如何在管道中访问"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

【问题讨论】:

  • 从字典中获取条目,可以使用pipelines["Log"]pipelines.get("Log")
  • 您的管道名为pipelines,而不是Pipelines,首字母大写L(错字)。
  • 感谢您的修改。非常有帮助。

标签: python machine-learning scikit-learn pipeline


【解决方案1】:

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

  1. 对于第一级(子管道),只需使用dict['key']
  2. 对于第二级(子管道中的步骤),您可以再次使用named_steps 获取带有步骤的dict,然后以相同的方式引用每个步骤。

这是代码-

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)

【讨论】:

  • 我试过这段代码,现在一切都清楚了。非常感谢!!
猜你喜欢
  • 2019-12-04
  • 2020-10-14
  • 2021-08-17
  • 2015-08-03
  • 2013-06-04
  • 1970-01-01
  • 2015-08-14
  • 2017-06-18
  • 2014-06-27
相关资源
最近更新 更多