2

I am trying to do RF classification in GEE but i am getting an error

Classifier (Error) Classifier.train, argument 'classProperty': Invalid type. Expected type: String. Actual type: List. Actual value: [name, id]

The code which i have entered is as follows:

/Loading Sentinel 2 data and filtering by date and bounds
var Sentinel2Collection=ee.ImageCollection("COPERNICUS/S2_SR")
    .filterDate('2019-01-01','2019-01-28')
    .filterBounds(geometry)
    .sort('CLOUD_COVER', true)
  
    
// Filter the collection by the IMAGE_QUALITY property.
var filtered = Sentinel2Collection
  .filterMetadata('IMAGE_QUALITY', 'equals', 9)
  
var Sentinel_med = Sentinel2Collection.median()
var Sentinel_crop = Sentinel_med.clip(geometry)
    
print(Sentinel_crop)

Map.addLayer(Sentinel_crop, {bands: ['B8', 'B4', 'B3'], gamma: 2}, 'S2 Image');

//Collecting training data
//Merging the three geometry layers into a single featurecollection
var newfc= forest.merge(urban).merge(agriculture);
print(newfc, 'newfc')

// Select the bands for training
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7','B8','B8A'];

// Sample the input imagery to get a FeatureCollection of training data.
var training = Sentinel_crop.select(bands).sampleRegions({
  collection: newfc, 
  properties: ['name','id'], 
  scale: 20
});
print(training, 'training')

// Making a Random Forest classifier and training it.
var classifier= ee.Classifier.smileRandomForest(10).train({
  features: training, 
  classProperty: ['name','id'],
  inputProperties: bands
});
print(classifier)

//Classifying the input imagery
var classification= Sentinel_crop.select(bands).classify(classifier)

// Define a palette for the Land Use classification.
var palette = [
  'D3D3D3', // forest (0)  // red
  '0000FF', // urban (1)  // green
  '008000' //  agriculture (2) // blue
];
print(classification)
// Display the classification result and the input image.
Map.addLayer(classification, {min: 0, max: 2, palette: palette}, 'id');

1 Answer 1

1

In training the classifier, you provided a list of ['name', and 'id']. From the docs:

classProperty (String): The name of the property containing the class value. Each feature must have this property, and its value must be numeric.

You must provide a string as input. I am unable to run your code due to not having access to the forest featurecollection, but assuming that the name property encodes the label for each of your features, this should work:

// Making a Random Forest classifier and training it.
var classifier= ee.Classifier.smileRandomForest(10).train({
  features: training, 
  classProperty: 'name',
  inputProperties: bands
});

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.