The minimumDistance classifier in GEE uses the nearest neighbor procedure to predict a class (in classification mode) or a real value (in regression mode). The parameter "Knearest" set by the user is the number (K) of neighbors to use. I understand that in classification mode it gives the class ID of each of the K neighbors, it's very clear, and after I can use Mode to estimate the class. But in regression mode, the notice says that it gives a "distance to the nearest class center". Strange ... Where can I find the coordinates of each class centers ? Why to not give the real value of the K nearest neighbors ! It would be more easy to average those values and find the prediction !!!! How can I fix this problem ?

My Javascript code is below:

// Create a bounding rectangle for the entire planet

var unboundedGeo = ee.Geometry.Rectangle([-180, -88, 180, 88], "EPSG:4326", false);
// Prepare the feature collection of points

var inputtedFeatureCollection = ee.FeatureCollection('users/romainwalcker/XXXXXXX_run1');

Map.addLayer(inputtedFeatureCollection, {}, "Points being sampled");

// Make a compositeOfInterest of selected covariates (This section is hided because not interesting)

// Making the composite bands of covariates

var compositeOfInterest = bio12.addBands(lai).addBands(moist).addBands(soc).addBands(vpd).addBands(sand);

// Choose the bands of interest from the composite
var listOfVars = ['bio12','lai','moist','soc','vpd','sand'];

// Input the name of the property of interest
var propertyToPredictAsString = 'log_pp';

// Sample the image
var trainingData = compositeOfInterest.sampleRegions({
collection: inputtedFeatureCollection,

// Classify and display the final image
var finalClassifiedImage = compositeOfInterest.classify(ee.Classifier.minimumDistance({
    metric: 'euclidean',
    kNearest: 5,
    .train(trainingData, propertyToPredictAsString, listOfVars));


In the inspector, my result stored in finalClassifiedImage is: 0:122.60172485474355 1:452.04783495770914 2:459.52036616946293 3:575.6726571970663 4:744.0845882199577

How can I predict my value (ranging between 0 and 15) with those values ?

1 Answer 1


The distance is provided in regression mode so you can easily estimate or weight the likely-hood of each point belonging to each of the K neighbors returned without having to figure out how to compute it in whatever distance metric you're using.

The class centers are what you get when you take the average of all the training data for each class, so you can get those directly (in which case you don't need the regression/distance mode):

var reducer = ee.Reducer.mean().repeat(listOfVars.length)
var centers = trainingData.classify(classifier)
    .reduceColumns(reducer.group(0), [propertyToPredictAsString].concat(listOfVars))

Here's a full example using different data because your data isn't shared. https://code.earthengine.google.com/65226609557bc746d9aaa2bd7d0838ef

If you want them in some other form, then you might find it easier to map over the classes and filter the training data to each individual class before taking the mean.

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.