在 GEE 中计算 RF 回归超参数调整的 RMSE 时遇到错误“(...)List<FeatureCollection>。”

问题描述 投票:0回答:1

我正在 GEE 中对随机森林回归算法进行非常基本的超参数调整。在此过程中,我还想计算 RSME 来评估所述超参数调整。

我收到一条错误消息:

AggregateFeatureCollection.array, argument 'collection': Invalid type. Expected type: FeatureCollection. Actual type: List<FeatureCollection>. Actual value: [<FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>, <FeatureCollection>]

// 我添加这些代码以获取更多上下文:

// 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());

对于以下代码:

// 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;
};

我可以通过创建变量“combinedFC”来临时解决它,但随后我收到错误消息:combinedFC.aggregate_array('B2_scaled');不是上述函数或消息。

变量“predicted”在控制台中显示一个由 135 个空元素组成的FeatureCollection,见下文。

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

有谁知道如何解决这个错误?

我已将输入更改为FeatureCollection,并将所述FeatureCollection保存在另一个变量中。并将 'input.aggregate_array' 更改为 ee.Array(input).aggregted_array' 更改为 'ee.FeatureCollection(input).aggregate_arry),然后更改为合并FC.aggregate_array。

javascript regression random-forest hyperparameters google-earth-engine
1个回答
0
投票

您可以尝试这个解决方案:

// Defining the vectors as Earth Engine arrays
var observed = ee.Array([3.0, -0.5, 2.0, 1.5]);
var predicted = ee.Array([2.5, 0.0, 2.1, 1.6]);

// Performing subtraction
var res = observed.subtract(predicted);

// Raising each element of "res" to the power of 2 using pow
var squaredres = res.pow(2);
// var squaredres = res.multiply(res); // other option

// Calculating the mean of the elements in squaredres
var meanSquaredres = squaredres.reduce(ee.Reducer.mean(), [0]); // For a one-dimensional array, the dimension is 0

// Taking the square root of the mean squared res to get the RMSE
var rmse = meanSquaredres.sqrt();

// Printing the RMSE value
print('The RMSE is: ', rmse);
© www.soinside.com 2019 - 2024. All rights reserved.