当前位置:   article > 正文

GEE学习笔记 八十六:分类中的特征重要性分析_gee随机森林特征重要性变成0到1

gee随机森林特征重要性变成0到1

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

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

(1)决策树

ee.Classifier.smileCart(maxNodes, minLeafPopulation)

(2)随机森林

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

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

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

 

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

具体代码:

  1. var roi =

  2.     /* color: #d63000 */

  3.     /* shown: false */

  4.     /* displayProperties: [

  5.       {

  6.         "type": "rectangle"

  7.       }

  8.     ] */

  9.     ee.Geometry.Polygon(

  10.         [[[114.23790821489501, 36.43657462800738],

  11.           [114.23790821489501, 36.29834769127675],

  12.           [114.49265369829345, 36.29834769127675],

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

  14.     crop =

  15.     /* color: #98ff00 */

  16.     /* shown: false */

  17.     ee.FeatureCollection(

  18.         [ee.Feature(

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

  20.             {

  21.               "type": 0,

  22.               "system:index": "0"

  23.             }),

  24.         ee.Feature(

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

  26.             {

  27.               "type": 0,

  28.               "system:index": "1"

  29.             }),

  30.         ee.Feature(

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

  32.             {

  33.               "type": 0,

  34.               "system:index": "2"

  35.             }),

  36.         ee.Feature(

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

  38.             {

  39.               "type": 0,

  40.               "system:index": "3"

  41.             }),

  42.         ee.Feature(

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

  44.             {

  45.               "type": 0,

  46.               "system:index": "4"

  47.             }),

  48.         ee.Feature(

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

  50.             {

  51.               "type": 0,

  52.               "system:index": "5"

  53.             }),

  54.         ee.Feature(

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

  56.             {

  57.               "type": 0,

  58.               "system:index": "6"

  59.             }),

  60.         ee.Feature(

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

  62.             {

  63.               "type": 0,

  64.               "system:index": "7"

  65.             }),

  66.         ee.Feature(

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

  68.             {

  69.               "type": 0,

  70.               "system:index": "8"

  71.             }),

  72.         ee.Feature(

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

  74.             {

  75.               "type": 0,

  76.               "system:index": "9"

  77.             }),

  78.         ee.Feature(

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

  80.             {

  81.               "type": 0,

  82.               "system:index": "10"

  83.             }),

  84.         ee.Feature(

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

  86.             {

  87.               "type": 0,

  88.               "system:index": "11"

  89.             }),

  90.         ee.Feature(

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

  92.             {

  93.               "type": 0,

  94.               "system:index": "12"

  95.             }),

  96.         ee.Feature(

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

  98.             {

  99.               "type": 0,

  100.               "system:index": "13"

  101.             }),

  102.         ee.Feature(

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

  104.             {

  105.               "type": 0,

  106.               "system:index": "14"

  107.             }),

  108.         ee.Feature(

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

  110.             {

  111.               "type": 0,

  112.               "system:index": "15"

  113.             }),

  114.         ee.Feature(

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

  116.             {

  117.               "type": 0,

  118.               "system:index": "16"

  119.             }),

  120.         ee.Feature(

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

  122.             {

  123.               "type": 0,

  124.               "system:index": "17"

  125.             }),

  126.         ee.Feature(

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

  128.             {

  129.               "type": 0,

  130.               "system:index": "18"

  131.             }),

  132.         ee.Feature(

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

  134.             {

  135.               "type": 0,

  136.               "system:index": "19"

  137.             }),

  138.         ee.Feature(

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

  140.             {

  141.               "type": 0,

  142.               "system:index": "20"

  143.             }),

  144.         ee.Feature(

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

  146.             {

  147.               "type": 0,

  148.               "system:index": "21"

  149.             }),

  150.         ee.Feature(

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

  152.             {

  153.               "type": 0,

  154.               "system:index": "22"

  155.             }),

  156.         ee.Feature(

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

  158.             {

  159.               "type": 0,

  160.               "system:index": "23"

  161.             })]),

  162.     nocrop =

  163.     /* color: #0b4a8b */

  164.     /* shown: false */

  165.     ee.FeatureCollection(

  166.         [ee.Feature(

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

  168.             {

  169.               "type": 1,

  170.               "system:index": "0"

  171.             }),

  172.         ee.Feature(

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

  174.             {

  175.               "type": 1,

  176.               "system:index": "1"

  177.             }),

  178.         ee.Feature(

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

  180.             {

  181.               "type": 1,

  182.               "system:index": "2"

  183.             }),

  184.         ee.Feature(

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

  186.             {

  187.               "type": 1,

  188.               "system:index": "3"

  189.             }),

  190.         ee.Feature(

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

  192.             {

  193.               "type": 1,

  194.               "system:index": "4"

  195.             }),

  196.         ee.Feature(

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

  198.             {

  199.               "type": 1,

  200.               "system:index": "5"

  201.             }),

  202.         ee.Feature(

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

  204.             {

  205.               "type": 1,

  206.               "system:index": "6"

  207.             }),

  208.         ee.Feature(

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

  210.             {

  211.               "type": 1,

  212.               "system:index": "7"

  213.             }),

  214.         ee.Feature(

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

  216.             {

  217.               "type": 1,

  218.               "system:index": "8"

  219.             }),

  220.         ee.Feature(

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

  222.             {

  223.               "type": 1,

  224.               "system:index": "9"

  225.             }),

  226.         ee.Feature(

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

  228.             {

  229.               "type": 1,

  230.               "system:index": "10"

  231.             }),

  232.         ee.Feature(

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

  234.             {

  235.               "type": 1,

  236.               "system:index": "11"

  237.             }),

  238.         ee.Feature(

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

  240.             {

  241.               "type": 1,

  242.               "system:index": "12"

  243.             }),

  244.         ee.Feature(

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

  246.             {

  247.               "type": 1,

  248.               "system:index": "13"

  249.             }),

  250.         ee.Feature(

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

  252.             {

  253.               "type": 1,

  254.               "system:index": "14"

  255.             }),

  256.         ee.Feature(

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

  258.             {

  259.               "type": 1,

  260.               "system:index": "15"

  261.             }),

  262.         ee.Feature(

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

  264.             {

  265.               "type": 1,

  266.               "system:index": "16"

  267.             }),

  268.         ee.Feature(

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

  270.             {

  271.               "type": 1,

  272.               "system:index": "17"

  273.             }),

  274.         ee.Feature(

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

  276.             {

  277.               "type": 1,

  278.               "system:index": "18"

  279.             }),

  280.         ee.Feature(

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

  282.             {

  283.               "type": 1,

  284.               "system:index": "19"

  285.             }),

  286.         ee.Feature(

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

  288.             {

  289.               "type": 1,

  290.               "system:index": "20"

  291.             }),

  292.         ee.Feature(

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

  294.             {

  295.               "type": 1,

  296.               "system:index": "21"

  297.             }),

  298.         ee.Feature(

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

  300.             {

  301.               "type": 1,

  302.               "system:index": "22"

  303.             })]),

  304.     water =

  305.     /* color: #ffc82d */

  306.     /* shown: false */

  307.     ee.FeatureCollection(

  308.         [ee.Feature(

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

  310.             {

  311.               "type": 2,

  312.               "system:index": "0"

  313.             }),

  314.         ee.Feature(

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

  316.             {

  317.               "type": 2,

  318.               "system:index": "1"

  319.             }),

  320.         ee.Feature(

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

  322.             {

  323.               "type": 2,

  324.               "system:index": "2"

  325.             }),

  326.         ee.Feature(

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

  328.             {

  329.               "type": 2,

  330.               "system:index": "3"

  331.             }),

  332.         ee.Feature(

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

  334.             {

  335.               "type": 2,

  336.               "system:index": "4"

  337.             }),

  338.         ee.Feature(

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

  340.             {

  341.               "type": 2,

  342.               "system:index": "5"

  343.             }),

  344.         ee.Feature(

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

  346.             {

  347.               "type": 2,

  348.               "system:index": "6"

  349.             }),

  350.         ee.Feature(

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

  352.             {

  353.               "type": 2,

  354.               "system:index": "7"

  355.             }),

  356.         ee.Feature(

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

  358.             {

  359.               "type": 2,

  360.               "system:index": "8"

  361.             }),

  362.         ee.Feature(

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

  364.             {

  365.               "type": 2,

  366.               "system:index": "9"

  367.             }),

  368.         ee.Feature(

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

  370.             {

  371.               "type": 2,

  372.               "system:index": "10"

  373.             }),

  374.         ee.Feature(

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

  376.             {

  377.               "type": 2,

  378.               "system:index": "11"

  379.             }),

  380.         ee.Feature(

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

  382.             {

  383.               "type": 2,

  384.               "system:index": "12"

  385.             }),

  386.         ee.Feature(

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

  388.             {

  389.               "type": 2,

  390.               "system:index": "13"

  391.             }),

  392.         ee.Feature(

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

  394.             {

  395.               "type": 2,

  396.               "system:index": "14"

  397.             }),

  398.         ee.Feature(

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

  400.             {

  401.               "type": 2,

  402.               "system:index": "15"

  403.             }),

  404.         ee.Feature(

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

  406.             {

  407.               "type": 2,

  408.               "system:index": "16"

  409.             })]);

  410. //监督分类

  411. Map.centerObject(roi, 11);

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

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

  414. //Landsat8 SR数据去云

  415. function rmCloud(image) {

  416.   var cloudShadowBitMask = (1 << 3);

  417.   var cloudsBitMask = (1 << 5);

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

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

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

  421.   return image.updateMask(mask);

  422. }

  423. //缩放

  424. function scaleImage(image) {

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

  426.   image = image.multiply(0.0001);

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

  428.   return image;

  429. }

  430. //NDVI

  431. function NDVI(image) {

  432.   return image.addBands(

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

  434.          .rename("NDVI"));

  435. }

  436. //NDWI

  437. function NDWI(image) {

  438.   return image.addBands(

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

  440.          .rename("NDWI"));

  441. }

  442. //NDBI

  443. function NDBI(image) {

  444.   return image.addBands(

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

  446.          .rename("NDBI"));

  447. }

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

  449.               .filterBounds(roi)

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

  451.               .map(rmCloud)

  452.               .map(scaleImage)

  453.               .map(NDVI)

  454.               .map(NDWI)

  455.               .map(NDBI);

  456. var bands = [

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

  458.   "NDBI", "NDWI", "NDVI"

  459. ];

  460. var l8Image = l8Col.median()

  461.                    .clip(roi)

  462.                    .select(bands);

  463. var rgbVisParam = {

  464.   min: 0,

  465.   max: 0.3,

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

  467. };

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

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

  470. var training = l8Image.sampleRegions({

  471.   collection: sampleData,

  472.   properties: ["type"],

  473.   scale: 30

  474. });

  475. //初始化分类器

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

  477. //   features: training,

  478. //   classProperty: "type",

  479. //   inputProperties: bands

  480. // });

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

  482.   features: training,

  483.   classProperty: "type",

  484.   inputProperties: bands

  485. });

  486. print(classifier.explain());

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

  488. var classified = l8Image.classify(classifier);

  489. //训练结果的混淆矩阵

  490. var trainAccuracy = classifier.confusionMatrix();

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

代码分析:

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

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

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

运行结果:

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

来源请引用:地理遥感生态网科学数据注册与出版系统。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/370288
推荐阅读
相关标签
  

闽ICP备14008679号