RandomForestRegressor和feature_importances_错误 [英] RandomForestRegressor and feature_importances_ error

查看:605
本文介绍了RandomForestRegressor和feature_importances_错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力从我的RandomForestRegressor中提取功能的重要性,我得到了:

I am struggling to pull out the feature importances from my RandomForestRegressor, I get an:

AttributeError:"GridSearchCV"对象没有属性 'feature_importances _'.

AttributeError: 'GridSearchCV' object has no attribute 'feature_importances_'.

谁知道为什么没有属性?根据文档,应该存在此属性吗?

Anyone know why there is no attribute? According to documentation there should exist this attribute?

完整代码:

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV

#Running a RandomForestRegressor GridSearchCV to tune the model.
parameter_candidates = {
    'n_estimators' : [650, 700, 750, 800],
    'min_samples_leaf' : [1, 2, 3],
    'max_depth' : [10, 11, 12],
    'min_samples_split' : [2, 3, 4, 5, 6]
}

RFR_regr = RandomForestRegressor()
CV_RFR_regr = GridSearchCV(estimator=RFR_regr, param_grid=parameter_candidates, n_jobs=5, verbose=2)
CV_RFR_regr.fit(X_train, y_train)

#Predict with testing set
y_pred = CV_RFR_regr.predict(X_test)

#Extract feature importances
importances = CV_RFR_regr.feature_importances_

推荐答案

您正试图在GridSearchCV对象上使用该属性.它不存在.您真正需要做的是访问完成网格搜索的估算器.

You are trying to use the attribute on the GridSearchCV object. Its not present there. What you actually need to do is to access the estimator on which the grid search is done.

通过以下方式访问属性:

Access the attribute by :

importances = CV_RFR_regr.best_estimator_.feature_importances_

这篇关于RandomForestRegressor和feature_importances_错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