Skip to content

Instantly share code, notes, and snippets.

@cwidmer
Last active December 30, 2015 09:59
Show Gist options
  • Save cwidmer/7813365 to your computer and use it in GitHub Desktop.
Save cwidmer/7813365 to your computer and use it in GitHub Desktop.
code snippet to calculate the common performance measures, area under the precision-recall curve and area under the receiver operator characteristic curve
import pylab
def plot_roc(y, out, label="", out_fn=None):
"""
show or save ROC curve
"""
pylab.figure()
plot_roc_noshow(y, out, label=label)
if not out_fn is None:
pylab.savefig(auc_fn)
else:
pylab.show()
def plot_roc_noshow(y, out, label=""):
"""
create area under the receiver operator characteristic curve plot
"""
# plot auc
from sklearn.metrics import roc_curve, auc
fpr, tpr, thresholds = roc_curve(y, out)
roc_auc = auc(fpr, tpr)
pylab.plot(fpr, tpr, label='%s (area = %0.2f)' % (label, roc_auc))
pylab.plot([0, 1], [0, 1], 'k--')
pylab.xlim([0.0, 1.0])
pylab.ylim([0.0, 1.0])
pylab.xlabel('False Positive Rate')
pylab.ylabel('True Positive Rate')
pylab.title('Receiver operating characteristic example')
pylab.legend(loc="lower right")
def plot_prc(y, out, label="", out_fn=None):
"""
plot precision recall plot
"""
pylab.figure()
plot_prc_noshow(y, out, label=label)
if not out_fn is None:
pylab.savefig(out_fn)
else:
pylab.show()
def plot_prc_noshow(y, out, label=""):
"""
"""
# Compute Precision-Recall and plot curve
from sklearn.metrics import precision_recall_curve, auc
precision, recall, thresholds = precision_recall_curve(y, out)
area = auc(recall, precision)
pylab.plot(recall, precision, label='%s (area = %0.2f)' % (label, area))
pylab.xlabel('Recall')
pylab.ylabel('Precision')
pylab.ylim([0.0, 1.05])
pylab.xlim([0.0, 1.0])
pylab.legend(loc="upper right")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment