天天看点

GEE学习笔记5:随机森林

在GEE中,随机森林的介绍如下图:

GEE学习笔记5:随机森林
/***********************已分好训练样本和实验样本******************************/
print("sam1_trainingPartition:",sam1_trainingPartition);
print("sam1_testingPartition:",sam1_testingPartition);

// 通过选取样本,把landcover属性赋予样本
//bands为数据集中波段组合
var trainingPartition = S1S2.select(bands).sampleRegions({
  collection: sam1_trainingPartition,
  properties: ['landcover'],
  scale: 10,
  tileScale:16
});

var testingPartition = S1S2.select(bands).sampleRegions({
  collection: sam1_testingPartition,
  properties: ['landcover'],
  scale: 10,
  tileScale:16
});
//先把棵树设置成10,后面会选择最优棵树
var trainedClassifier = ee.Classifier.smileRandomForest(10).train({
  features: trainingPartition,
  classProperty: 'landcover',
  inputProperties: bands
});
//对数据集进行分类
var class_img = S1S2.select(bands).classify(trainedClassifier).clip(roi);
           

选取随机森林的棵树

//选取森林棵树 
var numTrees = ee.List.sequence(5, 50, 5); 
var accuracies = numTrees.map(function(t)
{ 
  var classifier = ee.Classifier.smileRandomForest(t)
                    .train({
                     features: trainingPartition,
                     classProperty: 'landcover',
                     inputProperties: bands
                    });
  return testingPartition
      .classify(classifier)
      .errorMatrix('landcover', 'classification')
      .accuracy();
}); 
print(ui.Chart.array.values({
  array: ee.Array(accuracies),
  axis: 0,
  xLabels: numTrees
}));
           
GEE学习笔记5:随机森林

从图中可以看到当棵树为25,准确率最高,因此,可以把 ee.Classifier.smileRandomForest(10).train里面的参数设置成25,重新运行。

随机森林特征重要性,可以导出结果进行分析

//随机森林特征重要性
var dict = trainedClassifier.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);
           

hAxis: {title: ‘Bands’},

vAxis: {title: ‘Importance’}

});

print(chart);

继续阅读