MATLAB has a function for creating ROC curves and similar performance curves (such as precision-recall curves) in the Statistics and Machine Learning Toolbox: perfcurve
.
By default, the ROC curve is calculated.
The function has the following syntax:
[X, Y] = perfcurve(labels, scores, posclass)
Here, labels
is the true label for each sample, scores
is the prediction of the CNN (or any other classifier), and posclass
is the label of the class you assume to be "positive" - which appears to be 1
in your example. The outputs of the perfcurve
function are the (x, y)
coordinates of the ROC curve, so you can easily plot it using
plot(X, Y)
To make perfcurve
plot the precision-recall curve instead of the ROC curve, you have to set the optional 'XCrit'
and 'YCrit'
arguments of the function. As described in the documentation, different pre-defined criteria such as number of false positives ('fp'
), true positive rate ('tpr'
), accuracy ('accu'
) and many more, or even custom functions can be used.
By setting 'XCrit'
to 'tpr'
(Recall) and 'YCrit'
to 'prec'
(Precision), a precision-recall curve is created:
[X, Y] = perfcurve(labels, scores, posclass, 'XCrit', 'tpr', 'YCrit', 'prec');
plot(X, Y);
xlabel('Recall')
ylabel('Precision')
xlim([0, 1])
ylim([0, 1])
For example (using randomly generated data and a SVM):
