/** * Calculate f1 score for a given class * * @param classLabel the label to calculate f1 for * @return the f1 score for the given label */ public double f1(int classLabel) { return fBeta(1.0, classLabel); }
/** * Calculate the f_beta for a given class, where f_beta is defined as:<br> * (1+beta^2) * (precision * recall) / (beta^2 * precision + recall).<br> * F1 is a special case of f_beta, with beta=1.0 * * @param beta Beta value to use * @param classLabel Class label * @return F_beta */ public double fBeta(double beta, int classLabel) { return fBeta(beta, classLabel, 0.0); }
/** * Calculate the average F1 score across all classes, using macro or micro averaging * * @param averaging Averaging method to use */ public double f1(EvaluationAveraging averaging) { return fBeta(1.0, averaging); }
private int numClassesExcluded(String metric) { int countExcluded = 0; int nClasses = confusion().getClasses().size(); for (int i = 0; i < nClasses; i++) { double d; switch (metric.toLowerCase()) { case "precision": d = precision(i, -1); break; case "recall": d = recall(i, -1); break; case "f1": case "fbeta": d = fBeta(1.0, i, -1); break; default: throw new RuntimeException("Unknown metric: " + metric); } if (d == -1) { countExcluded++; } } return countExcluded; }
int count = 0; for (int i = 0; i < nClasses; i++) { double thisFBeta = fBeta(beta, i, -1); if (thisFBeta != -1) { macroFBeta += thisFBeta;