从 sklearn Pipeline 对象中返回系数

我已经使用 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…

AttributeError: ‘Pipeline’ object has no attribute ‘coef_’

scikit-learn 文档说明 coef_SGDClassifier 的属性,而 SGDClassifier 是我的 base_estimator_ 的类。

我做错了什么?


回答:

你总可以使用在创建管道时分配给它们的名称,通过使用 named_steps 字典来访问它们。

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

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

这是 Pipeline 正式暴露的属性,如文档中所指定

named_steps : dict

只读属性,通过用户指定的名称访问任何步骤参数。键是步骤名称,值是步骤参数。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注