0

I'm trying to perform a supervised classification of a composite image from a Sentinel-2 time series. I'm using cart as classifier and a set of points as trainers. Once computed the classification I'm trying to obtain a confusion matrix to compute my overall accuracy but something doesn't work.

Here the script

    //Set the input image for composite_median
var input_med = ee.Image(composite_median);
print (input_med);

//Add pings as feature clooctions (a series for each class)

//Merge feature collections
var newfc = water.merge(Deciduous).merge(Coniferous).merge(BareSoil);
print(newfc);

//Create training data
var bands_name = input_med.bandNames();
print (bands_name);
var bands = ['B2','B3','B4','B5','B8','nd'];
var training = input_med.sampleRegions({
  collection: newfc,
  properties: ['class'],
  scale:30
});
print(training);

//Train the classifier
var classifier = ee.Classifier.cart().train({
  features: training,
  classProperty: 'class',
  inputProperties: bands_name
});

//Run the classifier
var classified_med = input_med.select(bands_name).classify(classifier);
print(classified_med);

//Show classification results
Map.addLayer(classified_med,
{min:0, max:3, palette:['0209d6', 'b8a205', '12ff03', '2da008']},
'classification_med');

// Get a confusion matrix representing resubstitution accuracy. How well the classifier was able to correctly label resubstituted training data
print('error matrix: ', classifier_mean.confusionMatrix());
print('accuracy: ', classifier_mean.confusionMatrix().accuracy());

Now I have another set of points as feature collection that i want to use as validation points to compute the overall accuracy (not only for training data.

How can I do this?

I'm trying using the following script, but I'm not sure that is the right way and I have an error every time.

//Get a set of check points as validation data
var checkpoints = BareSoil_acc.merge(Deciduous_acc).merge(Coniferous_acc).merge(Water_acc);
print(checkpoints);

var bands_name_acc = classified_mean.bandNames();
print (bands_name_acc);

var validation = classified_mean.sampleRegions({
  collection: checkpoints,
  properties: ['class'],
  scale:30
}).filter(ee.Filter.neq('bands_name', null));
print(validation);

var classifier_acc = ee.Classifier.cart().train({
  features: checkpoints,
  classProperty: 'class',
  inputProperties: bands_name_acc
});

// Classify the validation data.
var validated = validation.classify(classifier_mean);
print (validated)

// Get a confusion matrix representing expected accuracy.
print('error matrix: ', validated.confusionMatrix());
print('accuracy: ', validated.confusionMatrix().accuracy());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.