ee.Classifier.smileKNN

สร้างตัวแยกประเภท k-NN ที่ว่างเปล่า

อัลกอริทึม k-Nearest Neighbor (k-NN) เป็นวิธีการจัดประเภทออบเจ็กต์โดยใช้การโหวตส่วนใหญ่ของเพื่อนบ้าน โดยระบบจะกำหนดออบเจ็กต์ให้กับคลาสที่พบบ่อยที่สุดในบรรดา k-Nearest Neighbor (k เป็นจำนวนเต็มบวก โดยปกติจะมีค่าน้อยและเป็นคี่)

การใช้งานการคืนสินค้า
ee.Classifier.smileKNN(k, searchMethod, metric)ตัวแยกประเภท
อาร์กิวเมนต์ประเภทรายละเอียด
kจำนวนเต็ม ค่าเริ่มต้น: 1จำนวนเพื่อนบ้านสำหรับการจัดประเภท
searchMethodสตริง ค่าเริ่มต้น: "AUTO"วิธีการค้นหา [AUTO, LINEAR_SEARCH, KD_TREE, COVER_TREE] ที่ถูกต้องมีดังนี้

  • `AUTO` เลือกได้ระหว่าง KD_TREE กับ COVER_TREE ขึ้นอยู่กับจำนวนมิติข้อมูล หากฟีเจอร์มีมิติข้อมูลน้อยกว่า 10 รายการ ระบบจะใช้ KD_TREE
  • `LINEAR_SEARCH` มีประสิทธิภาพมากกว่าวิธีการแบ่งพาร์ติชันพื้นที่ (เช่น ต้นไม้ K-D) ในพื้นที่ที่มีมิติสูงกว่า
  • วิธีการค้นหาเพื่อนบ้านที่ใกล้ที่สุด `KD_TREE` โดยใช้โครงสร้างชุดข้อมูลการแบ่งพาร์ติชันพื้นที่เพื่อจัดระเบียบจุดในพื้นที่ k มิติ KD-tree ไม่เหมาะสำหรับการค้นหาจุดข้อมูลที่อยู่ใกล้ที่สุดอย่างมีประสิทธิภาพในพื้นที่ที่มีมิติสูง ตามกฎทั่วไป หากมิติข้อมูลคือ D จำนวนจุดในชุดข้อมูล N ควรเป็น N >> 2^D
  • `COVER_TREE` Cover tree เป็นโครงสร้างข้อมูลสำหรับการค้นหาเพื่อนบ้านที่ใกล้ที่สุดทั่วไป ซึ่งมีประสิทธิภาพเป็นพิเศษในพื้นที่ที่มีมิติภายในขนาดเล็ก

ผลลัพธ์อาจแตกต่างกันระหว่างวิธีการค้นหาที่ต่างกันสำหรับค่าการเสมอกันของระยะทางและค่าความน่าจะเป็น เนื่องจากประสิทธิภาพและผลลัพธ์อาจแตกต่างกัน โปรดอ่านเอกสารประกอบของ SMILE และเอกสารอื่นๆ

metricสตริง ค่าเริ่มต้น: "EUCLIDEAN"เมตริกระยะทางที่จะใช้ หมายเหตุ: KD_TREE (และ AUTO สำหรับมิติข้อมูลต่ำ) จะไม่ใช้เมตริกที่เลือก ตัวเลือกมีดังนี้
  • `EUCLIDEAN` - ระยะทางแบบยุคลิด
  • `MAHALANOBIS` - ระยะทาง Mahalanobis
  • `MANHATTAN` - ระยะทางแบบแมนฮัตตัน
  • `BRAYCURTIS` - ระยะทาง Bray-Curtis

ตัวอย่าง

ตัวแก้ไขโค้ด (JavaScript)

// Cloud masking for Landsat 8.
function maskL8sr(image) {
  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);

  // Apply the scaling factors to the appropriate bands.
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);

  // Replace the original bands with the scaled ones and apply the masks.
  return image.addBands(opticalBands, null, true)
      .addBands(thermalBands, null, true)
      .updateMask(qaMask)
      .updateMask(saturationMask);
}

// Map the function over one year of data.
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
                     .filterDate('2020-01-01', '2021-01-01')
                     .map(maskL8sr);

// Make a median composite.
var composite = collection.median();

// Demonstration labels.
var labels = ee.FeatureCollection('projects/google/demo_landcover_labels')

// Use these bands for classification.
var bands = ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7'];
// The name of the property on the points storing the class label.
var classProperty = 'landcover';

// Sample the composite to generate training data.  Note that the
// class label is stored in the 'landcover' property.
var training = composite.select(bands).sampleRegions(
    {collection: labels, properties: [classProperty], scale: 30});

// Train a kNN classifier.
var classifier = ee.Classifier.smileKNN(5).train({
  features: training,
  classProperty: classProperty,
});

// Classify the composite.
var classified = composite.classify(classifier);
Map.setCenter(-122.184, 37.796, 12);
Map.addLayer(classified, {min: 0, max: 2, palette: ['red', 'green', 'blue']});

การตั้งค่า Python

ดูข้อมูลเกี่ยวกับ Python API และการใช้ geemap เพื่อการพัฒนาแบบอินเทอร์แอกทีฟได้ที่หน้าสภาพแวดล้อม Python

import ee
import geemap.core as geemap

Colab (Python)

# Cloud masking for Landsat 8.
def mask_l8_sr(image):
  qa_mask = image.select('QA_PIXEL').bitwiseAnd(int('11111', 2)).eq(0)
  saturation_mask = image.select('QA_RADSAT').eq(0)

  # Apply the scaling factors to the appropriate bands.
  optical_bands = image.select('SR_B.').multiply(0.0000275).add(-0.2)
  thermal_bands = image.select('ST_B.*').multiply(0.00341802).add(149.0)

  # Replace the original bands with the scaled ones and apply the masks.
  return (
      image.addBands(optical_bands, None, True)
      .addBands(thermal_bands, None, True)
      .updateMask(qa_mask)
      .updateMask(saturation_mask)
  )


# Map the function over one year of data.
collection = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
    .filterDate('2020-01-01', '2021-01-01')
    .map(mask_l8_sr)
)

# Make a median composite.
composite = collection.median()

# Demonstration labels.
labels = ee.FeatureCollection('projects/google/demo_landcover_labels')

# Use these bands for classification.
bands = ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7']
# The name of the property on the points storing the class label.
class_property = 'landcover'

# Sample the composite to generate training data.  Note that the
# class label is stored in the 'landcover' property.
training = composite.select(bands).sampleRegions(
    collection=labels, properties=[class_property], scale=30
)

# Train a kNN classifier.
classifier = ee.Classifier.smileKNN(5).train(
    features=training, classProperty=class_property
)

# Classify the composite.
classified = composite.classify(classifier)

m = geemap.Map()
m.set_center(-122.184, 37.796, 12)
m.add_layer(
    classified, {'min': 0, 'max': 2, 'palette': ['red', 'green', 'blue']}
)
m