问题描述
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))