4

I have a boosted trees model and probabilities and classification for test data set. I am trying to plot the roc_curve for the same. But I am unable to figure out how to define thresholds/alpha for roc curve in scikit learn.

from sklearn.metrics import precision_recall_curve,roc_curve,auc, average_precision_score

fpr = dict()
tpr = dict()
roc_auc = dict()

fpr,tpr,_ = roc_curve(ytest,p_test, pos_label=1)
roc_auc = auc(fpr,tpr)

plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange', lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")

plt.savefig('ROCProb.png')
plt.show()

I looked at a similar question here : thresholds in roc_curve in scikit learn

But could not figure out. I am open to using some other library as well.

4
  • 1
    Each value in fpr and tpr is computed for a certain threshold, the values of these thresholds are returned in the third output roc_curve (the variable _ in your case) Feb 7, 2018 at 8:25
  • @sgDysregulation - Thanks! Please post that as an answer. That would help others as well!
    – Dreams
    Feb 8, 2018 at 9:47
  • @Taurn you're welcome, done. Feb 8, 2018 at 9:58
  • This does not really answer the question though, though perhaps the OP is happy - the question asks how to define the thresholds, not what roc_curve computes them to be? I'd like to know the answer to the latter! @Dreamsn?
    – jtlz2
    Nov 16, 2018 at 9:32

1 Answer 1

3

Each value in fpr and tpr is computed for a certain threshold, the values of these thresholds are returned in the third output roc_curve (the variable _ in your case)

here is an example

import numpy as np
from sklearn import metrics
y_true = np.array([1, 1, 2, 2])
y_scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = metrics.roc_curve(y_true, y_scores, pos_label=2)

tabulating the data to demo

   Threshold  FPR  TPR
0       0.80  0.0  0.5
1       0.40  0.5  0.5
2       0.35  0.5  1.0
3       0.10  1.0  1.0

The first row above shows that for threshold .8 fpr is 0 and tpr is .5 and so on

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.