sklearn中管道对象的返回系数 [英] return coefficients from Pipeline object in sklearn

查看:171
本文介绍了sklearn中管道对象的返回系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将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)

我想访问best_estimator_coef_属性,但是我无法做到这一点.我尝试使用下面的代码访问coef_.

I want to access the coef_ attribute of the best_estimator_ but I'm unable to do that. I've tried accessing coef_ with the code below.

sgd_randomized_pipe.best_estimator_.coef_

但是我得到以下AttributeError ...

However I get the following AttributeError...

AttributeError:管道"对象没有属性"coef _"

AttributeError: 'Pipeline' object has no attribute 'coef_'

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

The scikit-learn docs say that coef_ is an attribute of SGDClassifier, which is the class of my base_estimator_.

我在做什么错了?

推荐答案

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

You can always use the names you assigned to them while making the pipeline by using the named_steps dict.

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

,然后访问所有coef_intercept_等属性,这些属性可用于相应的拟合估计量.

and then access all the attributes like coef_, intercept_ etc. which are available to corresponding fitted estimator.

这是管道公开的形式化属性,其定义为文档:

This is the formal attribute exposed by the Pipeline as specified in the documentation:

named_steps :字典

只读属性,可通过用户给定名称访问任何步骤参数.键是步骤名称,值是步骤参数.

Read-only attribute to access any step parameter by user given name. Keys are step names and values are steps parameters.

这篇关于sklearn中管道对象的返回系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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