Confusion Matrices

A confusion matrix is an Earth Engine variable object that represents what is sometimes called an “error matrix” or a “contingency table.” It is an array of two similar axes, one representing a set of known values and the other representing a corresponding set of predicted values. The array then records how often each of a set of predictions associates a particular known value with a particular predicted value. These predictions are usually those generated by a classifier or stored as a collection of trained features for multispectral satellite image classification. Confusion matrices can be processed by using operations of the types listed below.

Table of contents

  1. Creating Confusion Matrices
    1. ee.ConfusionMatrix
  2. Reproducing Confusion Matrices
    1. confusionMatrix.array
  3. Describing Confusion Matrices
    1. confusionMatrix.kappa
    2. confusionMatrix.accuracy
    3. confusionMatrix.producersAccuracy
    4. confusionMatrix.consumersAccuracy
    5. confusionMatrix.order
    6. confusionMatrix.getInfo
  4. Documenting Confusion Matrices
    1. confusionMatrix.getInfo
    2. confusionMatrix.toString
    3. ee.Algorithms.Describe
    4. confusionMatrix.serialize
  5. Presenting Confusion Matrices
    1. In Print
      1. print(confusionMatrix)
      2. alert(confusionMatrix)
      3. console.log(confusionMatrix)
      4. confirm

Creating Confusion Matrices

ee.ConfusionMatrix

Creates a new confusion matrix from a specified two-dimensional array whose

  • horizontal rows (axis 1) represent known classes
  • vertical columns (axis 0) represent predicted classes
  • values indicate the number cases in which a given known value was classfified as a given predicted value

Syntax

Javascript

newConfusionMatrix = ee.ConfusionMatrix(array,order)
  • newConfusionMatrix is the new confusion matrix.
  • array is the specified array.
  • order is the row and column size and order of a non-contiguous or non-zero matrix, given as a list.

Example

Javascript

var TheARRAY           = ee.Array( [ [0,2,0,0],  
                                     [0,1,0,0],  
                                     [0,1,3,1], 
                                     [0,1,3,4]   ] ); 
var TheCONFUSIONMATRIX = ee.ConfusionMatrix( TheARRAY ); 
print( TheCONFUSIONMATRIX ); 

Reproducing Confusion Matrices

confusionMatrix.array

  • Creates a creates a new two-dimensional array from a specified confusion matrix.

Syntax

Javascript

newArray = oldConfusionMatrix.array()
  • newArray is the new array.
  • oldConfusionMatrix is the specified confusion matrix.

Example

Javascript

var TheARRAY           = ee.Array( [ [0,2,0,0],  
                                     [0,1,0,0],  
                                     [0,1,3,1], 
                                     [0,1,3,4]   ] ); 
var TheCONFUSIONMATRIX = ee.ConfusionMatrix( TheARRAY ); 
var NewARRAY           = TheCONFUSIONMATRIX.array(); 
print( NewARRAY ); 

Describing Confusion Matrices

confusionMatrix.kappa

  • Creates a new floating-point number indicating the Kappa statistic for a specified confusion matrix.
  • This measures the agreement between two datasets on a scale that genereally ranges from 0 to 1.

Syntax

Javascript

newNumber = oldConfusionMatrix.kappa()
  • newNumber is the new number.
  • oldConfusionMatrix is the specified confusion matrix.

Example

Javascript

var TheARRAY           = ee.Array( [ [0,2,0,0],  
                                     [0,1,0,0],  
                                     [0,1,3,1], 
                                     [0,1,3,4]   ] ); 
var TheCONFUSIONMATRIX = ee.ConfusionMatrix( TheARRAY ); 
var NewNumber           = TheCONFUSIONMATRIX.kappa(); 
print( TheCONFUSIONMATRIX ); 
print( NewNumber ); 

confusionMatrix.accuracy

  • Creates a new floating-point number indicating the accuracy of a specified confusion matrix.
  • Computed as the number of correct classifications divided by the total number of classifications.

Syntax

JavaScript

newNumber = oldConfusionMatrix.accuracy()
  • newNumber is the new number.
  • oldConfusionMatrix is the specified confusion matrix.

Example

############## JavaScript

