我想在以下代码中访问DecisionTreeRegressor的feature_importances_属性:
#Create an estimator
from sklearn.tree import DecisionTreeRegressor
tree_reg = DecisionTreeRegressor(criterion='mse', random_state=0)
#Create parametre grid for GridSearchCV
param_grid = { 'max_depth':np.linspace(start=4, stop=12, num=9),
'max_leaf_nodes':[i for i in range(10,20,1)]}
# Construct gridsearchcv on param space
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)
#Make Pipeline
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_ #getting ERROR on this line
我想访问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时,我便可以轻松使用来访问feature_importances_tree_reg.feature_importances_
并获得理想的结果而不会出现任何错误。
如何访问DecisionTreeRegressor的feature_importances_属性?
最后,我找到了正确的方法,
best_est = grid.best_estimator_
feat_impo = best_est.feature_importances_
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句