orderings = ResultUtil.getOrderingResults(result);
// Outlier results are the main use case.
for (OutlierResult o : oresults) {
DBIDs sorted = o.getOrdering().order(o.getOrdering().getDBIDs());
XYCurve curve = computePrecisionResult(o.getScores().size(), positiveids, sorted.iter(), o.getScores());
db.getHierarchy().add(o, curve);
// Process them only once.
orderings.remove(o.getOrdering());
}
// FIXME: find appropriate place to add the derived result
// otherwise apply an ordering to the database IDs.
for (OrderingResult or : orderings) {
DBIDs sorted = or.order(or.getDBIDs());
XYCurve curve = computePrecisionResult(or.getDBIDs().size(), positiveids, sorted.iter(), null);
db.getHierarchy().add(or, curve);
}
}
private XYCurve computePrecisionResult(int size, SetDBIDs ids, DBIDIter iter, DoubleRelation scores) {
final int postot = ids.size();
int poscnt = 0, total = 0;
XYCurve curve = new PRCurve(postot + 2, postot);
double prevscore = Double.NaN;
for (; iter.valid(); iter.advance()) {
// Previous precision rate - y axis
final double curprec = ((double) poscnt) / total;
// Previous recall rate - x axis
final double curreca = ((double) poscnt) / postot;
// Analyze next point
// positive or negative match?
if (ids.contains(iter)) {
poscnt += 1;
}
total += 1;
// First iteration ends here
if (total == 1) {
continue;
}
// defer calculation for ties
if (scores != null) {
double curscore = scores.doubleValue(iter);
if (Double.compare(prevscore, curscore) == 0) {
continue;
}
prevscore = curscore;
}
// Add a new point (for the previous entry - because of tie handling!)
curve.addAndSimplify(curreca, curprec);
}
// End curve - always at all positives found.
curve.addAndSimplify(1.0, postot / total);
return curve;
}
/**
* P/R Curve
*
* @author Erich Schubert
*/
public static class PRCurve extends XYCurve {
/**
* AUC value for PR curve
*/
public static final String PRAUC_LABEL = "PR-AUC";
/**
* Area under curve
*/
double auc = Double.NaN;
/**
* Number of positive observations
*/
int positive;
/**
* Constructor.
*
* @param size Size estimation
* @param positive Number of positive elements (for AUC correction)
*/
public PRCurve(int size, int positive) {
super("Recall", "Precision", size);
this.positive = positive;
}
@Override
public String getLongName() {
return "Precision-Recall-Curve";
}
@Override
public String getShortName() {
return "pr-curve";
}
/**
* Get AUC value
*
* @return AUC value
*/
public double getAUC() {
if (Double.isNaN(auc)) {
double max = 1 - 1. / positive;
auc = areaUnderCurve(this) / max;
}
return auc;
}
@Override
public void writeToText(TextWriterStream out, String label) {
out.commentPrintLn(PRAUC_LABEL + ": " + getAUC());
out.flush();
super.writeToText(out, label);
}
}
/**
* Parameterization class.
*
* @author Erich Schubert
*
* @apiviz.exclude
*/
public static class Parameterizer extends AbstractParameterizer {
/**
* The pattern to identify positive classes.
*
*
* Key: {@code -precision.positive}
*
*/
public static final OptionID POSITIVE_CLASS_NAME_ID = new OptionID("precision.positive", "Class label for the 'positive' class.");
protected Pattern positiveClassName = null;
@Override
protected void makeOptions(Parameterization config) {
super.makeOptions(config);
PatternParameter positiveClassNameP = new PatternParameter(POSITIVE_CLASS_NAME_ID);
if (config.grab(positiveClassNameP)) {
positiveClassName = positiveClassNameP.getValue();
}
}
@Override
protected OutlierPrecisionRecallCurve makeInstance() {
return new OutlierPrecisionRecallCurve(positiveClassName);
}
}
}