Get the accuracy of you random forest classifier
step 1: add the image and training data
// add the image var image = ee.Image("users/servirmekong/Vietnam/Sent2_2017_Forest001") // add the reference data var referenceData = ee.FeatureCollection("users/servirmekong/Vietnam/ReferenceDataForestTraining");
step 2: select the different land cover categories
var water = referenceData.filter(ee.Filter.eq("land_type","water")); var crop = referenceData.filter(ee.Filter.eq("land_type","crop")); var urban = referenceData.filter(ee.Filter.eq("land_type","urban")); var plantations = referenceData.filter(ee.Filter.eq("land_type","plantations")); var forest = referenceData.filter(ee.Filter.eq("land_type","forest"));
step 3: Display the data
Map.addLayer(image,{min:0,max:6000,bands:"swir1,nir1,red"},"sentinel 2 image"); Map.addLayer(water.draw("blue"),{},"Water"); Map.addLayer(crop.draw("green"),{},"Crop"); Map.addLayer(urban.draw("darkred"),{},"Urban"); Map.addLayer(plantations.draw("yellow"),{},"Plantations"); Map.addLayer(forest.draw("darkgreen"),{},"Forest");
step 4: select 80% of the reference data
var sample = referenceData.randomColumn("random"); sample = sample.filter(ee.Filter.lt("random",0.8))
step 5: Sample the image
var trainingSample = image.sampleRegions(sample,["land_class"],10);
step 6: apply the classifier.
var bandNames = image.bandNames() var classifier = ee.Classifier.randomForest(10,0).train(trainingSample,"land_class",bandNames);
step 7: print all statistics of the classifier.
var confMatrix = classifier.confusionMatrix(); print(confMatrix); var OA = confMatrix.accuracy(); var CA = confMatrix.consumersAccuracy(); var Kappa = confMatrix.kappa(); var Order = confMatrix.order(); var PA = confMatrix.producersAccuracy(); print(confMatrix,'Confusion Matrix'); print(OA,'Overall Accuracy'); print(CA,'Consumers Accuracy'); print(Kappa,'Kappa'); print(Order,'Order'); print(PA,'Producers Accuracy');
hi, glad to find this blog, I am focusing to calculate the confusion matrix using GEE. May I get in touch with you?, my email: sarip.hidayat2013@gmail.com. thank you.
LikeLike