从 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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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