An example showing the plot_feature_importances method used by a scikit-learn classifier

In this example, we'll be plotting the feature importances in a RandomForestClassifier for the Iris dataset. In order for this to work, we need to first create an instance of our classifier then fit it to our data.


In [1]:
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris as load_data
import matplotlib.pyplot as plt

# Import scikit-plot
import scikitplot as skplt

%pylab inline
pylab.rcParams['figure.figsize'] = (12, 12)


Populating the interactive namespace from numpy and matplotlib

In [2]:
# Load data
X, y = load_data(return_X_y=True)

# Create classifier instance and fit
classifier = RandomForestClassifier(random_state=1)
classifier.fit(X,y)


Out[2]:
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features='auto', max_leaf_nodes=None,
            min_impurity_split=1e-07, min_samples_leaf=1,
            min_samples_split=2, min_weight_fraction_leaf=0.0,
            n_estimators=10, n_jobs=1, oob_score=False, random_state=1,
            verbose=0, warm_start=False)

In [3]:
skplt.estimators.plot_feature_importances(classifier, feature_names=['petal length', 'petal width',
                                                                     'sepal length', 'sepal width'])
plt.show()