var TheARRAY           =  ee.Array( [ [0,2,0,0],  
                                     [0,1,0,0],  
                                     [0,1,3,1], 
                                     [0,1,3,4]   ] ); 
var TheCONFUSIONMATRIX =  ee.ConfusionMatrix( TheARRAY ); 
var NewNumber           = TheCONFUSIONMATRIX.accuracy(); 
print(TheCONFUSIONMATRIX);
print( NewNumber ); 

confusionMatrix.producersAccuracy

  • creates a new array indicating the consumer’s accuracy(reliability) of a specified confusion matrix.
  • computed as the number of correct classifications divided by the the total number of classifications for each column.

Syntax

Javascript

newArray = oldConfusionMatrix.producersAccuracy()
  • newArray is the new array.
  • oldConfusionMatrix is the specified confusion matrix.

Example

Javascript

var TheARRAY           = ee.Array( [ [0,2,0,0],  
                                     [0,1,0,0],  
                                     [0,1,3,1], 
                                     [0,1,3,4]   ] ); 
var TheCONFUSIONMATRIX = ee.ConfusionMatrix( TheARRAY ); 
var NewARRAY           = TheCONFUSIONMATRIX.producersAccuracy(); 
print (TheCONFUSIONMATRIX);
print( NewARRAY ); 

confusionMatrix.consumersAccuracy

  • Creates a new array indicating the consumer’s accuracy(reliability) of a specified confusion matrix.
  • Computed as the number of correct classifications divided by the the total number of classifications for each row.

Syntax

Javascript

newArray = oldConfusionMatrix.consumersAccuracy()
  • newNumber is the new number.
  • oldConfusionMatrix is the specified confusion matrix.

Example

Javascript

var TheARRAY           = ee.Array( [ [0,2,0,0],  
                                     [0,1,0,0],  
                                     [0,1,3,1], 
                                     [0,1,3,4]   ] ); 
var TheCONFUSIONMATRIX = ee.ConfusionMatrix( TheARRAY ); 
var NewARRAY           = TheCONFUSIONMATRIX.consumersAccuracy(); 
print (TheCONFUSIONMATRIX);
print( NewARRAY ); 

confusionMatrix.order

  • Creates a new list indicating the name and order of the rows and columns of a specified confusion matrix.

Syntax

Javascript

newList = oldConfusionMatrix.order()
  • newList is the new list of name and order of rows and columns.
  • oldConfusionMatrix is the specified confusion matrix.

Example

Javascript

var TheARRAY           = ee.Array( [ [0,2,0,0],  
                                     [0,1,0,0],  
                                     [0,1,3,1], 
                                     [0,1,3,4]   ] ); 
var TheCONFUSIONMATRIX = ee.ConfusionMatrix( TheARRAY, [3,2,1,0] ); 
var NewLIST            = TheCONFUSIONMATRIX.order(); 
print( TheCONFUSIONMATRIX ); 
print( NewLIST ); 

confusionMatrix.getInfo

  • Creates a JSON-compatible text object representing a specified confusion matrix.

Syntax

Javascript

newObject = oldConfusionMatrix.getInfo()
  • newObject is the new text object.
  • oldConfusionMatrix is the specified confusion matrix.
  • alternatively, you could also use ee.Algorithms.Describe(oldConfusionMatrix) to achieve the same results

Example

Javascript

var TheARRAY           = ee.Array( [ [0,2,0,0],  
                                     [0,1,0,0],  
                                     [0,1,3,1], 
                                     [0,1,3,4]   ] ); 
var TheCONFUSIONMATRIX = ee.ConfusionMatrix( TheARRAY ); 
print( TheCONFUSIONMATRIX.getInfo( ) ); 

//alternatively
print( ee.Algorithms.Describe( TheCONFUSIONMATRIX ) ); 

Documenting Confusion Matrices

confusionMatrix.getInfo

Contribute!

confusionMatrix.toString

Contribute!

ee.Algorithms.Describe

Contribute!

confusionMatrix.serialize

Contribute!

Presenting Confusion Matrices

In Print

print(confusionMatrix)

alert(confusionMatrix)

console.log(confusionMatrix)

confirm