Help GEE random forest classification

I am a beginner in the use of GEE. I am trying to perform a classification with random forest taking as input the bands of a planet image, and the ndvi, ndwi and the segmentation of the image.
Is it possible what I want to do? I share the script I generated:

var ps = ee.ImageCollection('projects/planet-nicfi/assets/basemaps/americas');

var geometry = ee.FeatureCollection("users/eobrien/cajamarca");
var image = ps.filter(ee.Filter.date('2022-06-01', '2022-06-30')).first();
var image_cut = image.clip(geometry);

Map.centerObject(geometry, 10)

// Calcular NDVI y NDWI
var ndvi = image_cut.normalizedDifference(['N', 'R']);
var ps = ee.ImageCollection('projects/planet-nicfi/assets/basemaps/americas');

var geometry = ee.FeatureCollection("users/eobrien/cajamarca");
var image = ps.filter(ee.Filter.date('2022-06-01', '2022-06-30')).first();
var image_cut = image.clip(geometry);

Map.centerObject(geometry, 10);

// Calcular NDVI y NDWI
var ndvi = image_cut.normalizedDifference(['N', 'R']);
var ndwi = image_cut.normalizedDifference(['G', 'N']);


// Agregar banda ndvi y ndwi a la imagen
image_cut = image_cut.addBands(ndvi).addBands(ndwi).rename(['B', 'G', 'R', 'N', 'nd', 'nd_1']);



// Visualizar imágenes
var vis = {"bands":["R","G","B"],"min":64,"max":5454,"gamma":1.8};
Map.addLayer(image_cut, vis, 'Mosaic_CV_Planet_Jun_2022',true);

var vis1 = {"bands":["N","R","G"],"min":64,"max":5454,"gamma":1.8};
Map.addLayer(image_cut , vis1, 'Mosaic_NIR_Planet_Jun_2022',false);

// Visualizar imágenes NDVI y NDWI
var vis_ndvi = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.addLayer(ndvi, vis_ndvi, 'NDVI');

var vis_ndwi = {min: -1, max: 1, palette: ['navy', 'white', 'lime']};
Map.addLayer(ndwi, vis_ndwi, 'NDWI');

// Seeds
var seeds = ee.Algorithms.Image.Segmentation.seedGrid(15, 'hex');

// SNIC segmentation
var segment = ee.Algorithms.Image.Segmentation.SNIC({
  image: image_cut, 
  compactness: 50, 
  seeds: seeds
});

Map.addLayer(segment.select('clusters').randomVisualizer(), {}, 'Clusters', false);

Map.addLayer(segment, {bands: ['R_mean', 'G_mean', 'B_mean',], min: 170.02, max: 2086.81}, 'Planet Segment',false);

// Image for classification
var imageObject = segment.select(['R_mean', 'G_mean', 'B_mean', 'N_mean', 'nd', 'nd_1']);
var bandsName = imageObject.bandNames();

// Sample
var sample = Plantacion.merge(Agricola).merge(Sombras).merge(Agua).merge(Urbano).merge(Otra_veg).merge(Mineria);

// Trained
var trained = imageObject.sampleRegions({
  collection: sample,
  scale: 5,
  properties: ['class'],
  tileScale: 2
});

// Clasificación con Random Forest
var bandas = ['R_mean', 'G_mean', 'B_mean', 'N_mean', 'nd', 'nd_1'];
var clasificacion = ee.Classifier.smileRandomForest(100).train(trained, 'class', bandas);
var image1 = segment.select(bandas).classify(clasificacion);
Map.addLayer(image1, {min: 1, max: 7, palette: ['#79ff93', '#c2c116', '#d489ff', '#0000ff', '#6e7069','#009999','#ff00ff']}, 'Clasificación Random Forest', true);

print(image_cut.bandNames());

I get this error: Clasificación Random Forest: Layer error: Image.select: Pattern ‘nd’ did not match any bands.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Edit: I managed to correct the script.

var ps = ee.ImageCollection('projects/planet-nicfi/assets/basemaps/americas');
var geometry = ee.FeatureCollection("users/eobrien/cajamarca");
var image = ps.filter(ee.Filter.date('2022-06-01', '2022-06-30')).first();
var image_cut = image.clip(geometry);

