Let say we have a SVM classifier, how do we generate ROC curve? (Like theoretically) (because we are generate TPR and FPR with each of the threshold). And how do we determine the optimal threshold for this SVM classifier?


Use the SVM classifier to classify a set of annotated examples, and "one point" on the ROC space based on one prediction of the examples can be identified. Suppose the number of examples is 200, first count the number of examples of the four cases.

\begin{array} {|r|r|r|} \hline & labeled true & labeled false \\ \hline predictedtrue &71& 28\\ \hline predicted false &57&44 \\ \hline \end{array}

Then compute TPR (True Positive Rate) and FPR (False Positive Rate). $TPR = 71/ (71+57)=0.5547$, and $FPR=28/(28+44) = 0.3889$ On the ROC space, the x-axis is FPR, and the y-axis is TPR. So point $(0.3889, 0.5547)$ is obtained.

To draw an ROC curve, just
(1) Adjust some threshold value that control the number of examples labelled true or false
For example, if concentration of certain protein above α% signifies a disease, different values of α yield different final TPR and FPR values. The threshold values can be simply determined in a way similar to grid search; label training examples with different threshold values, train classifiers with different sets of labelled examples, run the classifier on the test data, compute FPR values, and select the threshold values that cover low (close to 0) and high (close to 1) FPR values, i.e., close to 0, 0.05, 0.1, ..., 0.95, 1

(2) Generate many sets of annotated examples
(3) Run the classifier on the sets of examples
(4) Compute a (FPR, TPR) point for each of them
(5) Draw the final ROC curve

Some details can be checked in http://en.wikipedia.org/wiki/Receiver_operating_characteristic.

Besides, these two links are useful about how to determine an optimal threshold. A simple method is to take the one with maximal sum of true positive and false negative rates. Other finer criteria may include other variables involving different thresholds like financial costs, etc.

| cite | improve this answer | |
  • 7
    $\begingroup$ Thanks for your explaination, what about the optimal threshold? $\endgroup$ – RockTheStar Nov 9 '14 at 3:22
  • 1
    $\begingroup$ Sorry, I learned that optimal threshold is a special term just before. After searching, I found that chapter "3.5 Selecting an Optimal Threshold" of book "Analyzing Receiver Operating Characteristic Curves with SAS" on Google Book has some detailed explanation on selecting optimal threshold. The two widely used ways as described on it are to choose the threshold that will make the resulting binary prediction (1) as close to a perfect predictor as possible. (2) as far away from a non-informative predictor as possible $\endgroup$ – Tom Nov 9 '14 at 7:41
  • $\begingroup$ Cool, where can I find the reference? Thanks! $\endgroup$ – RockTheStar Nov 9 '14 at 20:32
  • 2
    $\begingroup$ Yes, what does "far away from a non-informative predictor" mean? Please add the reference. $\endgroup$ – Simone Nov 9 '14 at 22:47
  • 1
    $\begingroup$ Besides, I also just read from that there are many criteria to determine an optimal threshold. For example, a simple criterion is that among all thresholds, pick the one with maximal sum of true-positive and false-negative values. There are also other more sophisticated criteria. $\endgroup$ – Tom Nov 10 '14 at 2:44

Choose the point closest to the top left corner of your ROC space. Now the threshold used to generate this point should be the optimal one.

| cite | improve this answer | |
  • 4
    $\begingroup$ How to do this automatically? $\endgroup$ – ldmtwo Jan 29 '19 at 3:27

A really easy way to pick a threshold is to take the median predicted values of the positive cases for a test set. This becomes your threshold.

The threshold comes relatively close to the same threshold you would get by using the roc curve where true positive rate(tpr) and 1 - false positive rate(fpr) overlap. This tpr (cross) 1-fpr cross maximizes true positive while minimizing false negatives.

| cite | improve this answer | |
  • $\begingroup$ I see. Median predicted value. Thanks for the suggestion. $\endgroup$ – RockTheStar Feb 13 '18 at 3:38
  • 3
    $\begingroup$ Is there a source for this method? $\endgroup$ – JEquihua Jun 7 '18 at 4:48
  • 2
    $\begingroup$ This is equivalent to picking the point with TPR=0.5 in the ROC curve, which sounds really arbitrary. $\endgroup$ – Bananin Jan 9 '19 at 21:19
  • $\begingroup$ Median predicted value? And what happens if you have a class imbalance of 1000:1? $\endgroup$ – ldmtwo Jan 29 '19 at 3:26

The choice of a threshold depends on the importance of TPR and FPR classification problem. For example, if your classifier will decide which criminal suspects will receive a death sentence, false positives are very bad (innocents will be killed!). Thus you would choose a threshold that yields a low FPR while keeping a reasonable TPR (so you actually catch some true criminals). If there is no external concern about low TPR or high FPR, one option is to weight them equally by choosing the threshold that maximizes $TPR-FPR$.

| cite | improve this answer | |


The optimal cut off would be where tpr is high and fpr is low

tpr - (1-fpr) is zero or near to zero is the optimal cut off point


def plot_roc_curve(fpr, tpr):
    plt.plot(fpr, tpr, color='orange', label='ROC')
    plt.plot([0, 1], [0, 1], color='darkblue', linestyle='--')
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver Operating Characteristic (ROC) Curve')

y_true = np.array([0,0, 1, 1,1])
y_scores = np.array([0.0,0.09, .05, .75,1])

fpr, tpr, thresholds = roc_curve(y_true, y_scores)
print(roc_auc_score(y_true, y_scores))
optimal_idx = np.argmax(tpr - fpr)
optimal_threshold = thresholds[optimal_idx]
print("Threshold value is:", optimal_threshold)
plot_roc_curve(fpr, tpr)

Threshold value is: 0.75

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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