Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the optimal threshold from ROC curve in Python? [duplicate]

I want to get the optimal threshold from ROC curve using Python. I know how to do it in R with the coords function but I can't seem to find a similar one in Python.

This is the way I'm displaying the ROC curve

def plot_roc_curve(fpr,tpr, thresholds):
    plt.figure()
    plt.plot(fpr, tpr, color='darkorange', label='ROC curve (area = %0.2f)' % metrics.auc(fpr, tpr))
    plt.plot([0, 1], [0, 1], color='navy', linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.legend(loc="lower right")

    # create the axis of thresholds (scores)
    ax2 = plt.gca().twinx()
    ax2.plot(fpr, thresholds, markeredgecolor='r',linestyle='dashed', color='r')
    ax2.set_ylabel('Threshold',color='r')
    ax2.set_ylim([thresholds[-1],thresholds[0]])
    ax2.set_xlim([fpr[0],fpr[-1]])

    plt.savefig('roc_and_threshold.png')
    plt.close()

enter image description here

like image 240
morty Avatar asked May 19 '19 00:05

morty


1 Answers

def Find_Optimal_Cutoff(target, predicted):
    fpr, tpr, threshold = roc_curve(target, predicted)
    i = np.arange(len(tpr)) 
    roc = pd.DataFrame({'tf' : pd.Series(tpr-(1-fpr), index=i), 'threshold' : pd.Series(threshold, index=i)})
    roc_t = roc.ix[(roc.tf-0).abs().argsort()[:1]]

    return list(roc_t['threshold']) 

threshold = Find_Optimal_Cutoff(target_column,predicted_column)

Source: Roc curve and cut off point. Python

like image 85
secretive Avatar answered Oct 04 '22 00:10

secretive