将列名称映射到随机森林特征重要性

新手上路,请多包涵

我正在尝试绘制随机森林模型的特征重要性并将每个特征重要性映射回原始系数。我设法创建了一个显示重要性的图,并使用原始变量名称作为标签,但现在它按照变量名称在数据集中的顺序(而不是按重要性顺序)对变量名称进行排序。我如何按照功能重要性对它们进行排序?谢谢!

在此处输入图像描述

我的代码是:

 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()

原文由 yogz123 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 408
2 个回答

一种通用的解决方案是将特征/重要性放入数据框中并在绘图之前对它们进行排序:

 import pandas as pd
%matplotlib inline
#do code to support model
#"data" is the X dataframe and model is the SKlearn object

feats = {} # a dict to hold feature_name: feature_importance
for feature, importance in zip(data.columns, model.feature_importances_):
    feats[feature] = importance #add the name/value pair

importances = pd.DataFrame.from_dict(feats, orient='index').rename(columns={0: 'Gini-importance'})
importances.sort_values(by='Gini-importance').plot(kind='bar', rot=45)

原文由 Sam 发布,翻译遵循 CC BY-SA 3.0 许可协议

很简单,我是这样画的。

 feat_importances = pd.Series(extraTree.feature_importances_, index=X.columns)
feat_importances.nlargest(15).plot(kind='barh')
plt.title("Top 15 important features")
plt.show()

原文由 Joshy Joy 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题