Creates an empty Random Forest classifier.

ee.Classifier.smileRandomForest(numberOfTrees, variablesPerSplit, minLeafPopulation, bagFraction, maxNodes, seed)Classifier

The number of decision trees to create.

variablesPerSplitInteger, default: null

The number of variables per split. If unspecified, uses the square root of the number of variables.

minLeafPopulationInteger, default: 1

Only create nodes whose training set contains at least this many points.

bagFractionFloat, default: 0.5

The fraction of input to bag per tree.

maxNodesInteger, default: null

The maximum number of leaf nodes in each tree. If unspecified, defaults to no limit.

seedInteger, default: 0

The randomization seed.


Code Editor (JavaScript)

// A Sentinel-2 surface reflectance image, reflectance bands selected,
// serves as the source for training and prediction in this contrived example.
var img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')

// ESA WorldCover land cover map, used as label source in classifier training.
var lc = ee.Image('ESA/WorldCover/v100/2020');

// Remap the land cover class values to a 0-based sequential series.
var classValues = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100];
var remapValues = ee.List.sequence(0, 10);
var label = 'lc';
lc = lc.remap(classValues, remapValues).rename([label]);

// Add land cover as a band of the reflectance image and sample 500 pixels at
// 10 m scale from within a region of interest.
var roi = ee.Geometry.Rectangle(-122.347, 37.743, -122.024, 37.838);
var trainingSample = img.addBands(lc).sample({
  region: roi,
  scale: 10,
  numPixels: 500,
  geometries: true

// Train a 10-tree random forest classifier from the sample.
var trainedClassifier = ee.Classifier.smileRandomForest(10)
                            .train(trainingSample, label);

// Classify the reflectance image.
var classified = img.classify(trainedClassifier);

// Add the layers to the map.
var classVis = {
  min: 0,
  max: 10,
  palette: ['006400' ,'ffbb22', 'ffff4c', 'f096ff', 'fa0000', 'b4b4b4',
            'f0f0f0', '0064c8', '0096a0', '00cf75', 'fae6a0']
Map.setCenter(-122.184, 37.796, 12);
Map.addLayer(img, {bands: ['B11', 'B8', 'B3'], min: 100, max: 3500}, 'img');
Map.addLayer(lc, classVis, 'lc');
Map.addLayer(classified, classVis, 'classified');
Map.addLayer(roi, {color: 'white'}, 'roi', false, 0.5);
Map.addLayer(trainingSample, {color: 'black'}, 'trainingSample', false, 0.7);