0

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();
print('feature3',feature3)

///**************************************************************
var imageSamp = ee.Image(water_class).sampleRegions({
  "collection": featurecollection,
  "scale": 30,
  'properties':['class']
  // geometries: true
});
print('imageSamp',imageSamp)

var confusionMatrix = imageSamp.errorMatrix('water_class', 'class');
print('confusionMatrix',confusionMatrix);
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

0

This code:

ee.FeatureCollection(nonwater).set({'class':'1'});

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'})
}
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.