如何访问scikit-learn中管道化GridSearchCV内部估计器的属性?

我想访问以下代码中DecisionTreeRegressor的feature_importances_属性:

# 创建估计器
from sklearn.tree import DecisionTreeRegressor
tree_reg = DecisionTreeRegressor(criterion='mse', random_state=0)
# 创建GridSearchCV的参数网格
param_grid = {  
    'max_depth':np.linspace(start=4, stop=12, num=9),
    'max_leaf_nodes':[i for i in range(10,20,1)]
}
# 在参数空间上构建gridsearchcv
from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import GridSearchCV
cv = ShuffleSplit(n_splits=10, test_size=0.30, random_state=0)
grid = GridSearchCV(estimator=tree_reg, param_grid=param_grid, cv=cv, refit=True)
# 构建管道
from sklearn.pipeline import Pipeline
pipe = Pipeline(steps=[('preprocess', StandardScaler()), ('grid_search', grid)])
pipe.fit(X_train, y_train)
feat_impo = tree_reg.feature_importances_ # 在这一行获取错误

我想访问DecisionTreeRegressor的feature_importances_属性,但在执行tree_reg.feature_importances_时得到了以下错误:

sklearn.exceptions.NotFittedError: This DecisionTreeRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

我也尝试了这个:

grid.__getattribute__('estimator').feature_importances_

但得到了完全相同的结果。

但是,当我运行不使用管道和网格搜索的程序时,即仅使用DecisionTreeRegressor,我可以轻松地使用tree_reg.feature_importances_访问feature_importances_,并且没有错误地获得所需的结果。

我如何访问DecisionTreeRegressor的feature_importances_属性?


回答:

最后,我找到了正确的方法来实现它,

best_est = grid.best_estimator_
feat_impo = best_est.feature_importances_

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

发表回复

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