Guest User

Untitled

a guest
Nov 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. def feature_importance_plot(df,target):
  2. from sklearn.ensemble import RandomForestClassifier
  3. model = RandomForestClassifier()
  4. y = df[target]
  5. x = df.drop(target,axis=1)
  6. model.fit(x,y)
  7. res_df = pd.DataFrame({'feature':x.columns,'importance':model.feature_importances_})
  8. res = res_df.sort_values('importance',ascending=False)
  9. res['cum_importance'] = res.importance.cumsum()
  10. plt.subplot(211)
  11. sns.barplot(res.feature,res.importance)
  12. plt.subplot(212)
  13. plt.plot(np.arange(1,res.shape[0]+1),res.cum_importance,linewidth=2)
  14. return(res)
Add Comment
Please, Sign In to add comment