I would like to calculate RMSE for a model.

I have a FeatureCollection that contains the modelled and origianl properties. How can I do an element-wise subtraction (as the beginning of calculating RMSE)?

var model = ee.Classifier.cart().train(to_Train, 'N', bands);

var validated = to_Test.classify(model, 'N_');

var N = validated.select('N');
var N_ = validated.select('N_');
var sub = N.subtract(N_)


N.subtract is not a function

This is what I would like to achieve

var RMSE = (N - N_).pow(2).mean().sqrt()

I think I have to make an array but can't seem to get there!

Here is a link to simplified version.


1 Answer 1


To get the arrays of properties from the feature collection 'validated', use 'aggregate_array' and cast the outputs to arrays (GEE does not know what object is returned):

// // validation
var validated = to_Test.classify(rf_model, 'NN'); // I renamed it to NN as N_ did not work
var N = ee.Array(validated.aggregate_array('N'));
var NN = ee.Array(validated.aggregate_array('NN'));

To calculate the RMSE from the arrays, you should use only server-side functions. I made multiple lines of it to explain a bit what happens. You could abbriviate it to one line off course.

// Calculate the RMSE
var RMSE = (N.subtract(NN)).pow(2);       // array calculations
RMSE = RMSE.reduce('sum', [0]).get([0]);  // reduce the array to the sum -> output is a ee.Number()
RMSE = RMSE.divide(N.length().get([0]));    // divide by the amount of observations
RMSE = RMSE.sqrt();                       // Get the RMSE

Now note that your input feature collection consist of almost 50k features, so that will easily run out of compution. Export the RMSE to make it work. In this example I limited your input features so GEE is able to print the resulting RMSE. Note that array calculations will fail if the length of the arrays are not the same, so make sure they are.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.