matplotlib: Plot Feature Importance with feature names

后端 未结 3 1313
春和景丽
春和景丽 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:06

    Not exactly sure what you are looking for. Derived a example from here. As mentioned in the comment: you can change indices to a list of labels at line plt.yticks(range(X.shape[1]), indices) if you want to customize feature labels.

    import numpy as np
    import matplotlib.pyplot as plt
    
    from sklearn.datasets import make_classification
    from sklearn.ensemble import ExtraTreesClassifier
    
    # Build a classification task using 3 informative features
    X, y = make_classification(n_samples=1000,
                               n_features=10,
                               n_informative=3,
                               n_redundant=0,
                               n_repeated=0,
                               n_classes=2,
                               random_state=0,
                               shuffle=False)
    
    # Build a forest and compute the feature importances
    forest = ExtraTreesClassifier(n_estimators=250,
                                  random_state=0)
    
    forest.fit(X, y)
    importances = forest.feature_importances_
    std = np.std([tree.feature_importances_ for tree in forest.estimators_],
                 axis=0)
    indices = np.argsort(importances)
    
    # Plot the feature importances of the forest
    plt.figure()
    plt.title("Feature importances")
    plt.barh(range(X.shape[1]), importances[indices],
           color="r", xerr=std[indices], align="center")
    # If you want to define your own labels,
    # change indices to a list of labels on the following line.
    plt.yticks(range(X.shape[1]), indices)
    plt.ylim([-1, X.shape[1]])
    plt.show()
    

提交回复
热议问题