In this practice, you will be able to create a Confusion Matrix to determine the Overall Accuracy, Producer's Accuracy, and User's Accuracy of your classification map
For this practice, let's use the same place we used for the Image classification class
> Add the code below in your Code Editor > click Run
The AOI will be near Sapporo City - Hokkaido, Japan
After clicking Run, you will be able to see the image below
As we did for the first classification, let's add the following code from line 12
Note that for the classes this time, we are going to use a prefix T_ to identify the classes for classification (later we will need to create classes for validation)
This time, the T_forest class will be assigned 0 instead of 1
Another important point is not to plot the classification map in the Map Window yet. This is necessary to eliminate the bias that can be generated if you see the data prior to the validation
Let's add the following code to create the validation points
> Add the code below on line 19
When creating the validation samples, instead of class, we are going to use validation like the figure below
Note that the class and validation values are the same for classification and validation
On line 20, we are creating validation samples (it is the same step to create training samples). The variable validation_classes will be used to validate your classification map
On line 21, we are comparing the validation points to the classification map, to create the Confusion Matrix
Google Earth Engine already has a function to automatic create the matrices and calculate the final value.
> Add the code below on line 23
On line 24 we are just defining what we need to cross, in this case, will be the validation points and classification results
Lines 25, 26, and 27 are just creating the final values
After calculating everything, let's plot the classification map in the Map Window and see the accuracy values on the console
> Add the following code on line 29
You will be able to see the classification map on the Map Window and the accuracies on the console
In this practice, we only use few validation points because of time constraints. In real life you need more validation points to create a more robust assessment of your classification map
var bands = ['SR_B7','SR_B6','SR_B5','SR_B4','SR_B3','SR_B2'];
var image = ee.ImageCollection('LANDSAT/LC09/C02/T1_L2')
.filterDate('2022-09-08', '2022-09-10')
.filterBounds(AOI)
.select(bands)
.median()
.clip(AOI);
Map.addLayer(image, {bands:['SR_B4','SR_B3','SR_B2'], min:7000, max:19000, gamma:2}, 'image');
// Classification
var training_classes = T_forest.merge(T_grassland).merge(T_water).merge(T_bare).merge(T_urban);
var training_samples = image.sampleRegions(training_classes, ['class'], 30);
var classifier = ee.Classifier.libsvm();
var trained_classifier = classifier.train(training_samples, 'class');
var classified = image.classify(trained_classifier);
// Validation
var validation_classes = V_forest.merge(V_grassland).merge(V_water).merge(V_bare).merge(V_urban);
var validation_training = classified.sampleRegions(validation_classes, ['validation'], 30);
// Confusion Matrix
var confusion_matrix = validation_training.errorMatrix('validation', 'classification');
var overall_accuracy = confusion_matrix.accuracy();
var producer_accuracy = confusion_matrix.producersAccuracy().toList();
var user_accuracy = confusion_matrix.consumersAccuracy().toList();
// Adding classification Map and accuracies
Map.addLayer(classified, {min:0, max:4, palette:['darkgreen', 'lime', 'blue', 'orange', 'red']}, 'Classification');
print('Confusion Matrix', confusion_matrix);
print('Overall Accuracy',overall_accuracy);
print('Producers Accuracy',producer_accuracy);
print('Users Accuracy',user_accuracy);