[GIS] Exporting classification error matrix Google Earth Engine

exportgoogle-earth-engineimage classification

I did a supervised classification of land cover and I need to export the Validation error matrix to .csv file. When using 'Export.table.toDrive' an error message 'Invalid argument: 'collection' must be a FeatureCollection' appears. Is there any way how to do that?

var year99=ee.Image('LANDSAT/LE07/C01/T1_SR/LE07_191025_19990915')
.clip(table)
.updateMask(FinalMask);

// Input image

var input=year99;

// Select and visualize MODIS land cover, IGBP classification, for training.

var modis=ee.Image('MODIS/051/MCD12Q1/2013_01_01')
.select('Land_Cover_Type_1')
.clip(table)
.updateMask(FinalMask);


// Define a palette for the 18 distinct land cover classes.

var igbpPalette = [
  'aec3d4', // water
  '152106', '225129', '369b47', '30eb5b', '387242', // forest
  '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40',  // shrub, grass
  '111149', // wetlands
  'cdb33b', // croplands
  'cc0013', // urban
  '33280d', // crop mosaic
  'd7cdcc', // snow and ice
  'f7e084', // barren
  '6f6f6f'  // tundra
];

// Specify the min and max labels and the color palette matching the labels.

Map.setCenter(15.293, 50.659, 8);

Map.addLayer(modis,
             {min: 0, max: 17, palette: igbpPalette},
             'IGBP classification');


// Visualize input image

var visParams = {bands: ['B3', 'B2', 'B1'], min: 16, max: 1412};

Map.addLayer(input,visParams, 'input');

// Print input image and Modis Landcover image 

print ('input',input);
print ('modis',modis);


// Sample the input imagery to get a FeatureCollection of training data.
var training = input.addBands(modis).sample({
  numPixels: 500,
  seed: 0,
  region: table
});

// Make a Random Forest classifier and train it.
var classifier = ee.Classifier.randomForest(10)
    .train(training, 'Land_Cover_Type_1');

// Classify the input imagery.
var classified = input.classify(classifier);

// Get a confusion matrix representing resubstitution accuracy.
var trainAccuracy = classifier.confusionMatrix();
print('Resubstitution error matrix: ', trainAccuracy);
print('Training overall accuracy: ', trainAccuracy.accuracy());

// Sample the input with a different random seed to get validation data.
var validation = input.addBands(modis).sample({
  numPixels: 500,
  seed: 1,
  region:table
  // Filter the result to get rid of any null pixels.
}).filter(ee.Filter.neq('B1', null));

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

// Get a confusion matrix representing expected accuracy.
var testAccuracy = validated.errorMatrix('Land_Cover_Type_1',         'classification');
print('Validation error matrix: ', testAccuracy);
print('Validation overall accuracy: ', testAccuracy.accuracy());


// Display the input and the classification.
Map.centerObject(table, 8);
Map.addLayer(input, {bands: ['B3', 'B2', 'B1'], max: 0.4}, 'landsat');
Map.addLayer(classified, {palette: igbpPalette, min: 0, max: 17},     'classification');


print('classified',classified);

Best Answer

Without sharing the table it is impossible to recreate your error. You could share the complete script by using the "Get Link" button.

If you want to export the error matrix you need to cast it to a FeatureCollection first - just as the error message tells you.

var exportAccuracy = ee.Feature(null, {matrix: testAccuracy.array()})

// Export the FeatureCollection.
Export.table.toDrive({
  collection: ee.FeatureCollection(exportAccuracy),
  description: 'exportAccuracy',
  fileFormat: 'CSV'
});