gov.sandia.cognition.learning.performance.categorization
Class DefaultConfusionMatrix<CategoryType>

java.lang.Object
  extended by gov.sandia.cognition.util.AbstractCloneableSerializable
      extended by gov.sandia.cognition.learning.performance.categorization.AbstractConfusionMatrix<CategoryType>
          extended by gov.sandia.cognition.learning.performance.categorization.DefaultConfusionMatrix<CategoryType>
Type Parameters:
CategoryType - The type of the category object over the confusion matrix.
All Implemented Interfaces:
ConfusionMatrix<CategoryType>, CloneableSerializable, Serializable, Cloneable

public class DefaultConfusionMatrix<CategoryType>
extends AbstractConfusionMatrix<CategoryType>

A default implementation of the ConfusionMatrix interface. It is backed by a two-level map storing the category object counts, making a sparse representation.

Since:
3.1
Author:
Justin Basilico
See Also:
Serialized Form

Nested Class Summary
static class DefaultConfusionMatrix.ActualPredictedPairSummarizer<CategoryType>
          A confusion matrix summarizer that summarizes actual-predicted pairs.
static class DefaultConfusionMatrix.CombineSummarizer<CategoryType>
          A confusion matrix summarizer that adds together confusion matrices.
static class DefaultConfusionMatrix.Factory<CategoryType>
          A factory for default confusion matrices.
 
Field Summary
protected  Map<CategoryType,Map<CategoryType,MutableDouble>> confusions
          The backing map of confusion matrix entries.
 
Constructor Summary
DefaultConfusionMatrix()
          Creates a new, empty DefaultConfusionMatrix.
DefaultConfusionMatrix(ConfusionMatrix<? extends CategoryType> other)
          Creates a copy of a given confusion matrix.
 
Method Summary
 void add(CategoryType target, CategoryType estimate, double value)
          Adds a given value to the matrix entry for the (actual, predicted) pair.
 void clear()
          Empties out all the data in this confusion matrix.
 DefaultConfusionMatrix<CategoryType> clone()
          This makes public the clone method on the Object class and removes the exception that it throws.
static
<CategoryType>
DefaultConfusionMatrix<CategoryType>
createFromActualPredictedPairs(Collection<? extends Pair<? extends CategoryType,? extends CategoryType>> pairs)
          Creates a new DefaultConfusionMatrix from the given actual-predicted pairs.
static
<CategoryType>
DefaultConfusionMatrix<CategoryType>
createUnweighted(Collection<? extends TargetEstimatePair<? extends CategoryType,? extends CategoryType>> pairs)
          Creates a new DefaultConfusionMatrix from the given actual-predicted pairs.
 Set<CategoryType> getActualCategories()
          Gets the set of all the actual categories.
 double getActualCount(CategoryType target)
          Gets the total number of entries for the given actual category.
 Set<CategoryType> getCategories()
          Gets the set of all categories in the confusion matrix.
 double getCount(CategoryType target, CategoryType estimate)
          Gets the entry in the matrix for the given actual and predicted categories, which is the count of the number of times the predicted category was given for the given actual category.
 Set<CategoryType> getPredictedCategories()
          Gets the set of all the predicted categories.
 Set<CategoryType> getPredictedCategories(CategoryType target)
          Gets the predicted categories for a given actual category.
 String toString()
           
 
Methods inherited from class gov.sandia.cognition.learning.performance.categorization.AbstractConfusionMatrix
add, addAll, getAccuracy, getAverageCategoryAccuracy, getAverageCategoryErrorRate, getCategoryAccuracy, getCategoryErrorRate, getErrorRate, getPredictedCount, getTotalCorrectCount, getTotalCount, getTotalIncorrectCount, isEmpty
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

confusions

protected Map<CategoryType,Map<CategoryType,MutableDouble>> confusions
The backing map of confusion matrix entries. The first key is the actual category and the second is the predicted category.

Constructor Detail

DefaultConfusionMatrix

public DefaultConfusionMatrix()
Creates a new, empty DefaultConfusionMatrix.


DefaultConfusionMatrix

