我想绘制随机森林模型的要素重要性并将每个要素重要性映射回原始系数。我设法创建了一个显示重要性并使用原始变量名称作为标签的图形,但现在它按照它们在数据集中的顺序排序变量名称(而不是按重要性排序)。我如何按照功能重要性排序?谢谢!将列名称映射到随机森林要素重要性
我的代码是:
importances = brf.feature_importances_
std = np.std([tree.feature_importances_ for tree in brf.estimators_],
axis=0)
indices = np.argsort(importances)[::-1]
# Print the feature ranking
print("Feature ranking:")
for f in range(x_dummies.shape[1]):
print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
# Plot the feature importances of the forest
plt.figure(figsize=(8,8))
plt.title("Feature importances")
plt.bar(range(x_train.shape[1]), importances[indices],
color="r", yerr=std[indices], align="center")
feature_names = x_dummies.columns
plt.xticks(range(x_dummies.shape[1]), feature_names)
plt.xticks(rotation=90)
plt.xlim([-1, x_dummies.shape[1]])
plt.show()
你还没有包括你目前得到的情节? –
已编辑!我不确定剧情增加了多少价值,因为我只是想改变底部x标签的顺序。对于小字体的道歉,这是将大部分图片放入屏幕截图的唯一方法。 – yogz123
'plt.bar(范围(x_dummies.shape [1]),重要性[indices], color =“r”,yerr = std [indices],align =“center”)'? –