weka.classifiers.evaluation
Class ConfusionMatrix

java.lang.Object
  extended byweka.core.Matrix
      extended byweka.classifiers.evaluation.ConfusionMatrix
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class ConfusionMatrix
extends Matrix

Cells of this matrix correspond to counts of the number (or weight) of predictions for each actual value / predicted value combination.

Version:
$Revision: 1.5 $
Author:
Len Trigg (len@reeltwo.com)
See Also:
Serialized Form

Field Summary
protected  java.lang.String[] m_ClassNames
          Stores the names of the classes
 
Fields inherited from class weka.core.Matrix
m_Elements
 
Constructor Summary
ConfusionMatrix(java.lang.String[] classNames)
          Creates the confusion matrix with the given class names.
 
Method Summary
 void addPrediction(NominalPrediction pred)
          Includes a prediction in the confusion matrix.
 void addPredictions(FastVector predictions)
          Includes a whole bunch of predictions in the confusion matrix.
 java.lang.String className(int index)
          Gets the name of one of the classes.
 java.lang.Object clone()
          Creates and returns a clone of this object.
 double correct()
          Gets the number of correct classifications (that is, for which a correct prediction was made).
 double errorRate()
          Returns the estimated error rate.
 TwoClassStats getTwoClassStats(int classIndex)
          Gets the performance with respect to one of the classes as a TwoClassStats object.
 double incorrect()
          Gets the number of incorrect classifications (that is, for which an incorrect prediction was made).
 ConfusionMatrix makeWeighted(CostMatrix costs)
          Makes a copy of this ConfusionMatrix after applying the supplied CostMatrix to the cells.
private static java.lang.String num2ShortID(int num, char[] IDChars, int IDWidth)
          Method for generating indices for the confusion matrix.
 int size()
          Gets the number of classes.
 java.lang.String toString()
          Calls toString() with a default title.
 java.lang.String toString(java.lang.String title)
          Outputs the performance statistics as a classification confusion matrix.
 double total()
          Gets the number of predictions that were made (actually the sum of the weights of predictions where the class value was known).
 
Methods inherited from class weka.core.Matrix
add, addElement, eigenvalueDecomposition, getColumn, getElement, getL, getRow, getU, hypot, initialize, isSymmetric, LUDecomposition, main, multiply, numColumns, numRows, regression, regression, setColumn, setElement, setRow, solve, testEigen, transpose, write
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

m_ClassNames

protected java.lang.String[] m_ClassNames
Stores the names of the classes

Constructor Detail

ConfusionMatrix

public ConfusionMatrix(java.lang.String[] classNames)
Creates the confusion matrix with the given class names.

Parameters:
classNames - an array containing the names the classes.
Method Detail

makeWeighted

public ConfusionMatrix makeWeighted(CostMatrix costs)
                             throws java.lang.Exception
Makes a copy of this ConfusionMatrix after applying the supplied CostMatrix to the cells. The resulting ConfusionMatrix can be used to get cost-weighted statistics.

Parameters:
costs - the CostMatrix.
Returns:
a ConfusionMatrix that has had costs applied.
Throws:
java.lang.Exception - if the CostMatrix is not of the same size as this ConfusionMatrix.

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Creates and returns a clone of this object.

Overrides:
clone in class Matrix
Returns:
a clone of this instance.
Throws:
java.lang.CloneNotSupportedException - if an error occurs

size

public int size()
Gets the number of classes.

Returns:
the number of classes

className

public java.lang.String className(int index)
Gets the name of one of the classes.

Parameters:
index - the index of the class.
Returns:
the class name.

addPrediction

public void addPrediction(NominalPrediction pred)
                   throws java.lang.Exception
Includes a prediction in the confusion matrix.

Parameters:
pred - the NominalPrediction to include
Throws:
java.lang.Exception - if no valid prediction was made (i.e. unclassified).

addPredictions

public void addPredictions(FastVector predictions)
                    throws java.lang.Exception
Includes a whole bunch of predictions in the confusion matrix.

Parameters:
predictions - a FastVector containing the NominalPredictions to include
Throws:
java.lang.Exception - if no valid prediction was made (i.e. unclassified).

getTwoClassStats

public TwoClassStats getTwoClassStats(int classIndex)
Gets the performance with respect to one of the classes as a TwoClassStats object.

Parameters:
classIndex - the index of the class of interest.
Returns:
the generated TwoClassStats object.

correct

public double correct()
Gets the number of correct classifications (that is, for which a correct prediction was made). (Actually the sum of the weights of these classifications)

Returns:
the number of correct classifications

incorrect

public double incorrect()
Gets the number of incorrect classifications (that is, for which an incorrect prediction was made). (Actually the sum of the weights of these classifications)

Returns:
the number of incorrect classifications

total

public double total()
Gets the number of predictions that were made (actually the sum of the weights of predictions where the class value was known).

Returns:
the number of predictions with known class

errorRate

public double errorRate()
Returns the estimated error rate.

Returns:
the estimated error rate (between 0 and 1).

toString

public java.lang.String toString()
Calls toString() with a default title.

Overrides:
toString in class Matrix
Returns:
the confusion matrix as a string

toString

public java.lang.String toString(java.lang.String title)
Outputs the performance statistics as a classification confusion matrix. For each class value, shows the distribution of predicted class values.

Parameters:
title - the title for the confusion matrix
Returns:
the confusion matrix as a String

num2ShortID

private static java.lang.String num2ShortID(int num,
                                            char[] IDChars,
                                            int IDWidth)
Method for generating indices for the confusion matrix.

Parameters:
num - integer to format
Returns:
the formatted integer as a string