📄 precisionrecallevaluation.java
字号:
* @return The number of incorrect responses. */ public long incorrectResponse() { return falsePositive() + falseNegative(); } /** * Returns the total number of cases. * * @return The total number of cases. */ public long total() { return mTP + mFP + mTN + mFN; } /** * Returns the sample accuracy of the responses. The accuracy is * just the number of correct responses divided by the total number * of respones. * * @return The sample accuracy. */ public double accuracy() { return div(correctResponse(), total()); } /** * Returns the recall. The recall is the number of true positives * divided by the number of positive references. This is the * fraction of positive reference cases that were found by the * classifier. * * @return The recall value. */ public double recall() { return div(truePositive(), positiveReference()); } /** * Returns the precision. The precision is the number of true * positives divided by the number of positive respones. This is * the fraction of positive responses returned by the classifier * that were correct. * * @return The precision value. */ public double precision() { return div(truePositive(), positiveResponse()); } /** * Returns the rejection recall, or specificity, value. * The rejection recall is the percentage of negative references * that had negative respones. * * @return The rejection recall value. */ public double rejectionRecall() { return div(trueNegative(), negativeReference()); } /** * Returns the rejection prection, or selectivity, value. * The rejection precision is the percentage of negative responses * that were negative references. * * @return The rejection precision value. */ public double rejectionPrecision() { return div(trueNegative(), negativeResponse()); } /** * Returns the F<sub><sub>1</sub></sub> measure. This is the * result of applying the method {@link #fMeasure(double)} to * <code>1</code>. of the method * * * @return The F<sub><sub>1</sub></sub> measure. */ public double fMeasure() { return fMeasure(1.0); } /** * Returns the <code>F<sub><sub>β</sub></sub></code> value for * the specified <code>β</code>. * * @param beta The <code>β</code> parameter. * @return The <code>F<sub><sub>β</sub></sub></code> value. */ public double fMeasure(double beta) { return fMeasure(beta,recall(),precision()); } /** * Returns the Jaccard coefficient. * * @return The Jaccard coefficient. */ public double jaccardCoefficient() { return div(truePositive(), truePositive() + falseNegative() + falsePositive()); } /** * Returns the χ<sup>2</sup> value. * * @return The χ<sup>2</sup> value. */ public double chiSquared() { double tp = truePositive(); double tn = trueNegative(); double fp = falsePositive(); double fn = falseNegative(); double tot = total(); double diff = tp * tn - fp * fn; return tot * diff * diff / ((tp + fn) * (fp + tn) * (tp + fp) * (fn + tn)); } /** * Returns the φ<sup>2</sup> value. * * @return The φ<sup>2</sup> value. */ public double phiSquared() { return chiSquared() / (double) total(); } /** * Return the value of Yule's Q statistic. * * @return The value of Yule's Q statistic. */ public double yulesQ() { double tp = truePositive(); double tn = trueNegative(); double fp = falsePositive(); double fn = falseNegative(); return (tp*tn - fp*fn) / (tp*tn + fp*fn); } /** * Return the value of Yule's Y statistic. * * @return The value of Yule's Y statistic. */ public double yulesY() { double tp = truePositive(); double tn = trueNegative(); double fp = falsePositive(); double fn = falseNegative(); return (Math.sqrt(tp*tn) - Math.sqrt(fp*fn)) / (Math.sqrt(tp*tn) + Math.sqrt(fp*fn)); } /** * Return the Fowlkes-Mallows score. * * @return The Fowlkes-Mallows score. */ public double fowlkesMallows() { double tp = truePositive(); return tp / Math.sqrt(precision() * recall()); } /** * Returns the standard deviation of the accuracy. This is * computed as the deviation of an equivalent accuracy generated * by a binomial distribution, which is just a sequence of * Bernoulli (binary) trials. * * @return The standard deviation of the accuracy. */ public double accuracyDeviation() { // e.g. p = 0.05 for a 5% conf interval double p = accuracy(); double total = total(); double variance = p * (1.0 - p) / total; return Math.sqrt(variance); } /** * The probability that the reference and response are the same if * they are generated randomly according to the reference and * response likelihoods. * * @return The accuracy of a random classifier. */ public double randomAccuracy() { double ref = referenceLikelihood(); double resp = responseLikelihood(); return ref * resp + (1.0 - ref) * (1.0 - resp); } /** * The probability that the reference and the response are the same * if the reference and response likelihoods are both the average * of the sample reference and response likelihoods. * * @return The unbiased random accuracy. */ public double randomAccuracyUnbiased() { double avg = (referenceLikelihood() + responseLikelihood()) / 2.0; return avg * avg + (1.0 - avg) * (1.0 - avg); } /** * Returns the value of the kappa statistic. * * @return The value of the kappa statistic. */ public double kappa() { return Statistics.kappa(accuracy(),randomAccuracy()); } /** * Returns the value of the unbiased kappa statistic. * * @return The value of the unbiased kappa statistic. */ public double kappaUnbiased() { return Statistics.kappa(accuracy(),randomAccuracyUnbiased()); } /** * Returns the value of the kappa statistic adjusted for * prevalence. * * @return The value of the kappa statistic adjusted for * prevalence. */ public double kappaNoPrevalence() { return 2.0 * accuracy() - 1.0; } /** * Returns a string-based representation of this evaluation. * * @return A string-based representation of this evaluation. */ public String toString() { StringBuffer sb = new StringBuffer(2048); sb.append(" Total=" + total() + '\n'); sb.append(" True Positive=" + truePositive() + '\n'); sb.append(" False Negative=" + falseNegative() + '\n'); sb.append(" False Positive=" + falsePositive() + '\n'); sb.append(" True Negative=" + trueNegative() + '\n'); sb.append(" Positive Reference=" + positiveReference() + '\n'); sb.append(" Positive Response=" + positiveResponse() + '\n'); sb.append(" Negative Reference=" + negativeReference() + '\n'); sb.append(" Negative Response=" + negativeResponse() + '\n'); sb.append(" Accuracy=" + accuracy() + '\n'); sb.append(" Recall=" + recall() + '\n'); sb.append(" Precision=" + precision() + '\n'); sb.append(" Rejection Recall=" + rejectionRecall() + '\n'); sb.append(" Rejection Precision=" + rejectionPrecision() + '\n'); sb.append(" F(1)=" + fMeasure(1) + '\n'); sb.append(" Fowlkes-Mallows=" + fowlkesMallows() + '\n'); sb.append(" Jaccard Coefficient=" + jaccardCoefficient() + '\n'); sb.append(" Yule's Q=" + yulesQ() + '\n'); sb.append(" Yule's Y=" + yulesY() + '\n'); sb.append(" Reference Likelihood=" + referenceLikelihood() + '\n'); sb.append(" Response Likelihood=" + responseLikelihood() + '\n'); sb.append(" Random Accuracy=" + randomAccuracy() + '\n'); sb.append(" Random Accuracy Unbiased=" + randomAccuracyUnbiased() + '\n'); sb.append(" kappa=" + kappa() + '\n'); sb.append(" kappa Unbiased=" + kappaUnbiased() + '\n'); sb.append(" kappa No Prevalence=" + kappaNoPrevalence() + '\n'); sb.append(" chi Squared=" + chiSquared() + '\n'); sb.append(" phi Squared=" + phiSquared() + '\n'); sb.append(" Accuracy Deviation=" + accuracyDeviation()); return sb.toString(); } /** * Returns the F<sub><sub>β</sub></sub> measure for * a specified β, recall and precision values. * * @param beta Relative weighting of precision. * @param recall Recall value. * @param precision Precision value. * @return The F<sub><sub>β</sub></sub> measure. */ public static double fMeasure(double beta, double recall, double precision) { double betaSq = beta * beta; return (1.0 + betaSq) * recall * precision / (recall + (betaSq*precision)); } private static void validateCount(String countName, long val) { if (val < 0) { String msg = "Count must be non-negative." + " Found " + countName + "=" + val; throw new IllegalArgumentException(msg); } } static double div(double x, double y) { return x/y; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -