公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.Classifier.smileKNN
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
创建空的 k-NN 分类器。
k 最近邻算法 (k-NN) 是一种通过邻居的多数表决对对象进行分类的方法,其中对象被分配到其 k 个最近邻中最常见的类别(k 是一个正整数,通常很小,通常为奇数)。
用法 | 返回 |
---|
ee.Classifier.smileKNN(k, searchMethod, metric) | 分类器 |
参数 | 类型 | 详细信息 |
---|
k | 整数,默认值:1 | 用于分类的邻数量。 |
searchMethod | 字符串,默认值:“AUTO” | 搜索方法。以下是有效值 [AUTO, LINEAR_SEARCH, KD_TREE, COVER_TREE]。
AUTO 将根据维度数量在 KD_TREE 和 COVER_TREE 之间进行选择。对于距离相同和概率值相同的情况,不同搜索方法的结果可能会有所不同。由于性能和结果可能会有所不同,请参阅 SMILE 的文档和其他文献。 |
metric | 字符串,默认值:“EUCLIDEAN” | 要使用的距离指标。注意:KD_TREE(以及低维度的 AUTO)不会使用所选的指标。选项包括:
“EUCLIDEAN” - 欧几里得距离。
'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
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eCreates a k-Nearest Neighbors (k-NN) classifier using the SMILE machine learning library within Google Earth Engine.\u003c/p\u003e\n"],["\u003cp\u003eThe classifier is trained using labeled data and can be applied to classify images based on the proximity of pixel values to known classes.\u003c/p\u003e\n"],["\u003cp\u003eUsers can customize the number of neighbors (k), search method, and distance metric for the k-NN algorithm.\u003c/p\u003e\n"],["\u003cp\u003eIncludes JavaScript and Python examples demonstrating classifier training and image classification using Landsat 8 data.\u003c/p\u003e\n"]]],[],null,["# ee.Classifier.smileKNN\n\nCreates an empty k-NN classifier.\n\n\u003cbr /\u003e\n\nThe k-nearest neighbor algorithm (k-NN) is a method for classifying objects by a majority vote of its neighbors, with the object being assigned to the class most common amongst its k nearest neighbors (k is a positive integer, typically small, typically odd).\n\n| Usage | Returns |\n|-----------------------------------------------------------------|------------|\n| `ee.Classifier.smileKNN(`*k* `, `*searchMethod* `, `*metric*`)` | Classifier |\n\n| Argument | Type | Details |\n|----------------|------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `k` | Integer, default: 1 | The number of neighbors for classification. |\n| `searchMethod` | String, default: \"AUTO\" | Search method. The following are valid \\[AUTO, LINEAR_SEARCH, KD_TREE, COVER_TREE\\]. AUTO will choose between KD_TREE and COVER_TREE depending on the dimension count. Results may vary between the different search methods for distance ties and probability values. Since performance and results may vary consult with SMILE's documentation and other literature. |\n| `metric` | String, default: \"EUCLIDEAN\" | The distance metric to use. NOTE: KD_TREE (and AUTO for low dimensions) will not use the metric selected. Options are: 'EUCLIDEAN' - Euclidean distance. 'MAHALANOBIS' - Mahalanobis distance. 'MANHATTAN' - Manhattan distance. 'BRAYCURTIS' - Bray-Curtis distance. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// Cloud masking for Landsat 8.\nfunction maskL8sr(image) {\n var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);\n var saturationMask = image.select('QA_RADSAT').eq(0);\n\n // Apply the scaling factors to the appropriate bands.\n var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);\n var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);\n\n // Replace the original bands with the scaled ones and apply the masks.\n return image.addBands(opticalBands, null, true)\n .addBands(thermalBands, null, true)\n .updateMask(qaMask)\n .updateMask(saturationMask);\n}\n\n// Map the function over one year of data.\nvar collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')\n .filterDate('2020-01-01', '2021-01-01')\n .map(maskL8sr);\n\n// Make a median composite.\nvar composite = collection.median();\n\n// Demonstration labels.\nvar labels = ee.FeatureCollection('projects/google/demo_landcover_labels')\n\n// Use these bands for classification.\nvar bands = ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7'];\n// The name of the property on the points storing the class label.\nvar classProperty = 'landcover';\n\n// Sample the composite to generate training data. Note that the\n// class label is stored in the 'landcover' property.\nvar training = composite.select(bands).sampleRegions(\n {collection: labels, properties: [classProperty], scale: 30});\n\n// Train a kNN classifier.\nvar classifier = ee.Classifier.smileKNN(5).train({\n features: training,\n classProperty: classProperty,\n});\n\n// Classify the composite.\nvar classified = composite.classify(classifier);\nMap.setCenter(-122.184, 37.796, 12);\nMap.addLayer(classified, {min: 0, max: 2, palette: ['red', 'green', 'blue']});\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Cloud masking for Landsat 8.\ndef mask_l8_sr(image):\n qa_mask = image.select('QA_PIXEL').bitwiseAnd(int('11111', 2)).eq(0)\n saturation_mask = image.select('QA_RADSAT').eq(0)\n\n # Apply the scaling factors to the appropriate bands.\n optical_bands = image.select('SR_B.').multiply(0.0000275).add(-0.2)\n thermal_bands = image.select('ST_B.*').multiply(0.00341802).add(149.0)\n\n # Replace the original bands with the scaled ones and apply the masks.\n return (\n image.addBands(optical_bands, None, True)\n .addBands(thermal_bands, None, True)\n .updateMask(qa_mask)\n .updateMask(saturation_mask)\n )\n\n\n# Map the function over one year of data.\ncollection = (\n ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')\n .filterDate('2020-01-01', '2021-01-01')\n .map(mask_l8_sr)\n)\n\n# Make a median composite.\ncomposite = collection.median()\n\n# Demonstration labels.\nlabels = ee.FeatureCollection('projects/google/demo_landcover_labels')\n\n# Use these bands for classification.\nbands = ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7']\n# The name of the property on the points storing the class label.\nclass_property = 'landcover'\n\n# Sample the composite to generate training data. Note that the\n# class label is stored in the 'landcover' property.\ntraining = composite.select(bands).sampleRegions(\n collection=labels, properties=[class_property], scale=30\n)\n\n# Train a kNN classifier.\nclassifier = ee.Classifier.smileKNN(5).train(\n features=training, classProperty=class_property\n)\n\n# Classify the composite.\nclassified = composite.classify(classifier)\n\nm = geemap.Map()\nm.set_center(-122.184, 37.796, 12)\nm.add_layer(\n classified, {'min': 0, 'max': 2, 'palette': ['red', 'green', 'blue']}\n)\nm\n```"]]