GEE学习笔记 八十六:分类中的特征重要性分析

    宅在家里不能回去工作,还是学习一下GEE吧!借用网友绘制的图片,加油,一切都会好起来的!

GEE学习笔记 八十六:分类中的特征重要性分析_第1张图片

 

    之前在GEE中做随机森林分类时候,很多人都在问如何做特征重要性分析?但是在GEE之前并没有相关API可以做特征重要性分析,最新的API更新后GEE也可以做特征重要性分析了。

 

1、目前常用的包含特征重要信息分析的分类方法包括:

(1)决策树

ee.Classifier.smileCart(maxNodes, minLeafPopulation)

GEE学习笔记 八十六:分类中的特征重要性分析_第2张图片

(2)随机森林

ee.Classifier.smileRandomForest(numberOfTrees, variablesPerSplit, minLeafPopulation, bagFraction, maxNodes, seed)

GEE学习笔记 八十六:分类中的特征重要性分析_第3张图片

    这两个方法和之前的分类决策树和随机森林分类方法参数非常类似,只不过调用这两个定义方法在使用explain()方法就可以得到每个分类特征重要性的信息。

 

2、得到特征重要性的API方法:

GEE学习笔记 八十六:分类中的特征重要性分析_第4张图片

    也就是之前在分类器中的方法explain,这个方法是最开始就有的,只不过是最近GEE官方加入了特征重要性的分析返回信息,下面通过一个具体例子说明一下如何使用以及具体输出。

 

具体代码:

