首页 > 解决方案 > 特征重要性分布图

问题描述

我基于此在我的数据框中进行了特征选择: https ://towardsdatascience.com/feature-selection-using-random-forest-26d7b747597f

在第 7 部分,为了绘制重要性分布,提供了以下代码:

pd.series(sel.estimator_,feature_importances_,.ravel()).hist()

我认为没有语法错误应该是这样的:

pd.series(sel.estimator_,feature_importances_.ravel()).hist()

我收到了这个错误:

AttributeError:模块“熊猫”没有属性“系列”

我认为 estimator_ 和 feature_importances_ 也没有定义。有没有办法调试这行代码? 在此处输入图像描述

标签: pythonmatplotlibplotrandom-forest

解决方案


pd.Series(sel.estimator_.feature_importances_.ravel()).hist()

是“系列”而不是“系列”

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.hist.html

绘制特征重要性

importances = sel.estimator_.feature_importances_
indices = np.argsort(importances)[::-1]
# X is the train data used to fit the model 
plt.figure()
plt.title("Feature importances")
plt.bar(range(X.shape[1]), importances[indices],
       color="r", align="center")
plt.xticks(range(X.shape[1]), indices)
plt.xlim([-1, X.shape[1]])
plt.show()

这应该呈现如下条形图,其中 x 轴是特征索引,y 轴是特征重要性。特征按重要性排序。 在此处输入图像描述


推荐阅读