ee.Classifier.smileKNN

Tạo một bộ phân loại k-NN trống.

Thuật toán k-hàng xóm gần nhất (k-NN) là một phương pháp phân loại các đối tượng bằng cách bỏ phiếu đa số của các hàng xóm, trong đó đối tượng được gán cho lớp phổ biến nhất trong số k hàng xóm gần nhất (k là một số nguyên dương, thường nhỏ, thường là số lẻ).

Cách sử dụngGiá trị trả về
ee.Classifier.smileKNN(k, searchMethod, metric)Bộ phân loại
Đối sốLoạiThông tin chi tiết
kSố nguyên, mặc định: 1Số lượng hàng xóm để phân loại.
searchMethodChuỗi, mặc định: "AUTO"Phương thức tìm kiếm. Sau đây là các giá trị hợp lệ [AUTO, LINEAR_SEARCH, KD_TREE, COVER_TREE]:

  • `AUTO` Chọn giữa KD_TREE và COVER_TREE tuỳ thuộc vào số lượng chiều. Nếu các đặc điểm có ít hơn 10 chiều, thì KD_TREE sẽ được sử dụng.
  • `LINEAR_SEARCH` Hiệu suất cao hơn các phương pháp phân vùng không gian (chẳng hạn như cây K-D) trong không gian nhiều chiều.
  • `KD_TREE` Phương thức tìm kiếm hàng xóm gần nhất bằng cấu trúc tập dữ liệu phân vùng không gian để sắp xếp các điểm trong không gian k chiều. Cây KD không phù hợp để tìm hàng xóm gần nhất một cách hiệu quả trong không gian nhiều chiều. Theo quy tắc chung, nếu số chiều là D thì số điểm trong tập dữ liệu, N, phải là N >> 2^D.
  • `COVER_TREE` Cây bao phủ là một cấu trúc dữ liệu để tìm kiếm hàng xóm gần nhất chung, đặc biệt hiệu quả trong không gian có chiều nội tại nhỏ.

Kết quả có thể khác nhau giữa các phương thức tìm kiếm khác nhau đối với các giá trị khoảng cách và xác suất bằng nhau. Vì hiệu suất và kết quả có thể khác nhau, hãy tham khảo tài liệu và các tài liệu khác của SMILE.

metricChuỗi, mặc định: "EUCLIDEAN"Chỉ số khoảng cách cần sử dụng. LƯU Ý: KD_TREE (và AUTO cho các chiều thấp) sẽ không sử dụng chỉ số đã chọn. Các lựa chọn là:
  • `EUCLIDEAN` – Khoảng cách Euclide.
  • `MAHALANOBIS` – Khoảng cách Mahalanobis.
  • `MANHATTAN` – Khoảng cách Manhattan.
  • `BRAYCURTIS` – Khoảng cách Bray-Curtis.

Ví dụ

Trình soạn thảo mã (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']});

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về Python API và cách sử dụng geemap cho quá trình phát triển có tính tương tác.

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