公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.Classifier.smileRandomForest
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
创建空的随机森林分类器。
用法 | 返回 |
---|
ee.Classifier.smileRandomForest(numberOfTrees, variablesPerSplit, minLeafPopulation, bagFraction, maxNodes, seed) | 分类器 |
参数 | 类型 | 详细信息 |
---|
numberOfTrees | 整数 | 要创建的决策树的数量。 |
variablesPerSplit | 整数,默认值:null | 每个拆分的变量数。如果未指定,则使用变量数量的平方根。 |
minLeafPopulation | 整数,默认值:1 | 仅创建训练集包含至少这么多点的节点。 |
bagFraction | 浮点数,默认值:0.5 | 每棵树的抽样输入比例。 |
maxNodes | 整数,默认值:null | 每棵树中的叶节点的最大数量。如果未指定,则默认为无限制。 |
seed | 整数,默认值:0 | 随机种子。 |
示例
代码编辑器 (JavaScript)
// A Sentinel-2 surface reflectance image, reflectance bands selected,
// serves as the source for training and prediction in this contrived example.
var img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')
.select('B.*');
// ESA WorldCover land cover map, used as label source in classifier training.
var lc = ee.Image('ESA/WorldCover/v100/2020');
// Remap the land cover class values to a 0-based sequential series.
var classValues = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100];
var remapValues = ee.List.sequence(0, 10);
var label = 'lc';
lc = lc.remap(classValues, remapValues).rename(label).toByte();
// Add land cover as a band of the reflectance image and sample 100 pixels at
// 10 m scale from each land cover class within a region of interest.
var roi = ee.Geometry.Rectangle(-122.347, 37.743, -122.024, 37.838);
var sample = img.addBands(lc).stratifiedSample({
numPoints: 100,
classBand: label,
region: roi,
scale: 10,
geometries: true
});
// Add a random value field to the sample and use it to approximately split 80%
// of the features into a training set and 20% into a validation set.
sample = sample.randomColumn();
var trainingSample = sample.filter('random <= 0.8');
var validationSample = sample.filter('random > 0.8');
// Train a 10-tree random forest classifier from the training sample.
var trainedClassifier = ee.Classifier.smileRandomForest(10).train({
features: trainingSample,
classProperty: label,
inputProperties: img.bandNames()
});
// Get information about the trained classifier.
print('Results of trained classifier', trainedClassifier.explain());
// Get a confusion matrix and overall accuracy for the training sample.
var trainAccuracy = trainedClassifier.confusionMatrix();
print('Training error matrix', trainAccuracy);
print('Training overall accuracy', trainAccuracy.accuracy());
// Get a confusion matrix and overall accuracy for the validation sample.
validationSample = validationSample.classify(trainedClassifier);
var validationAccuracy = validationSample.errorMatrix(label, 'classification');
print('Validation error matrix', validationAccuracy);
print('Validation accuracy', validationAccuracy.accuracy());
// Classify the reflectance image from the trained classifier.
var imgClassified = img.classify(trainedClassifier);
// Add the layers to the map.
var classVis = {
min: 0,
max: 10,
palette: ['006400' ,'ffbb22', 'ffff4c', 'f096ff', 'fa0000', 'b4b4b4',
'f0f0f0', '0064c8', '0096a0', '00cf75', 'fae6a0']
};
Map.setCenter(-122.184, 37.796, 12);
Map.addLayer(img, {bands: ['B11', 'B8', 'B3'], min: 100, max: 3500}, 'img');
Map.addLayer(lc, classVis, 'lc');
Map.addLayer(imgClassified, classVis, 'Classified');
Map.addLayer(roi, {color: 'white'}, 'ROI', false, 0.5);
Map.addLayer(trainingSample, {color: 'black'}, 'Training sample', false);
Map.addLayer(validationSample, {color: 'white'}, 'Validation sample', false);
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
import ee
import geemap.core as geemap
Colab (Python)
# A Sentinel-2 surface reflectance image, reflectance bands selected,
# serves as the source for training and prediction in this contrived example.
img = ee.Image(
'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG'
).select('B.*')
# ESA WorldCover land cover map, used as label source in classifier training.
lc = ee.Image('ESA/WorldCover/v100/2020')
# Remap the land cover class values to a 0-based sequential series.
class_values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100]
remap_values = ee.List.sequence(0, 10)
label = 'lc'
lc = lc.remap(class_values, remap_values).rename(label).toByte()
# Add land cover as a band of the reflectance image and sample 100 pixels at
# 10 m scale from each land cover class within a region of interest.
roi = ee.Geometry.Rectangle(-122.347, 37.743, -122.024, 37.838)
sample = img.addBands(lc).stratifiedSample(
numPoints=100, classBand=label, region=roi, scale=10, geometries=True
)
# Add a random value field to the sample and use it to approximately split 80%
# of the features into a training set and 20% into a validation set.
sample = sample.randomColumn()
training_sample = sample.filter('random <= 0.8')
validation_sample = sample.filter('random > 0.8')
# Train a 10-tree random forest classifier from the training sample.
trained_classifier = ee.Classifier.smileRandomForest(10).train(
features=training_sample,
classProperty=label,
inputProperties=img.bandNames(),
)
# Get information about the trained classifier.
display('Results of trained classifier', trained_classifier.explain())
# Get a confusion matrix and overall accuracy for the training sample.
train_accuracy = trained_classifier.confusionMatrix()
display('Training error matrix', train_accuracy)
display('Training overall accuracy', train_accuracy.accuracy())
# Get a confusion matrix and overall accuracy for the validation sample.
validation_sample = validation_sample.classify(trained_classifier)
validation_accuracy = validation_sample.errorMatrix(label, 'classification')
display('Validation error matrix', validation_accuracy)
display('Validation accuracy', validation_accuracy.accuracy())
# Classify the reflectance image from the trained classifier.
img_classified = img.classify(trained_classifier)
# Add the layers to the map.
class_vis = {
'min': 0,
'max': 10,
'palette': [
'006400',
'ffbb22',
'ffff4c',
'f096ff',
'fa0000',
'b4b4b4',
'f0f0f0',
'0064c8',
'0096a0',
'00cf75',
'fae6a0',
],
}
m = geemap.Map()
m.set_center(-122.184, 37.796, 12)
m.add_layer(
img, {'bands': ['B11', 'B8', 'B3'], 'min': 100, 'max': 3500}, 'img'
)
m.add_layer(lc, class_vis, 'lc')
m.add_layer(img_classified, class_vis, 'Classified')
m.add_layer(roi, {'color': 'white'}, 'ROI', False, 0.5)
m.add_layer(training_sample, {'color': 'black'}, 'Training sample', False)
m.add_layer(
validation_sample, {'color': 'white'}, 'Validation sample', False
)
m
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eCreates a Random Forest classifier using the SMILE implementation.\u003c/p\u003e\n"],["\u003cp\u003eOffers parameters to control the number of trees, variables per split, minimum leaf population, bag fraction, maximum nodes, and randomization seed.\u003c/p\u003e\n"],["\u003cp\u003eReturns an untrained classifier that needs to be trained using the \u003ccode\u003etrain()\u003c/code\u003e method with a FeatureCollection of labeled data.\u003c/p\u003e\n"],["\u003cp\u003eCommonly applied for land cover classification and other remote sensing image analysis tasks.\u003c/p\u003e\n"]]],[],null,["# ee.Classifier.smileRandomForest\n\nCreates an empty Random Forest classifier.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|---------------------------------------------------------------------------------------------------------------------------------------------|------------|\n| `ee.Classifier.smileRandomForest(numberOfTrees, `*variablesPerSplit* `, `*minLeafPopulation* `, `*bagFraction* `, `*maxNodes* `, `*seed*`)` | Classifier |\n\n| Argument | Type | Details |\n|---------------------|------------------------|-----------------------------------------------------------------------------------------------------|\n| `numberOfTrees` | Integer | The number of decision trees to create. |\n| `variablesPerSplit` | Integer, default: null | The number of variables per split. If unspecified, uses the square root of the number of variables. |\n| `minLeafPopulation` | Integer, default: 1 | Only create nodes whose training set contains at least this many points. |\n| `bagFraction` | Float, default: 0.5 | The fraction of input to bag per tree. |\n| `maxNodes` | Integer, default: null | The maximum number of leaf nodes in each tree. If unspecified, defaults to no limit. |\n| `seed` | Integer, default: 0 | The randomization seed. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// A Sentinel-2 surface reflectance image, reflectance bands selected,\n// serves as the source for training and prediction in this contrived example.\nvar img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')\n .select('B.*');\n\n// ESA WorldCover land cover map, used as label source in classifier training.\nvar lc = ee.Image('ESA/WorldCover/v100/2020');\n\n// Remap the land cover class values to a 0-based sequential series.\nvar classValues = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100];\nvar remapValues = ee.List.sequence(0, 10);\nvar label = 'lc';\nlc = lc.remap(classValues, remapValues).rename(label).toByte();\n\n// Add land cover as a band of the reflectance image and sample 100 pixels at\n// 10 m scale from each land cover class within a region of interest.\nvar roi = ee.Geometry.Rectangle(-122.347, 37.743, -122.024, 37.838);\nvar sample = img.addBands(lc).stratifiedSample({\n numPoints: 100,\n classBand: label,\n region: roi,\n scale: 10,\n geometries: true\n});\n\n// Add a random value field to the sample and use it to approximately split 80%\n// of the features into a training set and 20% into a validation set.\nsample = sample.randomColumn();\nvar trainingSample = sample.filter('random \u003c= 0.8');\nvar validationSample = sample.filter('random \u003e 0.8');\n\n// Train a 10-tree random forest classifier from the training sample.\nvar trainedClassifier = ee.Classifier.smileRandomForest(10).train({\n features: trainingSample,\n classProperty: label,\n inputProperties: img.bandNames()\n});\n\n// Get information about the trained classifier.\nprint('Results of trained classifier', trainedClassifier.explain());\n\n// Get a confusion matrix and overall accuracy for the training sample.\nvar trainAccuracy = trainedClassifier.confusionMatrix();\nprint('Training error matrix', trainAccuracy);\nprint('Training overall accuracy', trainAccuracy.accuracy());\n\n// Get a confusion matrix and overall accuracy for the validation sample.\nvalidationSample = validationSample.classify(trainedClassifier);\nvar validationAccuracy = validationSample.errorMatrix(label, 'classification');\nprint('Validation error matrix', validationAccuracy);\nprint('Validation accuracy', validationAccuracy.accuracy());\n\n// Classify the reflectance image from the trained classifier.\nvar imgClassified = img.classify(trainedClassifier);\n\n// Add the layers to the map.\nvar classVis = {\n min: 0,\n max: 10,\n palette: ['006400' ,'ffbb22', 'ffff4c', 'f096ff', 'fa0000', 'b4b4b4',\n 'f0f0f0', '0064c8', '0096a0', '00cf75', 'fae6a0']\n};\nMap.setCenter(-122.184, 37.796, 12);\nMap.addLayer(img, {bands: ['B11', 'B8', 'B3'], min: 100, max: 3500}, 'img');\nMap.addLayer(lc, classVis, 'lc');\nMap.addLayer(imgClassified, classVis, 'Classified');\nMap.addLayer(roi, {color: 'white'}, 'ROI', false, 0.5);\nMap.addLayer(trainingSample, {color: 'black'}, 'Training sample', false);\nMap.addLayer(validationSample, {color: 'white'}, 'Validation sample', false);\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# A Sentinel-2 surface reflectance image, reflectance bands selected,\n# serves as the source for training and prediction in this contrived example.\nimg = ee.Image(\n 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG'\n).select('B.*')\n\n# ESA WorldCover land cover map, used as label source in classifier training.\nlc = ee.Image('ESA/WorldCover/v100/2020')\n\n# Remap the land cover class values to a 0-based sequential series.\nclass_values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100]\nremap_values = ee.List.sequence(0, 10)\nlabel = 'lc'\nlc = lc.remap(class_values, remap_values).rename(label).toByte()\n\n# Add land cover as a band of the reflectance image and sample 100 pixels at\n# 10 m scale from each land cover class within a region of interest.\nroi = ee.Geometry.Rectangle(-122.347, 37.743, -122.024, 37.838)\nsample = img.addBands(lc).stratifiedSample(\n numPoints=100, classBand=label, region=roi, scale=10, geometries=True\n)\n\n# Add a random value field to the sample and use it to approximately split 80%\n# of the features into a training set and 20% into a validation set.\nsample = sample.randomColumn()\ntraining_sample = sample.filter('random \u003c= 0.8')\nvalidation_sample = sample.filter('random \u003e 0.8')\n\n# Train a 10-tree random forest classifier from the training sample.\ntrained_classifier = ee.Classifier.smileRandomForest(10).train(\n features=training_sample,\n classProperty=label,\n inputProperties=img.bandNames(),\n)\n\n# Get information about the trained classifier.\ndisplay('Results of trained classifier', trained_classifier.explain())\n\n# Get a confusion matrix and overall accuracy for the training sample.\ntrain_accuracy = trained_classifier.confusionMatrix()\ndisplay('Training error matrix', train_accuracy)\ndisplay('Training overall accuracy', train_accuracy.accuracy())\n\n# Get a confusion matrix and overall accuracy for the validation sample.\nvalidation_sample = validation_sample.classify(trained_classifier)\nvalidation_accuracy = validation_sample.errorMatrix(label, 'classification')\ndisplay('Validation error matrix', validation_accuracy)\ndisplay('Validation accuracy', validation_accuracy.accuracy())\n\n# Classify the reflectance image from the trained classifier.\nimg_classified = img.classify(trained_classifier)\n\n# Add the layers to the map.\nclass_vis = {\n 'min': 0,\n 'max': 10,\n 'palette': [\n '006400',\n 'ffbb22',\n 'ffff4c',\n 'f096ff',\n 'fa0000',\n 'b4b4b4',\n 'f0f0f0',\n '0064c8',\n '0096a0',\n '00cf75',\n 'fae6a0',\n ],\n}\nm = geemap.Map()\nm.set_center(-122.184, 37.796, 12)\nm.add_layer(\n img, {'bands': ['B11', 'B8', 'B3'], 'min': 100, 'max': 3500}, 'img'\n)\nm.add_layer(lc, class_vis, 'lc')\nm.add_layer(img_classified, class_vis, 'Classified')\nm.add_layer(roi, {'color': 'white'}, 'ROI', False, 0.5)\nm.add_layer(training_sample, {'color': 'black'}, 'Training sample', False)\nm.add_layer(\n validation_sample, {'color': 'white'}, 'Validation sample', False\n)\nm\n```"]]