var roi =

    /* color: #d63000 */

    /* shown: false */

    /* displayProperties: [

      {

        "type": "rectangle"

      }

    ] */

    ee.Geometry.Polygon(

        [[[114.23790821489501, 36.43657462800738],

          [114.23790821489501, 36.29834769127675],

          [114.49265369829345, 36.29834769127675],

          [114.49265369829345, 36.43657462800738]]], null, false),

    crop =

    /* color: #98ff00 */

    /* shown: false */

    ee.FeatureCollection(

        [ee.Feature(

            ee.Geometry.Point([114.31343922075439, 36.356156419842804]),

            {

              "type": 0,

              "system:index": "0"

            }),

        ee.Feature(

            ee.Geometry.Point([114.31056389269042, 36.35499859187555]),

            {

              "type": 0,

              "system:index": "1"

            }),

        ee.Feature(

            ee.Geometry.Point([114.31236633714843, 36.3529248270982]),

            {

              "type": 0,

              "system:index": "2"

            }),

        ee.Feature(

            ee.Geometry.Point([114.32159313615966, 36.35461840580203]),

            {

              "type": 0,

              "system:index": "3"

            }),

        ee.Feature(

            ee.Geometry.Point([114.32348141130615, 36.35638107103549]),

            {

              "type": 0,

              "system:index": "4"

            }),

        ee.Feature(

            ee.Geometry.Point([114.32936081346679, 36.35589720612248]),

            {

              "type": 0,

              "system:index": "5"

            }),

        ee.Feature(

            ee.Geometry.Point([114.33515438493896, 36.35565527253804]),

            {

              "type": 0,

              "system:index": "6"

            }),

        ee.Feature(

            ee.Geometry.Point([114.33545479234863, 36.35855842592195]),

            {

              "type": 0,

              "system:index": "7"

            }),

        ee.Feature(

            ee.Geometry.Point([114.29468521532226, 36.35679580999553]),

            {

              "type": 0,

              "system:index": "8"

            }),

        ee.Feature(

            ee.Geometry.Point([114.29979214128662, 36.34922647614173]),

            {

              "type": 0,

              "system:index": "9"

            }),

        ee.Feature(

            ee.Geometry.Point([114.28871998247314, 36.3387180531404]),

            {

              "type": 0,

              "system:index": "10"

            }),

        ee.Feature(

            ee.Geometry.Point([114.28279766496826, 36.33833778756726]),

            {

              "type": 0,

              "system:index": "11"

            }),

        ee.Feature(

            ee.Geometry.Point([114.39553627428222, 36.3299369018726]),

            {

              "type": 0,

              "system:index": "12"

            }),

        ee.Feature(

            ee.Geometry.Point([114.39725288805175, 36.32997147527504]),

            {

              "type": 0,

              "system:index": "13"

            }),

        ee.Feature(

            ee.Geometry.Point([114.39712414201904, 36.32796619257241]),

            {

              "type": 0,

              "system:index": "14"

            }),

        ee.Feature(

            ee.Geometry.Point([114.39918407854248, 36.3291762831369]),

            {

              "type": 0,

              "system:index": "15"

            }),

        ee.Feature(

            ee.Geometry.Point([114.39845451769042, 36.3274475765775]),

            {

              "type": 0,

              "system:index": "16"

            }),

        ee.Feature(

            ee.Geometry.Point([114.40467724260498, 36.32900341420687]),

            {

              "type": 0,

              "system:index": "17"

            }),

        ee.Feature(

            ee.Geometry.Point([114.40480598863769, 36.32734385296429]),

            {

              "type": 0,

              "system:index": "18"

            }),

        ee.Feature(

            ee.Geometry.Point([114.38210377153564, 36.33612529650057]),

            {

              "type": 0,

              "system:index": "19"

            }),

        ee.Feature(

            ee.Geometry.Point([114.38034424242187, 36.336194437797644]),

            {

              "type": 0,

              "system:index": "20"

            }),

        ee.Feature(

            ee.Geometry.Point([114.38098797258544, 36.34041194086619]),

            {

              "type": 0,

              "system:index": "21"

            }),

        ee.Feature(

            ee.Geometry.Point([114.38785442766357, 36.337473540724005]),

            {

              "type": 0,

              "system:index": "22"

            }),

        ee.Feature(

            ee.Geometry.Point([114.38532242235351, 36.34227863160822]),

            {

              "type": 0,

              "system:index": "23"

            })]),

    nocrop =

    /* color: #0b4a8b */

    /* shown: false */

    ee.FeatureCollection(

        [ee.Feature(

            ee.Geometry.Point([114.36832794603515, 36.344663782445146]),

            {

              "type": 1,

              "system:index": "0"

            }),

        ee.Feature(

            ee.Geometry.Point([114.37463650163818, 36.345078583829164]),

            {

              "type": 1,

              "system:index": "1"

            }),

        ee.Feature(

            ee.Geometry.Point([114.37742599901367, 36.34576991455905]),

            {

              "type": 1,

              "system:index": "2"

            }),

        ee.Feature(

            ee.Geometry.Point([114.378026813833, 36.34860430638123]),

            {

              "type": 1,

              "system:index": "3"

            }),

        ee.Feature(

            ee.Geometry.Point([114.37386402544189, 36.35009059256129]),

            {

              "type": 1,

              "system:index": "4"

            }),

        ee.Feature(

            ee.Geometry.Point([114.36987289842773, 36.352198996374725]),

            {

              "type": 1,

              "system:index": "5"

            }),

        ee.Feature(

            ee.Geometry.Point([114.37695393022705, 36.351887923993054]),

            {

              "type": 1,

              "system:index": "6"

            }),

        ee.Feature(

            ee.Geometry.Point([114.3797005122583, 36.35392715363849]),

            {

              "type": 1,

              "system:index": "7"

            }),

        ee.Feature(

            ee.Geometry.Point([114.37352070268798, 36.35627738595022]),

            {

              "type": 1,

              "system:index": "8"

            }),

        ee.Feature(

            ee.Geometry.Point([114.31785950121093, 36.34438724696162]),

            {

              "type": 1,

              "system:index": "9"

            }),

        ee.Feature(

            ee.Geometry.Point([114.31657204088378, 36.34345393245458]),

            {

              "type": 1,

              "system:index": "10"

            }),

        ee.Feature(

            ee.Geometry.Point([114.34390911516357, 36.33512274079625]),

            {

              "type": 1,

              "system:index": "11"

            }),

        ee.Feature(

            ee.Geometry.Point([114.35094723161865, 36.33723154988948]),

            {

              "type": 1,

              "system:index": "12"

            }),

        ee.Feature(

            ee.Geometry.Point([114.35309299883056, 36.337058698833104]),

            {

              "type": 1,

              "system:index": "13"

            }),

        ee.Feature(

            ee.Geometry.Point([114.35296425279785, 36.33501902740074]),

            {

              "type": 1,

              "system:index": "14"

            }),

        ee.Feature(

            ee.Geometry.Point([114.41952058729305, 36.35091929703152]),

            {

              "type": 1,

              "system:index": "15"

            }),

        ee.Feature(

            ee.Geometry.Point([114.41966006216182, 36.3516321782695]),

            {

              "type": 1,

              "system:index": "16"

            }),

        ee.Feature(

            ee.Geometry.Point([114.41982099470272, 36.35252219053154]),

            {

              "type": 1,

              "system:index": "17"

            }),

        ee.Feature(

            ee.Geometry.Point([114.42009458002224, 36.35383558809808]),

            {

              "type": 1,

              "system:index": "18"

            }),

        ee.Feature(

            ee.Geometry.Point([114.42027697023525, 36.35499343336394]),

            {

              "type": 1,

              "system:index": "19"

            }),

        ee.Feature(

            ee.Geometry.Point([114.42028233465328, 36.35609509850472]),

            {

              "type": 1,

              "system:index": "20"

            }),

        ee.Feature(

            ee.Geometry.Point([114.42228326257839, 36.35603461533285]),

            {

              "type": 1,

              "system:index": "21"

            }),

        ee.Feature(

            ee.Geometry.Point([114.42357072290554, 36.35606053669799]),

            {

              "type": 1,

              "system:index": "22"

            })]),

    water =

    /* color: #ffc82d */

    /* shown: false */

    ee.FeatureCollection(

        [ee.Feature(

            ee.Geometry.Point([114.3321717685144, 36.3306024371752]),

            {

              "type": 2,

              "system:index": "0"

            }),

        ee.Feature(

            ee.Geometry.Point([114.33210739549804, 36.333592954210076]),

            {

              "type": 2,

              "system:index": "1"

            }),

        ee.Feature(

            ee.Geometry.Point([114.28854832109619, 36.41266166922327]),

            {

              "type": 2,

              "system:index": "2"

            }),

        ee.Feature(

            ee.Geometry.Point([114.29610142168212, 36.40879346635716]),

            {

              "type": 2,

              "system:index": "3"

            }),

        ee.Feature(

            ee.Geometry.Point([114.28580173906494, 36.40547771059803]),

            {

              "type": 2,

              "system:index": "4"

            }),

        ee.Feature(

            ee.Geometry.Point([114.29541477617431, 36.40160915003326]),

            {

              "type": 2,

              "system:index": "5"

            }),

        ee.Feature(

            ee.Geometry.Point([114.3057144587915, 36.40547771059803]),

            {

              "type": 2,

              "system:index": "6"

            }),

        ee.Feature(

            ee.Geometry.Point([114.29953464922119, 36.39829308768715]),

            {

              "type": 2,

              "system:index": "7"

            }),

        ee.Feature(

            ee.Geometry.Point([114.39435610231533, 36.3617846267207]),

            {

              "type": 2,

              "system:index": "8"

            }),

        ee.Feature(

            ee.Geometry.Point([114.39429172929897, 36.361179837099506]),

            {

              "type": 2,

              "system:index": "9"

            }),

        ee.Feature(

            ee.Geometry.Point([114.38845524248256, 36.35940000037548]),

            {

              "type": 2,

              "system:index": "10"

            }),

        ee.Feature(

            ee.Geometry.Point([114.38152441438808, 36.35701530096524]),

            {

              "type": 2,

              "system:index": "11"

            }),

        ee.Feature(

            ee.Geometry.Point([114.38133129533901, 36.35653143999478]),

            {

              "type": 2,

              "system:index": "12"

            }),

        ee.Feature(

            ee.Geometry.Point([114.39965614732876, 36.36371992192421]),

            {

              "type": 2,

              "system:index": "13"

            }),

        ee.Feature(

            ee.Geometry.Point([114.40693029817714, 36.3668473954689]),

            {

              "type": 2,

              "system:index": "14"

            }),

        ee.Feature(

            ee.Geometry.Point([114.41094288286342, 36.36890351124372]),

            {

              "type": 2,

              "system:index": "15"

            }),

        ee.Feature(

            ee.Geometry.Point([114.40238127168789, 36.36840244602017]),

            {

              "type": 2,

              "system:index": "16"

            })]);



