从 sklearn 中的 Pipeline 对象返回系数

新手上路,请多包涵

我已将 Pipeline 对象与 RandomizedSearchCV

 pipe_sgd = Pipeline([('scl', StandardScaler()),
                    ('clf', SGDClassifier(n_jobs=-1))])

param_dist_sgd = {'clf__loss': ['log'],
                 'clf__penalty': [None, 'l1', 'l2', 'elasticnet'],
                 'clf__alpha': np.linspace(0.15, 0.35),
                 'clf__n_iter': [3, 5, 7]}

sgd_randomized_pipe = RandomizedSearchCV(estimator = pipe_sgd,
                                         param_distributions=param_dist_sgd,
                                         cv=3, n_iter=30, n_jobs=-1)

sgd_randomized_pipe.fit(X_train, y_train)

我想访问 --- 的 coef_ 属性 best_estimator_ 但我无法这样做。我尝试使用以下代码访问 coef_

sgd_randomized_pipe.best_estimator_.coef_

但是我得到以下 AttributeError …

AttributeError: ‘Pipeline’ 对象没有属性 ‘coef_’

scikit-learn 文档说 coef_SGDClassifier 的属性,这是我的类 base_estimator_

我究竟做错了什么?

原文由 spies006 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 801
2 个回答

在使用 named_steps dict 创建管道时,您始终可以使用分配给它们的名称。

 scaler = sgd_randomized_pipe.best_estimator_.named_steps['scl']
classifier = sgd_randomized_pipe.best_estimator_.named_steps['clf']

然后访问所有属性,如 coef_intercept_ 等可用于相应的拟合估计器。

这是 文档中指定 的管道公开的正式属性:

named_steps : 字典

只读属性,用于按用户给定名称访问任何步骤参数。键是步骤名称,值是步骤参数。

原文由 Vivek Kumar 发布,翻译遵循 CC BY-SA 4.0 许可协议

我认为这应该有效:

 sgd_randomized_pipe.named_steps['clf'].coef_

原文由 Roozbeh Bakhshi 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题