Plot Feature Importance with feature names(使用要素名称绘制要素重要性)
本文介绍了使用要素名称绘制要素重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在R中有预先构建的函数来绘制随机森林模型的特征重要性。但在蟒蛇中,似乎缺少这种方法。我在matplotlib中搜索方法。
model.feature_importances提供以下信息:
array([ 2.32421835e-03, 7.21472336e-04, 2.70491223e-03,
3.34521084e-03, 4.19443238e-03, 1.50108737e-03,
3.29160540e-03, 4.82320256e-01, 3.14117333e-03])
然后使用以下绘图函数:
>> pyplot.bar(range(len(model.feature_importances_)), model.feature_importances_)
>> pyplot.show()
我得到的是条形图,但我希望得到带有标签的条形图,同时以排序的方式水平显示重要性。我也在探索seaborn,但找不到方法。
推荐答案
不能完全确定您要查找的内容。从here派生了一个示例。如评论中所述:如果要自定义要素标签,可以将indices更改为plt.yticks(range(X.shape[1]), indices)行的标签列表。
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.ensemble import ExtraTreesClassifier
# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000,
n_features=10,
n_informative=3,
n_redundant=0,
n_repeated=0,
n_classes=2,
random_state=0,
shuffle=False)
# Build a forest and compute the feature importances
forest = ExtraTreesClassifier(n_estimators=250,
random_state=0)
forest.fit(X, y)
importances = forest.feature_importances_
std = np.std([tree.feature_importances_ for tree in forest.estimators_],
axis=0)
indices = np.argsort(importances)
# Plot the feature importances of the forest
plt.figure()
plt.title("Feature importances")
plt.barh(range(X.shape[1]), importances[indices],
color="r", xerr=std[indices], align="center")
# If you want to define your own labels,
# change indices to a list of labels on the following line.
plt.yticks(range(X.shape[1]), indices)
plt.ylim([-1, X.shape[1]])
plt.show()
这篇关于使用要素名称绘制要素重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:使用要素名称绘制要素重要性
基础教程推荐
猜你喜欢
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
