2

I am trying to calculate the probability of variable importance each bands for classification. Basically, I have 3 class but when I try to implement the probability of variable importance, the error statement is "Expected 2 classes for PROBABILITY, found 3". Could you please inform the problem in the code?

var geometry = ee.FeatureCollection("users/seneralkan77/water_4326");
Map.addLayer(geometry)
var aquatic = /* color: #ff0000 */ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.Point([-11.15696644,7.00857888]),
            {
              "watercover": 0,
              "system:index": "0"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.18116639,6.99607471]),
            {
              "watercover": 0,
              "system:index": "1"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.19274503,6.98715114]),
            {
              "watercover": 0,
              "system:index": "2"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.15285584 , 6.9951071]),
            {
              "watercover": 0,
              "system:index": "3"
            })]),
    fish_middle_class = /* color: #3b8b00 */ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.Point([-11.17854238,6.99824484]),
            {
              "watercover": 1,
              "system:index": "4"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.15719553,7.00526697]),
            {
              "watercover": 1,
              "system:index": "5"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.15692738,7.00521914]),
            {
              "watercover": 1,
              "system:index": "6"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.15856715,7.00432886]),
            {
              "watercover": 1,
              "system:index": "7"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.1668752,7.00175725]),
            {
              "watercover": 1,
              "system:index": "8"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.15510189,7.00024551]),
            {
              "watercover": 1,
              "system:index": "9"
            })]),
    fish_low_class = /* color: #0300ff */ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.Point([-11.15520286,7.00077292]),
            {
              "watercover": 2,
              "system:index": "10"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.14973369,6.9887299]),
            {
              "watercover": 2,
              "system:index": "11"
            }),
        ee.Feature(
            ee.Geometry.Point([-11.14880352,6.98930031]),
            {
              "watercover": 2,
              "system:index": "12"
            })]);


// Load the Sentinel-2
var sentinel2018 = ee.Image('COPERNICUS/S2/20180104T110431_20180104T111712_T29NKH')

// Merge the three geometry layers into a single FeatureCollection.
var newfc = aquatic.merge(fish_middle_class).merge(fish_low_class);

// Use these bands for classification.
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B11', 'B12'];
// The name of the property on the points storing the class label.
var classProperty = 'watercover'

// Sample the composite to generate training data.  Note that the
// class label is stored in the 'watercover' property.
var training = sentinel2018.select(bands).sampleRegions({
  collection: newfc,
  properties: [classProperty],
  scale: 30
});

// Train a RF classifier.
var classifier = ee.Classifier.smileRandomForest(10).setOutputMode('PROBABILITY').train(training,"watercover",bands);
// Print some info about the classifier (specific to RF).
//print('Random Forest, explained', classifier.explain());

var dict = classifier.explain();
print('Explain:',dict);

var variable_importance = ee.Feature(null, ee.Dictionary(dict).get('importance'));

var chart =
  ui.Chart.feature.byProperty(variable_importance)
    .setChartType('ColumnChart')
    .setOptions({
      title: 'Random Forest Variable Importance',
      legend: {position: 'none'},
      hAxis: {title: 'Bands'},
      vAxis: {title: 'Importance'}
    });


print(chart); 

2 Answers 2

2

Stumbled over the same problem and finally found out that in the description of the ee.Classifier.mode() another option is listed: MULTIPROBABILITY. Used this and it seemed to work, at least I was able to create the feature importances by adjusting my script to the example from Results of Variable Importance of RF Classifier in GEE.

Assuming an image called MYIMAGE with all_bands and some training_areas with a class attribute CLASSNUM, it looks like this:

var all_bands = ['b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8'];
var training_data = MYIMAGE.select(all_bands).sampleRegions({
    collection: training_areas,
    properties: ['CLASSNUM'],
    scale: 2
});
// Get probabilities
var probabilities = ee.Classifier.smileRandomForest({
    numberOfTrees: 10,
    variablesPerSplit: 2,
    seed: 42
}).setOutputMode('MULTIPROBABILITY').train({
    features: training_data,
    classProperty: 'CLASSNUM',
    inputProperties: all_bands
});
1

The docs state:

Not all classifier types support REGRESSION and PROBABILITY modes.

If you try your code with only two classes, it works. It must mean that ee.Classifier.smileRandomForest() only support PROBABILITY output mode when you have two classes, which pretty much is what the error message suggested.

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.