0

My goal is to come up with a heavy metal classification map using Sentinel 2 images by applying Random Forest. The heavy metal training data is a set of points saved as a .csv file in a column labelled as "metal". As someone new to GEE, I have a code below that resulted with this error upon running it:

Line 112: ee.ConfusionMatrix.fromColumns is not a function

What might have caused the error?

Here is the code.

var trainingData = ee.FeatureCollection('projects/assets/data4GEE');
var targetProperty = 'metal'; 
var latitudeProperty = 'latitude'; 
var longitudeProperty = 'longitude'; 

var studyArea = ee.Geometry.Rectangle(-84.52621592924908, 39.96878827999406, -85.49301280424908, 40.969525916935); 


var sentinel2 = ee.ImageCollection('COPERNICUS/S2')
  .filterBounds(studyArea) 
  .filterDate('2020-08-01','2020-08-30'); 

function calculateVegetationIndices(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
  var evi = image.expression(
    '2.5 * ((B8 - B4) / (B8 + 6 * B4 - 7.5 * B2 + 1))', {
      'B2': image.select('B2'),
      'B4': image.select('B4'),
      'B8': image.select('B8')
  }).rename('EVI');
  return image.addBands(ndvi).addBands(evi);
}

var sentinel2WithIndices = sentinel2.map(calculateVegetationIndices);

var predictors = sentinel2WithIndices.select(['B2', 'B4', 'B8', 'NDVI', 'EVI']);

var trainingDataFeatures = trainingData.map(function(feature) {
  var coordinates = ee.Geometry.Point([feature.get(longitudeProperty), feature.get(latitudeProperty)]);
  return ee.Feature(coordinates, {'metal': feature.get(targetProperty)});
});

var trainingDataWithPredictors = predictors.map(function(image) {
  var joinedFeatures = image.reduceRegions({
    collection: trainingDataFeatures,
    reducer: ee.Reducer.first(),
    scale: 10
  });
  return image.set('joinedFeatures', joinedFeatures);
}).flatten();

trainingDataWithPredictors = trainingDataWithPredictors.filter(ee.Filter.notNull(['joinedFeatures']));

var trainingDataFeatures = trainingDataWithPredictors.aggregate_array('joinedFeatures');
var predictorsWithTrainingData = predictors.map(function(image) {
  return image.select(predictors.first().bandNames()).set('trainingData', trainingDataFeatures);
});

var combinedImage = predictorsWithTrainingData.toBands();

var classifier = ee.Classifier.randomForest({
  numberOfTrees: 100, 
  variablesPerSplit: 5 
});

var trainedClassifier = classifier.train({
  features: trainingDataWithPredictors,
  classProperty: targetProperty, 
  inputProperties: predictors.first().bandNames() 
});


var trainingAccuracy = trainedClassifier.confusionMatrix();
print('Training Accuracy:', trainingAccuracy);

var classifiedImage = combinedImage.classify(trainedClassifier);


Map.addLayer(classifiedImage, { min: 0, max: 1, palette: ['blue', 'green'] }, 'metal Classification');

var evaluateModel = function(image) {
  // Retrieve the metal values from the training data
  var referenceData = trainingDataWithPredictors.reduceColumns({
    reducer: ee.Reducer.toList().repeat(2),
    selectors: [targetProperty, '.all']
  });

  var classifiedPixels = image.select('classification').reduceRegions({
    collection: studyArea,
    reducer: ee.Reducer.first(),
    scale: 10
  });

  var predictedData = classifiedPixels.reduceColumns({
    reducer: ee.Reducer.toList().repeat(2),
    selectors: ['classification', '.all']
  });

  var referencemetal = ee.List(referenceData.get(targetProperty)).flatten();

  var predictedClasses = ee.List(predictedData.get('classification')).flatten();

  var confusionMatrix = ee.ConfusionMatrix.fromColumns({
    reference: referencemetal,
    prediction: predictedClasses
  });

  var accuracy = confusionMatrix.accuracy();
  var producersAccuracy = confusionMatrix.producersAccuracy();
  var usersAccuracy = confusionMatrix.consumersAccuracy();

  print('Confusion Matrix:', confusionMatrix);
  print('Overall Accuracy:', accuracy);
  print('Producers Accuracy:', producersAccuracy);
  print('Users Accuracy:', usersAccuracy);
};

evaluateModel(classifiedImage);
1
  • The ee.ConfusionMatrix.fromColumns method does not exist in GEE (searched it in the Docs tab). Have you tried building the confusion matrix directly with ee.ConfusionMatrix? Commented Jun 20, 2023 at 0:14

0

Your Answer

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