错误提示表明在RandomForestClassifier对象中没有名为“coef_”的属性。这是因为随机森林模型不是基于线性回归的模型,因此没有权重系数。如果您想绘制特征的重要性,可以使用RandomForestClassifier对象的“feature_importances_”属性。

修改代码如下:

# 绘制特征重要性图
importances = rf.feature_importances_
labels = X.columns.values
plt.bar(np.arange(len(labels)), importances)
plt.xticks(np.arange(len(labels)), labels, rotation=90)
plt.show()

这将绘制一个条形图,显示每个特征的重要性。请注意,您可以使用plt.xticks()函数来旋转标签。

AttributeError Traceback most recent call lastCell In8 line 2 1 # 绘制特征权重图---- 2 coef = rfcoef_ravel 3 labels = Xcolumnsvalues 4 pltbarnparangelenlabels coefAt

原文地址: http://www.cveoy.top/t/topic/hnRs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录