Map.centerObject(geometry, 10);

// Calcular NDVI y NDWI
var ndvi = image_cut.normalizedDifference(['N', 'R']);
var ndwi = image_cut.normalizedDifference(['G', 'N']);

// Agregar banda ndvi y ndwi a la imagen
image_cut = image_cut.addBands(ndvi).addBands(ndwi).rename(['B', 'G', 'R', 'N', 'nd', 'nd_1']);

// Visualizar imágenes
var vis = {"bands":["R","G","B"],"min":64,"max":5454,"gamma":1.8};
Map.addLayer(image_cut, vis, 'Mosaic_CV_Planet_Jun_2022',true);

var vis1 = {"bands":["N","R","G"],"min":64,"max":5454,"gamma":1.8};
Map.addLayer(image_cut , vis1, 'Mosaic_NIR_Planet_Jun_2022',false);

// Visualizar imágenes NDVI y NDWI
var vis_ndvi = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.addLayer(ndvi, vis_ndvi, 'NDVI');

var vis_ndwi = {min: -1, max: 1, palette: ['navy', 'white', 'lime']};
Map.addLayer(ndwi, vis_ndwi, 'NDWI');

// Seeds
var seeds = ee.Algorithms.Image.Segmentation.seedGrid(15, 'hex');

// SNIC segmentation
var segment = ee.Algorithms.Image.Segmentation.SNIC({
  image: image_cut, 
  compactness: 50, 
  seeds: seeds
});

Map.addLayer(segment.select('clusters').randomVisualizer(), {}, 'Clusters', false);

Map.addLayer(segment, {bands: ['R_mean', 'G_mean', 'B_mean',], min: 170.02, max: 2086.81}, 'Planet Segment',false);

// Image for classification
var imageObject = segment.select(['R_mean', 'G_mean', 'B_mean', 'N_mean', 'nd_mean', 'nd_1_mean']);
var bandsName = imageObject.bandNames();

// Assign a 'class' property to each feature in the datasets
Plantacion = Plantacion.map(function(feature) {
  return feature.set('class', 1);
});
Agricola = Agricola.map(function(feature) {
  return feature.set('class', 2);
});
Sombras = Sombras.map(function(feature) {
  return feature.set('class', 3);
});
Agua = Agua.map(function(feature) {
  return feature.set('class', 4);
});
Urbano = Urbano.map(function(feature) {
  return feature.set('class', 5);
});
Otra_veg = Otra_veg.map(function(feature) {
  return feature.set('class', 6);
});
Mineria = Mineria.map(function(feature) {
  return feature.set('class', 7);
});

// Merge the feature collections into a single sample dataset
var sample = Plantacion.merge(Agricola).merge(Sombras).merge(Agua).merge(Urbano).merge(Otra_veg).merge(Mineria);

// Sample
var sample = Plantacion.merge(Agricola).merge(Sombras).merge(Agua).merge(Urbano).merge(Otra_veg).merge(Mineria);

// Print the size of the dataset before filtering
print('Sample size before filtering:', sample.size());

// Filter out any features without the 'class' property
sample = sample.filter(ee.Filter.notNull(['class']));

// Print the size of the dataset after filtering
print('Sample size after filtering:', sample.size());

// Trained
var trained = imageObject.sampleRegions({
  collection: sample,
  scale: 5,
  properties: ['class'],
  tileScale: 2
});

// Clasificación con Random Forest
var bandas = ['R_mean', 'G_mean', 'B_mean', 'N_mean', 'nd_mean', 'nd_1_mean'];
var clasificacion = ee.Classifier.smileRandomForest(100).train(trained, 'class', bandas);
var image1 = segment.select(bandas).classify(clasificacion);
Map.addLayer(image1, {min: 1, max: 7, palette: ['#79ff93', '#c2c116', '#d489ff', '#0000ff', '#6e7069','#009999','#ff00ff']}, 'Clasificación Random Forest', true);

print(image_cut.bandNames());

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.