public final class ConfusionMatrix<T extends Serializable> extends Object
This is a data structure for dealing with label-pair-relations. For
statistical information on a per-label-base there are methods which return
instance(s) of TableOfConfusion. That class has methods to calculate
values like Precision, Recall, [...].
Look at http://en.wikipedia.org/wiki/Confusion_matrix for detailed descriptions.
Although there is a method for adding a label or a list of labels to the
existing labels, it is highly recommended to use it as rare as possible, as
it requires that (internally) the array storing the label-combination-counts
has to be copied to a new array which then replaces the old array. Instead,
use the constructor with a list of labels as argument, where that list should
contain as much labels as possible so that the need for adding labels is at a
minimum frequency. However, if the labels are not known in advance, using
addLabels(java.util.List) is preferable to
#addLabel(java.lang.Object) as there are fewer array copy operations
needed with the first mentioned method.
TableOfConfusion| Modifier and Type | Field and Description |
|---|---|
private long[][] |
confusionMatrix
Array which stores the counts of classification instances (pairs of true
and predicted labels)
|
private List<T> |
labels
The list of all labels.
|
| Constructor and Description |
|---|
ConfusionMatrix()
Creates a new ConfusionMatrix instance with an empty list of labels.
|
ConfusionMatrix(List<T> labels)
Creates a new ConfusionMatrix instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(T truth,
T prediction)
Adds a classification instance (true and predicted label) to this
ConfusionMatrix. |
void |
addLabel(T additionalLabel)
Adds the provided label to the list of labels.
|
void |
addLabels(List<T> additionalLabels)
Adds the provided list of labels to the internal list of labels.
|
double |
calculateAccuracy()
Calculates and returns the overall accuracy for this confusion matrix in
range [0,1].
|
private long |
getFalseNegativeCount(T label)
Returns the number of "false negative" instances for the provided label.
|
private long |
getFalsePositiveCount(T label)
Returns the number of "false positive" instances for the provided label.
|
List<T> |
getLabels()
Returns the list of labels this
ConfusionMatrix maintains
counters for. |
TableOfConfusion |
getTableOfConfusion(T label)
Constructs and returns the
TableOfConfusion for the provided
label. |
Map<T,TableOfConfusion> |
getTablesOfConfusion()
Returns a map which contains a
TableOfConfusion per label. |
private long |
getTrueNegativeCount(T label)
Returns the number of "true negative" instances for the provided label.
|
private long |
getTruePositiveCount(T label)
Returns the number of "true positive" instances for the provided label.
|
long |
getWeightForLabel(T label)
Returns the class size as a weight for per-class-calculations.
|
String |
toHtml() |
String |
toString() |
private List<T extends Serializable> labels
confusionMatrix.private long[][] confusionMatrix
Array which stores the counts of classification instances (pairs of true and predicted labels)
The first dimension represents the true labels while the second dimension represents the predicted labels.
public ConfusionMatrix()
public void addLabel(T additionalLabel)
Adds the provided label to the list of labels. Afterwards it is necessary
to create a new array for the internal counters so the usage of this
method is expensive. Try to add as much labels as possible at a time by
using addLabels(java.util.List) or, which is even better, at
object creation time.
Duplicates are ignored.
additionalLabel - The label to add to the internal list of labelspublic void addLabels(List<T> additionalLabels)
Adds the provided list of labels to the internal list of labels. Afterwards it is necessary to create a new array for the internal counters so the usage of this method is expensive. Try to add as much labels as possible at object creation time.
Duplicates are ignored.
additionalLabels - The labels to add to the internal list of labelspublic List<T> getLabels()
ConfusionMatrix maintains
counters for.ConfusionMatrix maintains
counters for.public void add(T truth, T prediction)
Adds a classification instance (true and predicted label) to this
ConfusionMatrix.
Each label which didn't exist previously will be added within this method automatically (which is expensive and to be avoided).
truth - The true labelprediction - The predicted labelpublic Map<T,TableOfConfusion> getTablesOfConfusion()
TableOfConfusion per label.TableOfConfusion instances#getTableOfConfusion(java.lang.Object)public TableOfConfusion getTableOfConfusion(T label)
TableOfConfusion for the provided
label.label - The label to construct a TableOfConfusion forTableOfConfusion instance for the provided labelTableOfConfusionpublic double calculateAccuracy()
public long getWeightForLabel(T label)
label - The label to get the weight forprivate long getTruePositiveCount(T label)
label - The label to return the counted number of "true positive"
instances forprivate long getTrueNegativeCount(T label)
label - The label to return the counted number of "true negative"
instances forprivate long getFalsePositiveCount(T label)
label - The label to return the counted number of "false positive"
instances forprivate long getFalseNegativeCount(T label)
label - The label to return the counted number of "false negative"
instances forpublic String toHtml()
Copyright © 2018. All rights reserved.