Search code examples
random-forestgoogle-earth-engine

GEE - Random Forest: how to calculate % variable importance?


I'm trying to calculate the importance (in percentage) of each variable in my model (using smileRandomForest) in GEE.

var RFmodel = ee.Classifier.smileRandomForest(1000).train(trainingData, 'classID', predictionBands);

var var_imp = ee.Feature(null, ee.Dictionary(RFmodel.explain()).get('importance'));

In the example above, "var_imp" is a feature that has "importance" as a property. To calculate importance as %, I'm assuming I'll need to do something like:

Importance (%) = (variable importance value)/(total sum of all importance variables) * 100

Can someone help me to write a function for this? I'm relatively new to GEE and have no idea where to start. I've tried using aggregate_sum() at least to sum all variables, but "var_imp" isn't a FeatureCollection so it doesn't work.


Solution

  • You can work directly with the dictionary. Extract a list of the values and reduce it with a sum reducer to get the total importance. Then you can map over the importance dictionary and calculate the percentage of each band.

    For future questions, please include a link to the code editor (use Get Link) and make sure all used assets are shared. It makes it easier to help you, increasing your chance of getting answers to your questions.

    var importance = ee.Dictionary(
      classifier.explain().get('importance')
    )
    var totalImportance = importance.values().reduce(ee.Reducer.sum())
    var importancePercentage = importance.map(function (band, importance) {
      return ee.Number(importance).divide(totalImportance).multiply(100)
    })
    

    https://code.earthengine.google.com/bd63aa319a37516d924a6d8c391ab076