sklearn中管道对象的返回系数 [英] return coefficients from Pipeline object in 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屋!