微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在python中绘制随机森林的特征重要性

如何解决如何在python中绘制随机森林的特征重要性

我创建了一个随机森林模型,并想绘制特征重要性

model_RF_tune = RandomForestClassifier(random_state=0,n_estimators = 80,min_samples_split =10,max_depth= None,max_features = "auto",)

我尝试定义一个函数

def plot_feature_importances_health(model):
    n_features = model.data.shape
    plt.barh(range(n_features),model.feature_importances_,align = "center")
    plt.yticks(np.arrange(n_features),df_health_reconstructed.feature_names)
    plt.xlabel("Feature importance")
    plt.ylabel("Feature")
    plt.ylim(-1,n_features)

但是这个 plot_feature_importances_health(model_RF_tune)

给出这个结果: AttributeError: 'RandomForestClassifier' 对象没有属性 'data'

如何正确绘制?

解决方法

并非所有模型都可以执行 model.data。你想试试我的代码吗?但是,代码仅绘制了前 10 个特征。

# use RandomForestClassifier to look for important key features
n = 10    # choose top n features
rfc = RandomForestClassifier(random_state=SEED,n_estimators=200,max_depth=3)
rfc_model = rfc.fit(X,y)

(pd.Series(rfc_model.feature_importances_,index=X.columns)
    .nlargest(n)
    .plot(kind='barh',figsize=[8,n/2.5],color='navy')
    .invert_yaxis())    # most important feature is on top,ie,descending order

ticks_x = np.linspace(0,0.5,6)   # (start,end,number of ticks)
plt.xticks(ticks_x,fontsize=15,color='black')
plt.yticks(size=15,color='navy' )
plt.title('Top Features derived by RandomForestClassifier',family='fantasy',size=15)
print(list((pd.Series(rfc_model.feature_importances_,index=X.columns).nlargest(n)).index))
,

这个好像对我有用

%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(dataframe_name.columns,model_name.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='barh',color="SeaGreen",figsize=(10,8))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。