如何访问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

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

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