如何使用Pipeline和GridSearchCV查找LinearRegression问题的系数

我在使用Pipeline和GridSearchCV执行LinearRegression模型时,无法获取到为X_train的每个特征计算的系数。

mlr_gridsearchcv = Pipeline(steps =[('preprocessor', preprocessor),('gridsearchcv_lr', GridSearchCV(TransformedTargetRegressor(regressor= LinearRegression(), func = np.log,inverse_func = np.exp), param_grid=parameter_lr, cv = nfolds, scoring = ('r2','neg_mean_absolute_error'), return_train_score = True, refit='neg_mean_absolute_error', n_jobs = -1))])mlr_co2=mlr_gridsearchcv.fit(X_train,Y_train['co2e'])

我首先尝试获取best_estimator_:

mlr_co2.named_steps['gridsearchcv_lr'].cv_results_.best_estimator_

结果得到:

AttributeError: 'dict' object has no attribute 'best_estimator_'

如果我尝试这种方式:

mlr_co2.named_steps['gridsearchcv_lr'].best_estimator_.regressor.coef_

我得到:

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

我尝试了其他组合,但似乎都不起作用。


回答:

你可以使用:

results['gridsearchcv'].best_estimator_.regressor_.coef_

其中results是已拟合的pipeline,'gridsearchcv'是pipeline中网格搜索步骤的名称,请参见下面的代码。

import numpy as npfrom sklearn.pipeline import Pipelinefrom sklearn.linear_model import LinearRegressionfrom sklearn.model_selection import GridSearchCVfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.compose import TransformedTargetRegressornp.random.seed(42)# 生成数据X = np.random.lognormal(0, 1, (100, 3))y = np.mean(X, axis=1) + np.random.normal(0, 0.1, 100)# 定义pipelinepreprocessor = MinMaxScaler(feature_range=(0, 1))estimator = TransformedTargetRegressor(    regressor=LinearRegression(),    func=np.log,    inverse_func=np.exp)gridsearchcv = GridSearchCV(    estimator,    param_grid={'regressor__fit_intercept': [True, False]},    cv=5,    scoring=('r2', 'neg_mean_absolute_error'),    return_train_score=True,    refit='neg_mean_absolute_error',    n_jobs=-1)pipeline = Pipeline(steps=[    ('preprocessor', preprocessor),    ('gridsearchcv', gridsearchcv)])# 拟合pipelineresults = pipeline.fit(X, y)# 提取最佳模型的估计系数results['gridsearchcv'].best_estimator_.regressor_.coef_# [0.89791824 1.11311974 2.99750775]

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中创建了一个多类分类项目。该项目可以对…

发表回复

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