特征重要性分布图

distribution plot of feature importances

我已经根据这个在我的数据框中做了一个特征选择: 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: 模块 'pandas' 没有属性 'series'

而且我认为 estimator_ 和 feature_importances_ 也没有定义。 有什么办法可以调试这行代码吗?

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

是"Series"不是"series"

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 轴是特征重要性。功能按重要性排序。