【发布时间】:2019-01-18 18:42:42
【问题描述】:
我正在尝试使用交叉验证折叠生成随机森林的特征重要性图。当仅使用特征(X)和目标(y)数据时,实现很简单,例如:
rfc = RandomForestClassifier()
rfc.fit(X, y)
importances = pd.DataFrame({'FEATURE':data_x.columns,'IMPORTANCE':np.round(rfc.feature_importances_,3)})
importances = importances.sort_values('IMPORTANCE',ascending=False).set_index('FEATURE')
print(importances)
importances.plot.bar()
plt.show()
但是,我如何转换此代码以便为我将创建的每个交叉验证折叠 (k-fold) 创建一个类似的图?
我现在的代码是:
# Empty list storage to collect all results for displaying as plots
mylist = []
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold
kf = KFold(n_splits=3)
for train, test in kf.split(X, y):
train_data = np.array(X)[train]
test_data = np.array(y)[test]
for rfc = RandomForestClassifier():
rfc.fit(train_data, test_data)
例如,上面的代码使用交叉验证技术创建(3 个折叠),我的目标是为所有 3 个折叠创建特征重要性图,从而生成 3 个特征重要性图。目前,它给了我循环错误。
我不确定使用每个创建的(k-folds)分别通过随机森林为每个(k-folds)生成特征重要性图的最有效技术是什么。
【问题讨论】:
标签: python scikit-learn random-forest