Click or drag to resize
Accord.NET (logo)

ConfusionMatrix Class

Binary confusion matrix for binary decision problems. For multi-class decision problems, please see GeneralConfusionMatrix.
Inheritance Hierarchy
SystemObject
  Accord.Statistics.AnalysisConfusionMatrix
    Accord.Statistics.AnalysisReceiverOperatingCharacteristicPoint

Namespace:  Accord.Statistics.Analysis
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class ConfusionMatrix
Request Example View Source

The ConfusionMatrix type exposes the following members.

Constructors
  NameDescription
Public methodConfusionMatrix(Int32)
Constructs a new Confusion Matrix.
Public methodConfusionMatrix(Boolean, Boolean)
Constructs a new Confusion Matrix.
Public methodConfusionMatrix(Int32, Int32)
Constructs a new Confusion Matrix.
Public methodConfusionMatrix(Int32, Int32, Int32)
Constructs a new Confusion Matrix.
Public methodConfusionMatrix(Int32, Int32, Int32, Int32)
Constructs a new Confusion Matrix.
Public methodConfusionMatrix(Int32, Int32, Int32, Int32)
Constructs a new Confusion Matrix.
Top
Properties
  NameDescription
Public propertyAccuracy
Accuracy, or raw performance of the system
Public propertyActualNegatives
Gets the number of actual negatives
Public propertyActualPositives
Gets the number of actual positives.
Public propertyChanceAgreement
Chance agreement.
Public propertyChiSquare
Gets the Chi-Square statistic for the contingency table.
Public propertyColumnTotals
Gets the marginal sums for table columns.
Public propertyDiagonal
Gets the diagonal of the confusion matrix.
Public propertyEfficiency
Efficiency, the arithmetic mean of sensitivity and specificity
Public propertyError
Error rate, or 1 - accuracy.
Public propertyErrors
Gets the number of errors between the expected and predicted values.
Public propertyExpectedValues
Expected values, or values that could have been generated just by chance.
Public propertyFalseDiscoveryRate
False Discovery Rate, or the expected false positive rate.
Public propertyFalseNegatives
Cases incorrectly identified by the system as negatives.
Public propertyFalsePositiveRate
False Positive Rate, also known as false alarm rate.
Public propertyFalsePositives
Cases incorrectly identified by the system as positives.
Public propertyFScore
F-Score, computed as the harmonic mean of Precision and Recall.
Public propertyGeometricAgreement
Geometric agreement.
Public propertyHits
Gets the number of hits between the expected and predicted values.
Public propertyKappa
Kappa coefficient.
Public propertyMatrix
Gets the confusion matrix in count matrix form.
Public propertyMatthewsCorrelationCoefficient
Matthews Correlation Coefficient, also known as Phi coefficient
Public propertyNegativePredictiveValue
Negative Predictive Value, also known as Negative Precision
Public propertyNormalizedMutualInformation
Normalized Mutual Information.
Public propertyNumberOfClasses
Gets the number of classes in this decision problem.
Public propertyNumberOfSamples
Gets the number of observations for this matrix.
Public propertyOddsRatio
Odds-ratio.
Public propertyOverallAgreement
Overall agreement.
Public propertyOverallDiagnosticPower
Diagnostic power.
Public propertyPearson
Pearson's contingency coefficient C.
Public propertyPositivePredictiveValue
Positive Predictive Value, also known as Positive Precision
Public propertyPrecision
Precision, same as the PositivePredictiveValue.
Public propertyPredictedNegatives
Gets the number of predicted negatives.
Public propertyPredictedPositives
Gets the number of predicted positives.
Public propertyPrevalence
Prevalence of outcome occurrence.
Public propertyRecall
Recall, same as the Sensitivity.
Public propertyRowTotals
Gets the marginal sums for table rows.
Public propertySamples Obsolete.
Gets the number of observations for this matrix
Public propertySensitivity
Sensitivity, also known as True Positive Rate
Public propertySpecificity
Specificity, also known as True Negative Rate
Public propertyStandardError
Gets the standard error of the Kappa coefficient of performance.
Public propertyStandardErrorUnderNull
Gets the standard error of the Kappa under the null hypothesis that the underlying Kappa value is 0.
Public propertyTrueNegatives
Cases correctly identified by the system as negatives.
Public propertyTruePositives
Cases correctly identified by the system as positives.
Public propertyVariance
Gets the variance of the Kappa coefficient of performance.
Public propertyVarianceUnderNull
Gets the variance of the Kappa under the null hypothesis that the underlying Kappa value is 0.
Top
Methods
  NameDescription
