GEE中随机森林算法(变量重要性、决策树数量选择)

function NDVI(img) {
var nir = img.select("B8");
var red = img.select("B4");
var ndvi = nir.subtract(red).divide(nir.add(red));
return ndvi;
}

var s2img = sen2.filterBounds(xcqbuf).filterDate('2020-08-11','2020-08-12').filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',50))
.first()

                  // // .map(maskS2clouds)
                  // // .select(['B2', 'B3', 'B4','B5', 'B6', 'B7','B8', 'B10', 'B11','B12'])

                  // 
print(s2img)              
Map.addLayer(s2img,{bands: ['B4', 'B3', 'B2'],   min: 0,  max: 3000})
Map.addLayer(s2img,{bands: ['B4', 'B8', 'B11'],   min: 0,  max: 3000})
Map.addLayer(NDVI(s2img))
// Map.addLayer(NDVI(s2img.first()))
Map.setCenter(120.548,31.484)

//随机森林分类

var mndwi = s2img.normalizedDifference(['B3', 'B6']).rename('MNDWI');//计算MNDWI
var ndbi = s2img.normalizedDifference(['B4', 'B8A']).rename('NDBI');//计算NDBI
var ndvi = s2img.normalizedDifference(['B8', 'B4']).rename('NDVI');//计算NDVI

s2img=s2img
      .addBands(ndvi)
      .addBands(ndbi)
      .addBands(mndwi)


//选择样本
var classNames = zao.merge(water).merge(till).merge(hulu).merge(vege);

var bands = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7','B8','B8A', 'MNDWI','NDBI','NDVI'];

var training = s2img.select(bands).sampleRegions({
  collection: classNames,
  properties: ['landcover'],
  scale: 10
});


// random uniforms to the training dataset.
var withRandom = training.randomColumn('random');//样本点随机的排列
// 我们想保留一些数据进行测试,以避免模型过度拟合。
var split = 0.7; 
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));//筛选70%的样本作为训练样本
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));//筛选30%的样本作为测试样本


// 选择分类的属性
var classProperty = 'landcover';

//分类方法选择smileCart() randomForest() minimumDistance libsvm
var classifier = ee.Classifier.smileRandomForest(10).train({
  features: trainingPartition,
  classProperty: 'landcover',
  inputProperties: bands
});

var s2img1 = sen2.filterBounds(xcqbuf).filterDate('2020-08-16','2020-08-17').filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',10))
.first()
s2img=s2img
      .addBands(ndvi)
      .addBands(ndbi)
      .addBands(mndwi)
// Map.addLayer(s2img1,{bands: ['B4', 'B8', 'B11'],   min: 0,  max: 3000})
//分类
var classified = s2img.select(bands).classify(classifier);
//Display the classification map
//Map.centerObject(classNames, 11);
var dict = classifier.explain();
print('Explain:',dict);//变量重要性
//Map.addLayer(classified,{min: 0, max: 4, palette: ['00FFFF', '00FF33','FFFF33','CC33FF','#C0C0C0']},'classification');
             
//print(classified)

var test = testingPartition.classify(classifier);//运用测试样本分类,确定要进行函数运算的数据集以及函数

var confusionMatrix = test.errorMatrix('landcover', 'classification');//计算混淆矩阵
print('confusionMatrix',confusionMatrix);//面板上显示混淆矩阵
print('overall accuracy', confusionMatrix.accuracy());//面板上显示总体精度
print('kappa accuracy', confusionMatrix.kappa());//面板上显示kappa值


// var smooth_map = s2img
//                     .focal_mode({
//                       radius: 2, kernelType: 'octagon', units: 'pixels', iterations: 1
//                     })
//                     .mask(classified.gte(1))

// var crude_object_removal = classified
//                               .updateMask(classified.connectedPixelCount(2, false).gte(2))
//                               .unmask(smooth_map)


// Define a palette for the Land Use classification.
var palette = [
  'd63000', // forest(0)  
  '47f2ff', // water(1)  
  '42fd55', //  arableland (2) 
  'ebea4a',//  constructionland (4)
  'd0d3ff',
];

// Display the classification result and the input image.

Map.addLayer(classified, {min: 0, max:4, palette: palette}, 'Land Use Classification');

//Map.addLayer(result,{min: 0, max: 4, palette: ['00FFFF', '00FF33', 'FFFF33','CC33FF','#C0C0C0']},'paddies');

//决策树数量选择

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
}));


 

  • 1
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值