Calculate RMSE for RF regression hyperparameter tuning in GEE encountering issue with error "(...)List<FeatureCollection>."

16 views Asked by At

I'm proceeding a very basic hyperparameter tuning for my Random Forest regression algorithm in GEE. In the process I also wanted to compute the RSME to assess said hyperparameter tuning.

I receive an error message saying: AggregateFeatureCollection.array, argument 'collection': Invalid type. Expected type: FeatureCollection. Actual type: List<FeatureCollection>. Actual value: [<FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>]

// I add these code for further context:

// Tune multiple parameters
var numTreesList = ee.List.sequence(10, 150, 10);
var bagFractionList = ee.List.sequence(0.1, 0.9, 0.1);

var accuracies = numTreesList.map(function(numTrees) {
  var bag = bagFractionList.map(function(bagFraction) {
// Create RF model with standard arguments and train it
var trainedClassifier = ee.Classifier.smileRandomForest({
    numberOfTrees: numTrees,
    bagFraction: bagFraction
    })
      .setOutputMode('REGRESSION')
      .train({
        features: normalizedFeatures,
        classProperty: 'biomass_g',
        inputProperties: ['B2_scaled', 'B3_scaled', 'B4_scaled', 'B5_scaled', 'B6_scaled', 'B7_scaled', 'B8_scaled', 'B8A_scaled', 'B11_scaled', 'B12_scaled', 'EVI_scaled', 'MCARI_scaled', 'MTVI2_scaled', 'NDVI_scaled']
});
return trainedClassifier;
      });
return bag;
  });

print('Result of trained Classifier from Hyperparameter tuning', accuracies);



// Computing RMSE to assess hyperparameter tuning and choose best parameters
var predicted = accuracies.map(function(classifiers) {
  var classifier_ind = ee.List(classifiers).map(function(classifier) {
    var classified = normalizedFeatures.classify({
  classifier: classifier,
  outputName: 'agb_predicted'
   });
return classified;
});
return classifier_ind;
  });
print('Predicted', predicted.flatten());

For the following code:

// Computing RMSE to assess hyperparameter tuning and choose best parameters
var predicted = accuracies.map(function(classifiers) {
  var classifier_ind = ee.List(classifiers).map(function(classifier) {
    var classified = normalizedFeatures.classify({
  classifier: classifier,
  outputName: 'agb_predicted'
   });
return classified;
});
return classifier_ind;
  });
print('Predicted', predicted.flatten());

// RMSE
var calculateRMSE = function(input) {
    var combinedFC = ee.FeatureCollection(input);
    var observed =  combinedFC.aggregate_array('B2_scaled');
    var predicted = combinedFC.aggregate_array('agb_predicted');
    var obs_rmse = ee.Array(observed);
    var rmse = obs_rmse.subtract(predicted).pow(2)
        .reduce('mean', [0]).sqrt().get([0]);
    return rmse;
};

I can solve temporally it by creating the variable 'combinedFC, but then I receive the error message: combinedFC.aggregate_array('B2_scaled'); is not a function or the message described above.

The variable 'predicted' shows in the console a FeatureCollection consisting of 135 empty elements, see below.

FeatureCollection (0 columns)
1: 
FeatureCollection (0 columns)
2: 
FeatureCollection (0 columns)
3: 
FeatureCollection (0 columns)

Does anyone know how I can solve this error?

I have changed the input into a FeatureCollection and saved said FeatureCollection in another variable. And changed 'input.aggregated_array' to ee.Array(input).aggregted_array' to 'ee.FeatureCollection(input).aggregate_arry) and then to combinedFC.aggregate_array.

0

There are 0 answers