Click or drag to resize
Accord.NET (logo)

GeneralConfusionMatrix Class

General confusion matrix for multi-class decision problems. For binary problems, please see ConfusionMatrix.
Inheritance Hierarchy
SystemObject
  Accord.Statistics.AnalysisGeneralConfusionMatrix
    Accord.Statistics.AnalysisWeightedConfusionMatrix

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

The GeneralConfusionMatrix type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyAccuracy
Accuracy. This is the same value as OverallAgreement.
Public propertyChanceAgreement
Chance agreement.
Public propertyChiSquare
Gets the Chi-Square statistic for the contingency table.
Public propertyClasses Obsolete.
Obsolete. Please use NumberOfClasses instead.
Public propertyColumnErrors
Gets the col errors.
Public propertyColumnProportions
Gets the column marginals (proportions).
Public propertyColumnTotals
Gets the column totals.
Public propertyCramer
Cramer's V association measure.
Public propertyDiagonal
Gets the diagonal of the confusion matrix.
Public propertyError
Error. This is the same value as 1.0 - OverallAgreement.
Public propertyExpectedValues
Expected values, or values that could have been generated just by chance.
Public propertyGeometricAgreement
Geometric agreement.
Public propertyKappa
Gets the Kappa coefficient of performance.
Public propertyMatrix
Gets the confusion matrix, in which each element e_ij represents the number of elements from class i classified as belonging to class j.
Public propertyMax
Gets the maximum number of correct matches (the maximum over the diagonal)
Public propertyMin
Gets the minimum number of correct matches (the minimum over the diagonal)
Public propertyNumberOfClasses
Gets the number of classes.
Public propertyNumberOfSamples
Gets the number of samples.
Public propertyOverallAgreement
Overall agreement.
Public propertyPearson
Pearson's contingency coefficient C.
Public propertyPerClassMatrices
Gets binary confusion matrices for each class in the multi-class classification problem. You can use this property to obtain recall, precision and other metrics for each of the classes.
Public propertyPhi
Phi coefficient.
Public propertyPrecision
Gets the row precision.
Public propertyProportionMatrix
Gets the confusion matrix in terms of cell percentages.
Public propertyRecall
Gets the column recall.
Public propertyRowErrors
Gets the row errors.
Public propertyRowProportions
Gets the row marginals (proportions).
Public propertyRowTotals
Gets the row totals.
Public propertySakoda
Sakoda's contingency coefficient V.
Public propertySamples Obsolete.
Obsolete. Please use NumberOfSamples instead.
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 propertyTau
Gets the Tau coefficient of performance.
Public propertyTitleAboveColumns
Gets or sets the title that ought be displayed on top of the columns of this GeneralConfusionMatrix. Default is "Expected (Ground-truth)".
Public propertyTitleOnTheLeftOfRows
Gets or sets the title that ought be displayed on left side of this GeneralConfusionMatrix. Default is "Actual (Prediction)".
Public propertyTschuprow
Tschuprow's T association measure.
Public propertyVariance
Gets the variance of the Kappa coefficient of performance.
Public propertyVarianceDeltaMethod
Gets the variance of the Kappa coefficient of performance using Congalton's delta method.
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 GeneralConfusionMatrix directly from a classifier, a set of inputs and its expected outputs.
Public methodStatic memberEstimateTInput(IClassifierTInput, Int32, TInput, Int32)
Estimates a GeneralConfusionMatrix 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 methodToString
Returns a string that represents the current object.
(Inherited from Object.)
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 general confusion matrix directly from a integer matrix with the class assignments:

// Let's say we have the following matrix
int[,] matrix =
{
    { 29,  6,  5 },
    {  8, 20,  7 },
    {  1,  2, 22 },
};

// Create a new multi-class Confusion Matrix
var cm = new GeneralConfusionMatrix(matrix);

// Now we can use it to obtain info such as
int classes = cm.NumberOfClasses; // should be 3
int samples = cm.NumberOfSamples; // should be 100

double acc = cm.Accuracy;          // should be 0.71
double err = cm.Error;             // should be 0.29
double ca = cm.ChanceAgreement;    // should be 0.335
double ga = cm.GeometricAgreement; // should be 23.37
double kappa = cm.Kappa;           // should be 0.563

The third example shows how to construct a general confusion matrix directly from a classifier and a dataset:

// Generate always same random numbers
Accord.Math.Random.Generator.Seed = 0;

// Let's say we would like to learn a classifier for the famous Iris
// dataset, and measure its performance using a GeneralConfusionMatrix

// Download and load the Iris dataset
var iris = new Iris();
double[][] inputs = iris.Instances;
int[] outputs = iris.ClassLabels;

// Create the multi-class learning algorithm for the machine
var teacher = new MulticlassSupportVectorLearning<Linear>()
{
    // Configure the learning algorithm to use SMO to train the
    //  underlying SVMs in each of the binary class subproblems.
    Learner = (param) => new SequentialMinimalOptimization<Linear>()
    {
        // If you would like to use other kernels, simply replace
        // the generic parameter to the desired kernel class, such
        // as for example, Polynomial or Gaussian:

        Kernel = new Linear() // use the Linear kernel
    }
};

// Estimate the multi-class support vector machine using one-vs-one method
MulticlassSupportVectorMachine<Linear> ovo = teacher.Learn(inputs, outputs);

// Compute classification error
GeneralConfusionMatrix cm = GeneralConfusionMatrix.Estimate(ovo, inputs, outputs);

double error = cm.Error;         // should be 0.066666666666666652
double accuracy = cm.Accuracy;   // should be 0.93333333333333335
double kappa = cm.Kappa;         // should be 0.9
double chiSquare = cm.ChiSquare; // should be 248.52216748768473

The next examples reproduce the results shown in various papers, with special attention to the multiple ways of computing the Variance for the Kappa statistic:

// Example from R. Congalton's book, "Assesssing 
// the accuracy of remotely sensed data", 1999.

int[,] table = // Analyst #1 (page 108)
{
    { 65,  4, 22, 24 },
    {  6, 81,  5,  8 },
    {  0, 11, 85, 19 },
    {  4,  7,  3, 90 },
};

// Create a new multi-class confusion matrix
var cm = new GeneralConfusionMatrix(table);

int[] rowTotals = cm.RowTotals;     // should be { 115, 100, 115, 104 }
int[] colTotals = cm.ColumnTotals;  // should be { 115, 100, 115, 104 }

double kappa = cm.Kappa;            // should be 0.65
double var = cm.Variance;           // should be 0.00076995084473426684
double var0 = cm.VarianceUnderNull; // should be 0.00074886435981842887
double varD = Accord.Statistics.Testing.KappaTest.DeltaMethodKappaVariance(cm); // should be 0.0007778
// Example from Ientilucci, Emmett (2006). "On Using and Computing the Kappa Statistic".
// Available on: http://www.cis.rit.edu/~ejipci/Reports/On_Using_and_Computing_the_Kappa_Statistic.pdf 

// Note: Congalton's method uses the Delta Method for approximating the Kappa variance,
// but the framework uses the corrected methods by Cohen and Fleiss. For more information, see:
// https://stats.stackexchange.com/questions/30604/computing-cohens-kappa-variance-and-standard-errors

int[,] matrix = // Matrix A (page 1)
{
    { 317,  23,  0,  0 },
    {  61, 120,  0,  0 },
    {   2,   4, 60,  0 },
    {  35,  29,  0,  8 },
};

// Create the multi-class confusion matrix
var a = new GeneralConfusionMatrix(matrix);

// Method A row totals (page 2)
int[] rowTotals = a.RowTotals;       // should be { 340, 181, 66, 72 }

// Method A col totals (page 2)
int[] colTotals = a.ColumnTotals;    // should be { 415, 176, 60, 8 }

// Number of samples for A (page 2)
int samples = a.NumberOfSamples;     // should be 659
int classes = a.NumberOfClasses;     // should be 4

// Po for A (page 2)
double po = a.OverallAgreement;      // should be 0.7663

// Pc for A (page 2)
double ca = a.ChanceAgreement;       // should be 0.4087

// Kappa value k_hat for A (page 3)
double kappa = a.Kappa;              // should be 0.605

// Variance value var_k for A (page 4)
double varD = a.VarianceDeltaMethod; // should be 0.00073735

// Other variance values according to Fleiss and Cohen
double var = a.Variance;             // should be 0.00071760415564207924
double var0 = a.VarianceUnderNull;   // should be 0.00070251065008366978
// Example from J. L. Fleiss, J. Cohen, B. S. Everitt, "Large sample
//  standard errors of kappa and weighted kappa" Psychological Bulletin (1969)
//  Volume: 72, Issue: 5, American Psychological Association, Pages: 323-327

// This was the paper which presented the finally correct
// large sample variance for Kappa after so many attempts.

double[,] matrix =
{
    { 0.53,  0.05,  0.02 },
    { 0.11,  0.14,  0.05 },
    { 0.01,  0.06,  0.03 },
};

// Create a new multi-class confusion matrix:
var a = new GeneralConfusionMatrix(matrix, 200);

double[] rowProportions = a.RowProportions;    // should be { 0.6, 0.3, 0.1 }
double[] colProportions = a.ColumnProportions; // should be { 0.65, 0.25, 0.10 }

double kappa = a.Kappa;                        // should be 0.429
double var = a.Variance;                       // should be 0.002885
double var0 = a.VarianceUnderNull;             // should be 0.003082
See Also