/** * Returns the number of times the given label * has actually occurred * * @param clazz the label * @return the number of times the label * actually occurred */ public int classCount(Integer clazz) { return confusion().getActualTotal(clazz); }
/** * Adds to the confusion matrix * * @param real the actual guess * @param guess the system guess */ public void addToConfusion(Integer real, Integer guess) { confusion().add(real, guess); }
/** * False negatives: correctly rejected * * @return the total false negatives so far */ public Map<Integer, Integer> falseNegatives() { return convertToMap(falseNegatives, confusion().getClasses().size()); }
/** * False positive: wrong guess * * @return the count of the false positives */ public Map<Integer, Integer> falsePositives() { return convertToMap(falsePositives, confusion().getClasses().size()); }
/** * True positives: correctly rejected * * @return the total true positives so far */ public Map<Integer, Integer> truePositives() { return convertToMap(truePositives, confusion().getClasses().size()); }
/** * True negatives: correctly rejected * * @return the total true negatives so far */ public Map<Integer, Integer> trueNegatives() { return convertToMap(trueNegatives, confusion().getClasses().size()); }
/** * Accuracy: * (TP + TN) / (P + N) * * @return the accuracy of the guesses so far */ public double accuracy() { //Accuracy: sum the counts on the diagonal of the confusion matrix, divide by total int nClasses = confusion().getClasses().size(); int countCorrect = 0; for (int i = 0; i < nClasses; i++) { countCorrect += confusion().getCount(i, i); } return countCorrect / (double) getNumRowCounter(); }
/** * Return the number of correct predictions according to top N value. For top N = 1 (default) this is equivalent to * the number of correct predictions * @return Number of correct top N predictions */ public int getTopNCorrectCount() { if (confusion == null) confusion = new ConfusionMatrix<>(); if (topN <= 1) { int nClasses = confusion().getClasses().size(); int countCorrect = 0; for (int i = 0; i < nClasses; i++) { countCorrect += confusion().getCount(i, i); } return countCorrect; } return topNCorrectCount; }
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; }
/** * Calculate the average false negative rate for all classes - can specify whether macro or micro averaging should be used * * @param averaging Averaging method - macro or micro * @return Average false negative rate */ public double falseNegativeRate(EvaluationAveraging averaging) { int nClasses = confusion().getClasses().size(); if (averaging == EvaluationAveraging.Macro) { double macroFNR = 0.0; for (int i = 0; i < nClasses; i++) { macroFNR += falseNegativeRate(i); } macroFNR /= nClasses; return macroFNR; } else if (averaging == EvaluationAveraging.Micro) { long fnCount = 0; long tnCount = 0; for (int i = 0; i < nClasses; i++) { fnCount += falseNegatives.getCount(i); tnCount += trueNegatives.getCount(i); } return EvaluationUtils.falseNegativeRate(fnCount, tnCount, DEFAULT_EDGE_VALUE); } else { throw new UnsupportedOperationException("Unknown averaging approach: " + averaging); } }
StringBuilder builder = new StringBuilder().append("\n"); StringBuilder warnings = new StringBuilder(); List<Integer> classes = confusion().getClasses(); int count = confusion().getCount(clazz, clazz2); if (count != 0) { predicted = resolveLabelForClass(clazz2); builder.append(warnings); int nClasses = confusion().getClasses().size(); DecimalFormat df = new DecimalFormat("0.0000"); double acc = accuracy();
/** * Calculate the average false positive rate across all classes. Can specify whether macro or micro averaging should be used * * @param averaging Averaging method - macro or micro * @return Average false positive rate */ public double falsePositiveRate(EvaluationAveraging averaging) { int nClasses = confusion().getClasses().size(); if (averaging == EvaluationAveraging.Macro) { double macroFPR = 0.0; for (int i = 0; i < nClasses; i++) { macroFPR += falsePositiveRate(i); } macroFPR /= nClasses; return macroFPR; } else if (averaging == EvaluationAveraging.Micro) { long fpCount = 0; long tnCount = 0; for (int i = 0; i < nClasses; i++) { fpCount += falsePositives.getCount(i); tnCount += trueNegatives.getCount(i); } return EvaluationUtils.falsePositiveRate(fpCount, tnCount, DEFAULT_EDGE_VALUE); } else { throw new UnsupportedOperationException("Unknown averaging approach: " + averaging); } }
} else { if (other.confusion != null) confusion().add(other.confusion);
int nClasses = confusion().getClasses().size(); if (averaging == EvaluationAveraging.Macro) { double macroPrecision = 0.0;
int nClasses = confusion().getClasses().size(); if (averaging == EvaluationAveraging.Macro) { double macroRecall = 0.0;
int nClasses = confusion().getClasses().size(); if (averaging == EvaluationAveraging.Macro) { double macroMatthewsCorrelation = 0.0;
int nClasses = confusion().getClasses().size(); if (averaging == EvaluationAveraging.Macro) { double macroGMeasure = 0.0;
int nClasses = confusion().getClasses().size();