I want to check the classification accuracy using the confusion matrix in GEE, but I run into some problems.

I generate some random points of known classification types and want to use them to check the classification against the images I generated myself. However, when I use the confusion matrix API in GEE, the confusion matrix I get is [[0]].

I don't know why I get this result.

How can I solve this problem?

The code involved in the confusion matrix is as follows:

var nonwater = table2.geometry();
var permanent = table3.geometry();
var seasonal = table4.geometry();
var vis = {
  bands: ['waterClass'],
  min: 0.0,
  max: 1.0,
  palette: ['white', 'black']

Map.addLayer(nonwater, vis, 'nonwater');
Map.addLayer(permanent, vis, 'permanent');
Map.addLayer(seasonal, vis, 'seasonal');

var feature1 = ee.FeatureCollection(nonwater).set({'class':'1'});
var feature2 = ee.FeatureCollection(permanent).set({'class':'3'});
var feature3 = ee.FeatureCollection(seasonal).set({'class':'2'});
var featurecollection = ee.FeatureCollection([feature1,feature2,feature3]).flatten();

var imageSamp = ee.Image(water_class).sampleRegions({
  "collection": featurecollection,
  "scale": 30,
  // geometries: true

var confusionMatrix = imageSamp.errorMatrix('water_class', 'class');
print('overall accuracy', confusionMatrix.accuracy());
print('kappa accuracy', confusionMatrix.kappa());

Here is the link to the full code: https://code.earthengine.google.com/45c1a51d9e991a6081e8112240d13b99

1 Answer 1


This code:


does not do what you think it does. It sets a value on the collection object, not on the elements in the collection. Instead, you want:

ee.FeatureCollection(nonwater).map(function(f) {
   return f.set({'class':'1'})
  • Many thanks for your replying! I realized that this was one of my errors and did not notice that the code before modification was directly setting properties to the feature collection. But after correcting this error, the result of the confusion matrix is still [[0]]. After checking the whole code again, I found that the classification image water_class doesn't inherit the property 'water_class'. I speculate this may be the reason why the confusion matrix result is still incorrect. How can I solve this problem?
    – YX Zhao
    Commented Dec 27, 2023 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.