After a classification algorithm such as
TreeBagger has trained on data, you may want to
examine the performance of the algorithm on a specific test dataset. One common way of doing
this would be to compute a gross measure of performance such as quadratic loss or accuracy,
averaged over the entire test dataset.
You may want to inspect the classifier performance more closely, for example, by plotting a Receiver Operating Characteristic (ROC) curve. By definition, a ROC curve [1,2] shows true positive rate versus false positive rate (equivalently, sensitivity versus 1–specificity) for different thresholds of the classifier output. You can use it, for example, to find the threshold that maximizes the classification accuracy or to assess, in more broad terms, how the classifier performs in the regions of high sensitivity and high specificity.
perfcurve computes measures
for a plot of classifier performance. You can use this utility to
evaluate classifier performance on test data after you train the classifier.
Various measures such as mean squared error, classification error,
or exponential loss can summarize the predictive power of a classifier
in a single number. However, a performance curve offers more information
as it lets you explore the classifier performance across a range of
thresholds on its output.
You can use
perfcurve with any classifier
or, more broadly, with any method that returns a numeric score for
an instance of input data. By convention adopted here,
A high score returned by a classifier for any given instance signifies that the instance is likely from the positive class.
A low score signifies that the instance is likely from the negative classes.
For some classifiers, you can interpret the score as
the posterior probability of observing an instance of the positive
class at point
X. An example of such a score is
the fraction of positive observations in a leaf of a decision tree.
In this case, scores fall into the range from 0 to 1 and scores from
positive and negative classes add up to unity. Other methods can return
scores ranging between minus and plus infinity, without any obvious
mapping from the score to the posterior class probability.
perfcurve does not impose any requirements
on the input score range. Because of this lack of normalization, you
perfcurve to process scores returned by
any classification, regression, or fit method.
not make any assumptions about the nature of input scores or relationships
between the scores for different classes. As an example, consider
a problem with three classes,
C, and assume that the scores returned by some
classifier for two instances are as follows:
If you want to compute a performance curve for separation of
you need to address the ambiguity in selecting
You could opt to use the score ratio,
or score difference,
s(A)-s(B); this choice could
depend on the nature of these scores and their normalization.
takes one score per instance. If you only supply scores for class
not distinguish between observations 1 and 2. The performance curve
in this case may not be optimal.
perfcurve is intended for
use with classifiers that return scores, not those that return only
predicted classes. As a counter-example, consider a decision tree
that returns only hard classification labels, 0 or 1, for data with
two classes. In this case, the performance curve reduces to a single
point because classified instances can be split into positive and
negative categories in one way only.
perfcurve takes true class labels
for some data and scores assigned by a classifier to these data. By
default, this utility computes a Receiver Operating Characteristic
(ROC) curve and returns values of 1–specificity, or false positive
X and sensitivity, or true positive rate,
Y. You can choose other criteria for
selecting one out of several provided criteria or specifying an arbitrary
criterion through an anonymous function. You can display the computed
performance curve using
perfcurve can compute values for various
criteria to plot either on the x- or the y-axis.
All such criteria are described by a 2-by-2 confusion matrix, a 2-by-2
cost matrix, and a 2-by-1 vector of scales applied to class counts.
is defined as
P stands for "positive".
N stands for "negative".
T stands for "true".
F stands for "false".
For example, the first row of the confusion matrix
defines how the classifier identifies instances of the positive class:
the count of correctly identified positive instances and
the count of positive instances misidentified as negative.
The cost matrix defines the cost of misclassification for each category:
the cost of assigning an instance of class
perfcurve allows you to specify
nonzero costs for correct classification as well.
The two scales include prior information about class probabilities.
these scales by taking
normalizing the sum
scale(P)+scale(N) to 1.
the total instance counts in the positive and negative class, respectively.
The function then applies the scales as multiplicative factors to
the counts from the corresponding class:
counts from the positive class by
counts from the negative class by
for example, computation of positive predictive value,
TP counts come from the
positive class and
FP counts come from the negative
class. Therefore, you need to scale
and the modified formula for
PPV with prior probabilities
taken into account is now:
If all scores in the
data are above a certain threshold,
all instances as
'positive'. This means that
the total number of instances in the positive class and
the total number of instances in the negative class. In this case,
simply given by the prior:
returns two vectors,
of performance measures. Each measure is some function of
scale values. You can request specific measures
by name or provide a function handle to compute a custom measure.
The function you provide should take
scale as its three inputs and return a vector
of output values.
The criterion for
X must be a monotone function
of the positive classification count, or equivalently, threshold for
the supplied scores. If
perfcurve cannot perform
a one-to-one mapping between values of the
and score thresholds, it exits with an error message.
perfcurve computes values of
Y criteria for all
possible score thresholds. Alternatively, it can compute a reduced
number of specific
X values supplied as an input
argument. In either case, for
M requested values,
Y. The first value
out of these
M+1 values is special.
it by setting the
TP instance count to zero and
TN to the total count in the negative class.
This value corresponds to the
'reject all' threshold.
On a standard ROC curve, this translates into an extra point placed
If there are
NaN values among input scores,
process them in either of two ways:
It can discard rows with
It can add them to false classification counts in the respective class.
That is, for any threshold, instances with
from the positive class are counted as false negative (
and instances with
NaN scores from the negative
class are counted as false positive (
FP). In this
case, the first value of
computed by setting
TP to zero and setting
the total count minus the
NaN count in the negative
class. For illustration, consider an example with two rows in the
positive and two rows in the negative class, each pair having a
If you discard rows with
then as the score cutoff varies,
performance measures as in the following table. For example, a cutoff
of 0.5 corresponds to the middle row where rows 1 and 3 are classified
correctly, and rows 2 and 4 are omitted.
If you add rows with
to the false category in their respective classes,
performance measures as in the following table. For example, a cutoff
of 0.5 corresponds to the middle row where now rows 2 and 4 are counted
as incorrectly classified. Notice that only the
differ between these two tables.
For data with three or more
perfcurve takes one positive class and
a list of negative classes for input. The function computes the
using counts in the positive class to estimate
and using counts in all negative classes to estimate
Y values for each negative class
separately and, in addition to
Y, return a matrix
the number of elements in
the number of negative classes. You can use this functionality to
monitor components of the negative class contribution. For example,
you can plot
TP counts on the
FP counts on the
In this case, the returned matrix shows how the
is split across negative classes.
You can also use
estimate confidence intervals.
confidence bounds using either cross-validation or bootstrap. If you
supply cell arrays for
cross-validation and treats elements in the cell arrays as cross-validation
folds. If you set input parameter
NBoot to a positive
replicas to compute pointwise confidence bounds.
perfcurve estimates the confidence bounds using one of two methods:
Vertical averaging (VA) — estimate confidence bounds on
T at fixed values of
X. Use the
XVals input parameter to use this method for computing confidence bounds.
Threshold averaging (TA) — estimate confidence bounds for
Y at fixed thresholds for the positive class score. Use the
TVals input parameter to use this method for computing confidence bounds.
To use observation weights instead of observation counts, you can use the
'Weights' parameter in your call to
perfcurve. When you
use this parameter, to compute
T or to compute confidence bounds by cross-validation,
perfcurve uses your supplied observation weights instead of observation
counts. To compute confidence bounds by bootstrap,
N out of N with replacement using your weights as
multinomial sampling probabilities.