Class ConfusionMetrics

java.lang.Object
org.tribuo.classification.evaluation.ConfusionMetrics

public final class ConfusionMetrics extends Object
Static functions for computing classification metrics based on a ConfusionMatrix.
  • Method Details

    • accuracy

      public static <T extends Classifiable<T>> double accuracy(MetricTarget<T> target, ConfusionMatrix<T> cm)
      Calculates the accuracy given this confusion matrix.
      Type Parameters:
      T - The type parameter
      Parameters:
      target - The metric target
      cm - The confusion matrix
      Returns:
      The accuracy
    • accuracy

      public static <T extends Classifiable<T>> double accuracy(T label, ConfusionMatrix<T> cm)
      Calculates a per label accuracy given this confusion matrix.
      Type Parameters:
      T - The type parameter
      Parameters:
      label - The label
      cm - The confusion matrix
      Returns:
      The accuracy
    • accuracy

      public static <T extends Classifiable<T>> double accuracy(EvaluationMetric.Average average, ConfusionMatrix<T> cm)
      Calculates the accuracy using the specified average type and confusion matrix.
      Type Parameters:
      T - the type parameter
      Parameters:
      average - the average
      cm - The confusion matrix
      Returns:
      The accuracy
    • balancedErrorRate

      public static <T extends Classifiable<T>> double balancedErrorRate(ConfusionMatrix<T> cm)
      Calculates the balanced error rate, i.e., the mean of the recalls.
      Type Parameters:
      T - the type parameter
      Parameters:
      cm - The confusion matrix
      Returns:
      the balanced error rate.
    • tp

      public static <T extends Classifiable<T>> double tp(MetricTarget<T> tgt, ConfusionMatrix<T> cm)
      Returns the number of true positives, possibly averaged depending on the metric target.
      Type Parameters:
      T - the type parameter
      Parameters:
      tgt - The metric target
      cm - The confusion matrix
      Returns:
      the true positives.
    • fp

      public static <T extends Classifiable<T>> double fp(MetricTarget<T> tgt, ConfusionMatrix<T> cm)
      Returns the number of false positives, possibly averaged depending on the metric target.
      Type Parameters:
      T - the type parameter
      Parameters:
      tgt - The metric target
      cm - The confusion matrix
      Returns:
      the false positives.
    • tn

      public static <T extends Classifiable<T>> double tn(MetricTarget<T> tgt, ConfusionMatrix<T> cm)
      Returns the number of true negatives, possibly averaged depending on the metric target.
      Type Parameters:
      T - the type parameter
      Parameters:
      tgt - The metric target
      cm - The confusion matrix
      Returns:
      the true negatives.
    • fn

      public static <T extends Classifiable<T>> double fn(MetricTarget<T> tgt, ConfusionMatrix<T> cm)
      Returns the number of false negatives, possibly averaged depending on the metric target.
      Type Parameters:
      T - the type parameter
      Parameters:
      tgt - The metric target
      cm - The confusion matrix
      Returns:
      the false negatives.
    • precision

      public static <T extends Classifiable<T>> double precision(MetricTarget<T> tgt, ConfusionMatrix<T> cm)
      Calculates the precision for this metric target.
      Type Parameters:
      T - the type parameter
      Parameters:
      tgt - The metric target
      cm - The confusion matrix
      Returns:
      the precision.
    • precision

      public static double precision(double tp, double fp, double tn, double fn)
      Calculates the precision based upon the supplied statistics.
      Parameters:
      tp - the true positives
      fp - the false positives
      tn - the true negatives
      fn - the false negatives
      Returns:
      The recall.
    • recall

      public static <T extends Classifiable<T>> double recall(MetricTarget<T> tgt, ConfusionMatrix<T> cm)
      Calculates the recall for this metric target.
      Type Parameters:
      T - the type parameter
      Parameters:
      tgt - The metric target
      cm - The confusion matrix
      Returns:
      The recall.
    • recall

      public static double recall(double tp, double fp, double tn, double fn)
      Calculates the recall based upon the supplied statistics.
      Parameters:
      tp - the true positives
      fp - the false positives
      tn - the true negatives
      fn - the false negatives
      Returns:
      The recall.
    • f1

      public static <T extends Classifiable<T>> double f1(MetricTarget<T> tgt, ConfusionMatrix<T> cm)
      Computes the F_1 score.
      Type Parameters:
      T - the type parameter
      Parameters:
      tgt - the metric target.
      cm - the confusion matrix.
      Returns:
      the F_1 score.
    • f1

      public static double f1(double tp, double fp, double tn, double fn)
      Computes the F_1 score.
      Parameters:
      tp - the true positives
      fp - the false positives
      tn - the true negatives
      fn - the false negatives
      Returns:
      the F_1 score.
    • fscore

      public static double fscore(double beta, double tp, double fp, double tn, double fn)
      Computes the Fscore.
      Parameters:
      beta - the beta.
      tp - the true positives.
      fp - the false positives.
      tn - the true negatives.
      fn - the false negatives.
      Returns:
      the F_beta score.
    • fscore

      public static <T extends Classifiable<T>> double fscore(MetricTarget<T> tgt, ConfusionMatrix<T> cm, double beta)
      Computes the Fscore.
      Type Parameters:
      T - the type parameter
      Parameters:
      tgt - The metric target
      cm - The confusion matrix
      beta - the beta
      Returns:
      The F_beta score.