Class ConfusionMatrix


  • public class ConfusionMatrix
    extends java.lang.Object
    Provides a confusion matrix with some prediction performance measures that can be derived from a confusion matrix.
    Since:
    0.7.0
    Author:
    Arthur Zimek
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int[][] confusion
      Holds the confusion matrix.
      private java.util.ArrayList<ClassLabel> labels
      Holds the class labels.
    • Constructor Summary

      Constructors 
      Constructor Description
      ConfusionMatrix​(java.util.ArrayList<ClassLabel> labels, int[][] confusion)
      Provides a confusion matrix for the given values.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int colSum​(int classindex)
      The number of instances present in the specified column.
      int falseNegatives​(int classindex)
      The false negatives for the specified class.
      double falsePositiveRate()
      Provides the false positive rate.
      double falsePositiveRate​(int classindex)
      Provides the false positive rate for the specified class.
      int falsePositives​(int classindex)
      The false positives for the specified class.
      double positivePredictedValue()
      Provides the positive predicted value.
      double positivePredictedValue​(int classindex)
      Provides the positive predicted value for the specified class.
      int rowSum​(int classindex)
      The number of instances present in the specified row.
      java.lang.String toString()
      Provides a String representation of this confusion matrix.
      int totalInstances()
      The total number of instances covered by this confusion matrix.
      int trueNegatives​(int classindex)
      The number of true negatives of the specified class.
      double truePositiveRate()
      Provides the true positive rate.
      double truePositiveRate​(int classindex)
      Provides the true positive rate for the specified class.
      int truePositives()
      The number of correctly classified instances.
      int truePositives​(int classindex)
      The number of correctly classified instances belonging to the specified class.
      int value​(int trueClassindex, int predictedClassindex)
      The number of instances belonging to class trueClassindex and predicted as predictedClassindex.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • confusion

        private int[][] confusion
        Holds the confusion matrix. Must be a square matrix. The rows (first index) give the values which classes are classified as row, the columns (second index) give the values the class col has been classified as. Thus, confusion[predicted][real] addresses the number of instances of class real that have been assigned to the class predicted.
      • labels

        private java.util.ArrayList<ClassLabel> labels
        Holds the class labels.
    • Constructor Detail

      • ConfusionMatrix

        public ConfusionMatrix​(java.util.ArrayList<ClassLabel> labels,
                               int[][] confusion)
                        throws java.lang.IllegalArgumentException
        Provides a confusion matrix for the given values.
        Parameters:
        labels - the class labels - must conform the confusion matrix in length
        confusion - the confusion matrix. Must be a square matrix. The rows (first index) give the values which classes are classified as row, the columns (second index) give the values the class col has been classified as. Thus, confusion[predicted][real] addresses the number of instances of class real that have been assigned to the class predicted.
        Throws:
        java.lang.IllegalArgumentException - if the confusion matrix is not square or not complete or if the length of class labels does not conform the length of the confusion matrix
    • Method Detail

      • truePositiveRate

        public double truePositiveRate()
        Provides the true positive rate. Aka accuracy or sensitivity or recall: TP / (TP+FN).
        Returns:
        the true positive rate
      • falsePositiveRate

        public double falsePositiveRate​(int classindex)
        Provides the false positive rate for the specified class.
        Parameters:
        classindex - the index of the class to retrieve the false positive rate for
        Returns:
        the false positive rate for the specified class
      • falsePositiveRate

        public double falsePositiveRate()
        Provides the false positive rate. Aka false alarm rate: FP / (FP+TN).
        Returns:
        the false positive rate
      • positivePredictedValue

        public double positivePredictedValue​(int classindex)
        Provides the positive predicted value for the specified class.
        Parameters:
        classindex - the index of the class to retrieve the positive predicted value for
        Returns:
        the positive predicted value for the specified class
      • positivePredictedValue

        public double positivePredictedValue()
        Provides the positive predicted value. Aka precision or specificity: TP / (TP+FP).
        Returns:
        the positive predicted value
      • truePositives

        public int truePositives()
        The number of correctly classified instances.
        Returns:
        the number of correctly classified instances
      • truePositives

        public int truePositives​(int classindex)
        The number of correctly classified instances belonging to the specified class.
        Parameters:
        classindex - the index of the class to retrieve the correctly classified instances of
        Returns:
        the number of correctly classified instances belonging to the specified class
      • truePositiveRate

        public double truePositiveRate​(int classindex)
        Provides the true positive rate for the specified class.
        Parameters:
        classindex - the index of the class to retrieve the true positive rate for
        Returns:
        the true positive rate
      • trueNegatives

        public int trueNegatives​(int classindex)
        The number of true negatives of the specified class.
        Parameters:
        classindex - the index of the class to retrieve the true negatives for
        Returns:
        the number of true negatives of the specified class
      • falsePositives

        public int falsePositives​(int classindex)
        The false positives for the specified class.
        Parameters:
        classindex - the index of the class to retrieve the false positives for
        Returns:
        the false positives for the specified class
      • falseNegatives

        public int falseNegatives​(int classindex)
        The false negatives for the specified class.
        Parameters:
        classindex - the index of the class to retrieve the false negatives for
        Returns:
        the false negatives for the specified class
      • totalInstances

        public int totalInstances()
        The total number of instances covered by this confusion matrix.
        Returns:
        the total number of instances covered by this confusion matrix
      • rowSum

        public int rowSum​(int classindex)
        The number of instances present in the specified row. I.e., classified as class classindex.
        Parameters:
        classindex - the index of the class the resulting number of instances has been classified as
        Returns:
        the number of instances present in the specified row
      • colSum

        public int colSum​(int classindex)
        The number of instances present in the specified column. I.e., the instances of class classindex.
        Parameters:
        classindex - the index of the class theresulting number of instances belongs to
        Returns:
        the number of instances present in the specified column
      • value

        public int value​(int trueClassindex,
                         int predictedClassindex)
        The number of instances belonging to class trueClassindex and predicted as predictedClassindex.
        Parameters:
        trueClassindex - the true class index
        predictedClassindex - the predicted class index
        Returns:
        the number of instances belonging to class trueClassindex and predicted as predictedClassindex
      • toString

        public java.lang.String toString()
        Provides a String representation of this confusion matrix.
        Overrides:
        toString in class java.lang.Object
        See Also:
        Object.toString()