Public methodStatic memberCombine
Combines several confusion matrices into one single matrix.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodStatic memberEstimateTInput(IClassifierTInput, Boolean, TInput, Boolean)
Estimates a ConfusionMatrix directly from a classifier, a set of inputs and its expected outputs.
Public methodStatic memberEstimateTInput(IClassifierTInput, Int32, TInput, Int32)
Estimates a ConfusionMatrix directly from a classifier, a set of inputs and its expected outputs.
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToGeneralMatrix
Converts this matrix into a GeneralConfusionMatrix.
Public methodToString
Returns a String representing this confusion matrix.
(Overrides ObjectToString.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks
Examples

The first example shows how a confusion matrix can be constructed from a vector of expected (ground-truth) values and their associated predictions (as done by a test, procedure or machine learning classifier):

// Let's say we have a decision problem involving 3 classes. In a typical
// machine learning problem, have a set of expected, ground truth values:
// 
int[] expected = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2 };

// And we have a set of values that have been predicted by a machine model:
// 
int[] predicted = { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 };

// We can get different performance measures to assess how good our model was at 
// predicting the true, expected, ground-truth labels for the decision problem:
var cm = new GeneralConfusionMatrix(classes: 3, expected: expected, predicted: predicted);

// We can obtain the proper confusion matrix using:
int[,] matrix = cm.Matrix;

// The values of this matrix should be the same as:
int[,] expectedMatrix =
{
    //              expected
    /*predicted*/ { 4, 0, 0 },
                  { 0, 4, 4 },
                  { 0, 0, 0 },
};


// We can get more information about our problem as well:
int classes = cm.NumberOfClasses; // should be 3
int samples = cm.NumberOfSamples; // should be 12

// And multiple performance measures:
double accuracy = cm.Accuracy;                      // should be 0.66666666666666663
double error = cm.Error;                            // should be 0.33333333333333337
double chanceAgreement = cm.ChanceAgreement;        // should be 0.33333333333333331
double geommetricAgreement = cm.GeometricAgreement; // should be 0 (the classifier completely missed one class)
double pearson = cm.Pearson;                        // should be 0.70710678118654757
double kappa = cm.Kappa;                            // should be 0.49999999999999994
double tau = cm.Tau;                                // should be 0.49999999999999994
double chiSquare = cm.ChiSquare;                    // should be 12

// and some of their standard errors:
double kappaStdErr = cm.StandardError;              // should be 0.15590239111558091
double kappaStdErr0 = cm.StandardErrorUnderNull;    // should be 0.16666666666666663

The second example shows how to construct a binary confusion matrix directly from a classifier and a dataset:

// Let's say we want to classify the following 2-dimensional 
// data samples into 2 possible classes, either true or false:
double[][] inputs =
{
    new double[] {  10, 42 },
    new double[] { 162, 96 },
    new double[] { 125, 20 },
    new double[] {  96,  6 },
    new double[] {   2, 73 },
    new double[] {  52, 51 },
    new double[] {  71, 49 },
};

// And those are their associated class labels
bool[] outputs =
{
    false, false, true, true, false, false, true
};

// We can create an AdaBoost algorithm as:
var learner = new AdaBoost<DecisionStump>()
{
    Learner = (p) => new ThresholdLearning(),

    // Train until:
    MaxIterations = 5,
    Tolerance = 1e-3
};

// Now, we can use the Learn method to learn a boosted classifier
Boost<DecisionStump> classifier = learner.Learn(inputs, outputs);

// And we can test its performance using (error should be 0):
ConfusionMatrix cm = ConfusionMatrix.Estimate(classifier, inputs, outputs);

double error = cm.Error;  // should be 0.0
double acc = cm.Accuracy; // should be 1.0
double kappa = cm.Kappa;  // should be 1.0

// And compute a decision for a single data point using:
bool y = classifier.Decide(inputs[0]); // result should false
See Also