//监督分类

Map.centerObject(roi, 11);

Map.addLayer(roi, {color: "red"}, "roi", false);

var sampleData = crop.merge(nocrop).merge(water);



//Landsat8 SR数据去云

function rmCloud(image) {

  var cloudShadowBitMask = (1 << 3);

  var cloudsBitMask = (1 << 5);

  var qa = image.select("pixel_qa");

  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)

                 .and(qa.bitwiseAnd(cloudsBitMask).eq(0));

  return image.updateMask(mask);

}



//缩放

function scaleImage(image) {

  var time_start = image.get("system:time_start");

  image = image.multiply(0.0001);

  image = image.set("system:time_start", time_start);

  return image;

}



//NDVI

function NDVI(image) {

  return image.addBands(

    image.normalizedDifference(["B5", "B4"])

         .rename("NDVI"));

}

//NDWI

function NDWI(image) {

  return image.addBands(

    image.normalizedDifference(["B3", "B5"])

         .rename("NDWI"));

}

//NDBI

function NDBI(image) {

  return image.addBands(

    image.normalizedDifference(["B6", "B5"])

         .rename("NDBI"));

}

var l8Col = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")

              .filterBounds(roi)

              .filterDate("2018-1-1", "2019-1-1")

              .map(rmCloud)

              .map(scaleImage)

              .map(NDVI)

              .map(NDWI)

              .map(NDBI);

