matplotlib: Plot Feature Importance with feature names

后端 未结 3 1253
春和景丽
春和景丽 2021-02-04 11:18

In R there are pre-built functions to plot feature importance of Random Forest model. But in python such method seems to be missing. I search for a method in matplotlib

3条回答
  •  后悔当初
    2021-02-04 12:02

    Quick answer for data scientists that ain't got no time to waste:

    Load the feature importances into a pandas series indexed by your column names, then use its plot method. For a classifier model trained using X:

    feat_importances = pd.Series(model.feature_importances_, index=X.columns)
    feat_importances.nlargest(20).plot(kind='barh')
    

    Slightly more detailed answer with a full example:

    Assuming you trained your model with data contained in a pandas dataframe, this is fairly painless if you load the feature importance into a panda's series, then you can leverage its indexing to get the variable names displayed easily. The plot argument kind='barh' gives us a horizontal bar chart, but you could easily substitute this argument for kind='bar' for a traditional bar chart with the feature names along the x-axis if you prefer.

    nlargest(n) is a pandas Series method which will return a subset of the series with the largest n values. This is useful if you've got lots of features in your model and you only want to plot the most important.

    A quick complete example using the classic Kaggle Titanic dataset...

    import pandas as pd
    from sklearn.ensemble import RandomForestClassifier
    %matplotlib inline            # don't forget this if you're using jupyter!
    
    X = pd.read_csv("titanic_train.csv")
    X = X[['Pclass', 'Age', 'Fare', 'Parch', 'SibSp', 'Survived']].dropna()
    y = X.pop('Survived')
    
    model = RandomForestClassifier()
    model.fit(X, y)
    
    (pd.Series(model.feature_importances_, index=X.columns)
       .nlargest(4)
       .plot(kind='barh'))        # some method chaining, because it's sexy!
    

    Which will give you this:

提交回复
热议问题