Skip to content

Instantly share code, notes, and snippets.

@robert-spurrier
Created February 25, 2016 19:03
Show Gist options
  • Select an option

  • Save robert-spurrier/b3215825cb593e2f90ea to your computer and use it in GitHub Desktop.

Select an option

Save robert-spurrier/b3215825cb593e2f90ea to your computer and use it in GitHub Desktop.
Generate AUCROC in Python with scikit-learn (sklearn)
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
import random
import csv
actual = []
predictions = []
with open('scores_and_outcomes.tsv','rb') as tsvin:
tsvin = csv.reader(tsvin, delimiter='\t')
for row in tsvin:
predictions.append(float(row[0]))
actual.append(int(row[1]))
false_positive_rate, true_positive_rate, thresholds = roc_curve(actual, predictions)
roc_auc = auc(false_positive_rate, true_positive_rate)
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate, true_positive_rate, 'b',
label='AUC = %0.2f'% roc_auc)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([0.0,1.0])
plt.ylim([0.0,1.0])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment