GeneralConfusionMatrix Class |
Namespace: Accord.Statistics.Analysis
The GeneralConfusionMatrix type exposes the following members.
Name | Description | |
---|---|---|
GeneralConfusionMatrix(Int32) |
Creates a new Confusion Matrix.
| |
GeneralConfusionMatrix(Double, Int32) |
Creates a new Confusion Matrix.
| |
GeneralConfusionMatrix(Int32, Int32) |
Creates a new Confusion Matrix.
| |
GeneralConfusionMatrix(Int32, Int32, Int32) |
Creates a new Confusion Matrix.
|
Name | Description | |
---|---|---|
Accuracy |
Accuracy. This is the same value as OverallAgreement.
| |
ChanceAgreement |
Chance agreement.
| |
ChiSquare |
Gets the Chi-Square statistic for the contingency table.
| |
Classes | Obsolete.
Obsolete. Please use NumberOfClasses instead.
| |
ColumnErrors |
Gets the col errors.
| |
ColumnProportions |
Gets the column marginals (proportions).
| |
ColumnTotals |
Gets the column totals.
| |
Cramer |
Cramer's V association measure.
| |
Diagonal |
Gets the diagonal of the confusion matrix.
| |
Error |
Error. This is the same value as 1.0 - OverallAgreement.
| |
ExpectedValues |
Expected values, or values that could
have been generated just by chance.
| |
GeometricAgreement |
Geometric agreement.
| |
Kappa |
Gets the Kappa coefficient of performance.
| |
Matrix |
Gets the confusion matrix, in which each element e_ij
represents the number of elements from class i classified
as belonging to class j.
| |
Max |
Gets the maximum number of correct
matches (the maximum over the diagonal)
| |
Min |
Gets the minimum number of correct
matches (the minimum over the diagonal)
| |
NumberOfClasses |
Gets the number of classes.
| |
NumberOfSamples |
Gets the number of samples.
| |
OverallAgreement |
Overall agreement.
| |
Pearson |
Pearson's contingency coefficient C.
| |
PerClassMatrices |
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.
| |
Phi |
Phi coefficient.
| |
Precision |
Gets the row precision.
| |
ProportionMatrix |
Gets the confusion matrix in
terms of cell percentages.
| |
Recall |
Gets the column recall.
| |
RowErrors |
Gets the row errors.
| |
RowProportions |
Gets the row marginals (proportions).
| |
RowTotals |
Gets the row totals.
| |
Sakoda |
Sakoda's contingency coefficient V.
| |
Samples | Obsolete.
Obsolete. Please use NumberOfSamples instead.
| |
StandardError |
Gets the standard error of the Kappa
coefficient of performance.
| |
StandardErrorUnderNull |
Gets the standard error of the Kappa
under the null hypothesis that the underlying Kappa
value is 0.
| |
Tau |
Gets the Tau coefficient of performance.
| |
TitleAboveColumns |
Gets or sets the title that ought be displayed on top of the columns of
this GeneralConfusionMatrix. Default is "Expected (Ground-truth)".
| |
TitleOnTheLeftOfRows |
Gets or sets the title that ought be displayed on left side of this
GeneralConfusionMatrix. Default is "Actual (Prediction)".
| |
Tschuprow |
Tschuprow's T association measure.
| |
Variance |
Gets the variance of the Kappa
coefficient of performance.
| |
VarianceDeltaMethod |
Gets the variance of the Kappa
coefficient of performance using Congalton's delta method.
| |
VarianceUnderNull |
Gets the variance of the Kappa
under the null hypothesis that the underlying
Kappa value is 0.
|
Name | Description | |
---|---|---|
Combine |
Combines several confusion matrices into one single matrix.
| |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
EstimateTInput(IClassifierTInput, Boolean, TInput, Boolean) |
Estimates a GeneralConfusionMatrix directly from a classifier, a set of inputs and its expected outputs.
| |
EstimateTInput(IClassifierTInput, Int32, TInput, Int32) |
Estimates a GeneralConfusionMatrix directly from a classifier, a set of inputs and its expected outputs.
| |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(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.) | |
ToT | 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.) |
References:
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