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

In this example, we'll be plotting a precision-recall curve using the Naive Bayes method from scikit-learn applied to our digits dataset. Here, we'll call an instance of our classifier, GaussianNB, and fit it to our data. Then, we'll obtain the predicted probabilities via the predict_proba() method. Finally, we can then pass our ground-truth labels and predicted probabilities to the scikitplot.metrics.plot_precision_recall method.


In [1]:
from __future__ import absolute_import
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_digits as load_data

# Import scikit-plot
import scikitplot as skplt

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


Populating the interactive namespace from numpy and matplotlib

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

# Create classifier instance then fit
nb = GaussianNB()
nb.fit(X,y)

# Get predicted probabilities
y_probas = nb.predict_proba(X)

In [3]:
skplt.metrics.plot_precision_recall_curve(y, y_probas, cmap='nipy_spectral')
plt.show()