var bands = [

  "B1", "B2", "B3", "B4", "B5", "B6", "B7",

  "NDBI", "NDWI", "NDVI"

];

var l8Image = l8Col.median()

                   .clip(roi)

                   .select(bands);

var rgbVisParam = {

  min: 0,

  max: 0.3,

  bands: ["B4", "B3", "B2"]

};

Map.addLayer(l8Image, rgbVisParam, "l8Image");

//生成监督分类训练使用的样本数据

var training = l8Image.sampleRegions({

  collection: sampleData,

  properties: ["type"],

  scale: 30

});

//初始化分类器

// var classifier = ee.Classifier.smileCart().train({

//   features: training,

//   classProperty: "type",

//   inputProperties: bands

// });

var classifier = ee.Classifier.smileRandomForest(10).train({

  features: training,

  classProperty: "type",

  inputProperties: bands

});

print(classifier.explain());


//影像数据调用classify利用训练数据训练得到分类结果

var classified = l8Image.classify(classifier);

//训练结果的混淆矩阵

var trainAccuracy = classifier.confusionMatrix();

Map.addLayer(classified, {min:0, max:2, palette: ["green", "orange", "blue"]}, "classify");

 

代码分析:

(1)这段代码做的是一个简单的监督分类,通过自己标注的样本将影像分成3大类(作物、非作物和水),样本由于是我做测试随机标注的,所以测试精度和分类准确度不高。

(2)和之前的分类代码相比较,这里只是更换了分类方法,比如随机森林我使用的是smileRandomForest方法,参数和之前的方法的参数类似,这里只是简单设置了树的个数为10。

(3)然后对分类器调用explain方法就可以查看特征重要性信息,输出结果如下面所示。

 

运行结果:

    比如使用ee.Classifier.smileRandomForest()方法做的分类结果,使用explain后的信息如下图:

GEE学习笔记 八十六:分类中的特征重要性分析_第5张图片

 


大家如果有问题需要交流或者有项目需要合作,可以微信联系我,加微信好友请留言加上“GEE”。

知乎专栏:https://zhuanlan.zhihu.com/c_123993183

CSDN:https://blog.csdn.net/shi_weihappy

微信号:shi_weihappy

GEE学习笔记 八十六:分类中的特征重要性分析_第6张图片

你可能感兴趣的:(GEE学习笔记 八十六:分类中的特征重要性分析)