sklearn中管道对象的返回系数

2024-05-14 22:10:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用RandomizedSearchCV匹配了一个Pipeline对象

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_

sgd_randomized_pipe.best_estimator_.coef_

但是我得到了以下的属性错误。。。

AttributeError:“Pipeline”对象没有属性“coef”

scikit学习文档说coef_SGDClassifier的一个属性,这是我的base_estimator_类。

我做错什么了?


Tags: 对象属性pipelineparamdistjobsclfpipe
3条回答

通过使用named_steps指令,在生成管道时始终可以使用分配给它们的名称

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

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

这是管道公开为specified in the documentation的形式属性:

named_steps : dict

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

我认为这应该管用:

sgd_randomized_pipe.named_steps['clf'].coef_

我找到了一种方法,通过使用steps属性链接索引。。。

sgd_randomized_pipe.best_estimator_.steps[1][1].coef_

这是最佳实践,还是有其他方法?

相关问题 更多 >

    热门问题