在GEE中,随機森林的介紹如下圖:
/***********************已分好訓練樣本和實驗樣本******************************/
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
}));
從圖中可以看到當棵樹為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);