Commit 34cce6da authored by Davis King's avatar Davis King

Added roc_point and compute_roc_curve().

parent ee0576de
......@@ -180,6 +180,55 @@ namespace dlib
return std::make_pair((low_error+high_error)/2, thresh);
}
// ----------------------------------------------------------------------------------------
struct roc_point
{
double true_positive_rate;
double false_positive_rate;
double detection_threshold;
};
inline std::vector<roc_point> compute_roc_curve (
const std::vector<double>& true_detections,
const std::vector<double>& false_detections
)
{
DLIB_CASSERT(true_detections.size() != 0);
DLIB_CASSERT(false_detections.size() != 0);
std::vector<std::pair<double,int> > temp;
temp.reserve(true_detections.size()+false_detections.size());
for (unsigned long i = 0; i < true_detections.size(); ++i)
temp.push_back(std::make_pair(true_detections[i], +1));
for (unsigned long i = 0; i < false_detections.size(); ++i)
temp.push_back(std::make_pair(false_detections[i], -1));
std::sort(temp.rbegin(), temp.rend());
std::vector<roc_point> roc_curve;
roc_curve.reserve(temp.size());
double num_false_included = 0;
double num_true_included = 0;
for (unsigned long i = 0; i < temp.size(); ++i)
{
if (temp[i].second > 0)
num_true_included++;
else
num_false_included++;
roc_point p;
p.true_positive_rate = num_true_included/true_detections.size();
p.false_positive_rate = num_false_included/false_detections.size();
p.detection_threshold = temp[i].first;
roc_curve.push_back(p);
}
return roc_curve;
}
// ----------------------------------------------------------------------------------------
}
......
......@@ -74,6 +74,41 @@ namespace dlib
- returns make_pair(ERR,T)
!*/
// ----------------------------------------------------------------------------------------
struct roc_point
{
double true_positive_rate;
double false_positive_rate;
double detection_threshold;
};
std::vector<roc_point> compute_roc_curve (
const std::vector<double>& true_detections,
const std::vector<double>& false_detections
);
/*!
requires
- true_detections.size() != 0
- false_detections.size() != 0
ensures
- This function computes the ROC curve (receiver operating characteristic)
curve of the given data. Therefore, we interpret true_detections as
containing detection scores for a bunch of true detections and
false_detections as detection scores from a bunch of false detections. A
perfect detector would always give higher scores to true detections than to
false detections, resulting in a true positive rate of 1 and a false positive
rate of 0, for some appropriate detection threshold.
- Returns an array, ROC, such that:
- ROC.size() == true_detections.size()+false_detections.size()
- for all valid i:
- If you were to accept all detections with a score >= ROC[i].detection_threshold
then you would obtain a true positive rate of ROC[i].true_positive_rate and a
false positive rate of ROC[i].false_positive_rate.
- ROC is ordered such that low detection rates come first. That is, the
curve is swept from a high detection threshold to a low threshold.
!*/
// ----------------------------------------------------------------------------------------
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment