我想访问以下代码中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_