public DefaultConfusionMatrix(ConfusionMatrix<? extends CategoryType> other)
Creates a copy of a given confusion matrix.

Parameters:
other - The other confusion matrix to copy.
Method Detail

clone

public DefaultConfusionMatrix<CategoryType> clone()
Description copied from class: AbstractCloneableSerializable
This makes public the clone method on the Object class and removes the exception that it throws. Its default behavior is to automatically create a clone of the exact type of object that the clone is called on and to copy all primitives but to keep all references, which means it is a shallow copy. Extensions of this class may want to override this method (but call super.clone() to implement a "smart copy". That is, to target the most common use case for creating a copy of the object. Because of the default behavior being a shallow copy, extending classes only need to handle fields that need to have a deeper copy (or those that need to be reset). Some of the methods in ObjectUtil may be helpful in implementing a custom clone method. Note: The contract of this method is that you must use super.clone() as the basis for your implementation.

Specified by:
clone in interface CloneableSerializable
Overrides:
clone in class AbstractCloneableSerializable
Returns:
A clone of this object.

add

public void add(CategoryType target,
                CategoryType estimate,
                double value)
Description copied from interface: ConfusionMatrix
Adds a given value to the matrix entry for the (actual, predicted) pair.

Parameters:
target - The actual category.
estimate - The predicted category.
value - The value to add.

getCount

public double getCount(CategoryType target,
                       CategoryType estimate)
Description copied from interface: ConfusionMatrix
Gets the entry in the matrix for the given actual and predicted categories, which is the count of the number of times the predicted category was given for the given actual category.

Parameters:
target - The actual category.
estimate - The predicted category.
Returns:
The count for (actual, predicted).

getActualCount

public double getActualCount(CategoryType target)
Description copied from interface: ConfusionMatrix
Gets the total number of entries for the given actual category.

Specified by:
getActualCount in interface ConfusionMatrix<CategoryType>
Overrides:
getActualCount in class AbstractConfusionMatrix<CategoryType>
Parameters:
target - The actual category.
Returns:
The total number of instances of the given actual category.

clear

public void clear()
Description copied from interface: ConfusionMatrix
Empties out all the data in this confusion matrix.


getCategories

public Set<CategoryType> getCategories()
Description copied from interface: ConfusionMatrix
Gets the set of all categories in the confusion matrix. It is the union of the predicted and actual categories.

Returns:
The set of all categories.

getActualCategories

public Set<CategoryType> getActualCategories()
Description copied from interface: ConfusionMatrix
Gets the set of all the actual categories.

Returns:
The set of all actual categories.

getPredictedCategories

public Set<CategoryType> getPredictedCategories()
Description copied from interface: ConfusionMatrix
Gets the set of all the predicted categories.

Returns:
The set of all predicted categories.

getPredictedCategories

public Set<CategoryType> getPredictedCategories(CategoryType target)
Description copied from interface: ConfusionMatrix
Gets the predicted categories for a given actual category. This can be either a sparse set (non-zero values) or a dense set (zeros allowed). However, the sparse set will usually yield higher performance in most use cases.

Parameters:
target - The actual category to get the set of prediction categories for.
Returns:
The set of predicted categories for the given actual categories.

toString

public String toString()
Overrides:
toString in class Object

createUnweighted

public static <CategoryType> DefaultConfusionMatrix<CategoryType> createUnweighted(Collection<? extends TargetEstimatePair<? extends CategoryType,? extends CategoryType>> pairs)
Creates a new DefaultConfusionMatrix from the given actual-predicted pairs.

Type Parameters:
CategoryType - The category type.
Parameters:
pairs - The actual-category pairs.
Returns:
A new confusion matrix populated from the given actual-category pairs.

createFromActualPredictedPairs

public static <CategoryType> DefaultConfusionMatrix<CategoryType> createFromActualPredictedPairs(Collection<? extends Pair<? extends CategoryType,? extends CategoryType>> pairs)
Creates a new DefaultConfusionMatrix from the given actual-predicted pairs.

Type Parameters:
CategoryType - The category type.
Parameters:
pairs - The actual-category pairs.
Returns:
A new confusion matrix populated from the given actual-category pairs.