ee.FeatureCollection.errorMatrix
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คำนวณเมทริกซ์ข้อผิดพลาด 2 มิติสำหรับคอลเล็กชันโดยการเปรียบเทียบคอลัมน์ 2 คอลัมน์ของคอลเล็กชัน ได้แก่ คอลัมน์ที่มีค่าจริงและคอลัมน์ที่มีค่าที่คาดการณ์ ค่าควรเป็นจำนวนเต็มที่ต่อเนื่องกันและมีค่าน้อย โดยเริ่มจาก 0 แกน 0 (แถว) ของเมทริกซ์สอดคล้องกับค่าจริง และแกน 1 (คอลัมน์) สอดคล้องกับค่าที่คาดการณ์
การใช้งาน | การคืนสินค้า |
---|
FeatureCollection.errorMatrix(actual, predicted, order) | ConfusionMatrix |
อาร์กิวเมนต์ | ประเภท | รายละเอียด |
---|
ดังนี้ collection | FeatureCollection | คอลเล็กชันอินพุต |
actual | สตริง | ชื่อของพร็อพเพอร์ตี้ที่มีค่าจริง |
predicted | สตริง | ชื่อของพร็อพเพอร์ตี้ที่มีค่าที่คาดการณ์ |
order | รายการ (ค่าเริ่มต้น: null) | รายการค่าที่คาดหวัง หากไม่ได้ระบุอาร์กิวเมนต์นี้ ระบบจะถือว่าค่าต่างๆ อยู่ติดกันและครอบคลุมช่วง 0 ถึง maxValue หากระบุไว้ ระบบจะใช้เฉพาะค่าที่ตรงกับรายการนี้ และเมทริกซ์จะมีมิติข้อมูลและลำดับที่ตรงกับรายการนี้ |
ตัวอย่าง
โปรแกรมแก้ไขโค้ด (JavaScript)
/**
* Classifies features in a FeatureCollection and computes an error matrix.
*/
// Combine Landsat and NLCD images using only the bands representing
// predictor variables (spectral reflectance) and target labels (land cover).
var spectral =
ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_038032_20160820').select('SR_B[1-7]');
var landcover =
ee.Image('USGS/NLCD_RELEASES/2016_REL/2016').select('landcover');
var sampleSource = spectral.addBands(landcover);
// Sample the combined images to generate a FeatureCollection.
var sample = sampleSource.sample({
region: spectral.geometry(), // sample only from within Landsat image extent
scale: 30,
numPixels: 2000,
geometries: true
})
// Add a random value column with uniform distribution for hold-out
// training/validation splitting.
.randomColumn({distribution: 'uniform'});
print('Sample for classifier development', sample);
// Split out ~80% of the sample for training the classifier.
var training = sample.filter('random < 0.8');
print('Training set', training);
// Train a random forest classifier.
var classifier = ee.Classifier.smileRandomForest(10).train({
features: training,
classProperty: landcover.bandNames().get(0),
inputProperties: spectral.bandNames()
});
// Classify the sample.
var predictions = sample.classify(
{classifier: classifier, outputName: 'predicted_landcover'});
print('Predictions', predictions);
// Split out the validation feature set.
var validation = predictions.filter('random >= 0.8');
print('Validation set', validation);
// Get a list of possible class values to use for error matrix axis labels.
var order = sample.aggregate_array('landcover').distinct().sort();
print('Error matrix axis labels', order);
// Compute an error matrix that compares predicted vs. expected values.
var errorMatrix = validation.errorMatrix({
actual: landcover.bandNames().get(0),
predicted: 'predicted_landcover',
order: order
});
print('Error matrix', errorMatrix);
// Compute accuracy metrics from the error matrix.
print("Overall accuracy", errorMatrix.accuracy());
print("Consumer's accuracy", errorMatrix.consumersAccuracy());
print("Producer's accuracy", errorMatrix.producersAccuracy());
print("Kappa", errorMatrix.kappa());
การตั้งค่า Python
ดูข้อมูลเกี่ยวกับ Python API และการใช้ geemap
เพื่อการพัฒนาแบบอินเทอร์แอกทีฟได้ที่หน้า
สภาพแวดล้อม Python
import ee
import geemap.core as geemap
Colab (Python)
from pprint import pprint
# Classifies features in a FeatureCollection and computes an error matrix.
# Combine Landsat and NLCD images using only the bands representing
# predictor variables (spectral reflectance) and target labels (land cover).
spectral = ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_038032_20160820').select(
'SR_B[1-7]')
landcover = ee.Image('USGS/NLCD_RELEASES/2016_REL/2016').select('landcover')
sample_source = spectral.addBands(landcover)
# Sample the combined images to generate a FeatureCollection.
sample = sample_source.sample(**{
# sample only from within Landsat image extent
'region': spectral.geometry(),
'scale': 30,
'numPixels': 2000,
'geometries': True
})
# Add a random value column with uniform distribution for hold-out
# training/validation splitting.
sample = sample.randomColumn(**{'distribution': 'uniform'})
print('Sample for classifier development:', sample.getInfo())
# Split out ~80% of the sample for training the classifier.
training = sample.filter('random < 0.8')
print('Training set:', training.getInfo())
# Train a random forest classifier.
classifier = ee.Classifier.smileRandomForest(10).train(**{
'features': training,
'classProperty': landcover.bandNames().get(0),
'inputProperties': spectral.bandNames()
})
# Classify the sample.
predictions = sample.classify(
**{'classifier': classifier, 'outputName': 'predicted_landcover'})
print('Predictions:', predictions.getInfo())
# Split out the validation feature set.
validation = predictions.filter('random >= 0.8')
print('Validation set:', validation.getInfo())
# Get a list of possible class values to use for error matrix axis labels.
order = sample.aggregate_array('landcover').distinct().sort()
print('Error matrix axis labels:')
pprint(order.getInfo())
# Compute an error matrix that compares predicted vs. expected values.
error_matrix = validation.errorMatrix(**{
'actual': landcover.bandNames().get(0),
'predicted': 'predicted_landcover',
'order': order
})
print('Error matrix:')
pprint(error_matrix.getInfo())
# Compute accuracy metrics from the error matrix.
print('Overall accuracy:', error_matrix.accuracy().getInfo())
print('Consumer\'s accuracy:')
pprint(error_matrix.consumersAccuracy().getInfo())
print('Producer\'s accuracy:')
pprint(error_matrix.producersAccuracy().getInfo())
print('Kappa:', error_matrix.kappa().getInfo())
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eComputes a 2D error matrix (confusion matrix) for a FeatureCollection by comparing actual and predicted values.\u003c/p\u003e\n"],["\u003cp\u003eTakes the names of the properties containing the actual and predicted values as inputs.\u003c/p\u003e\n"],["\u003cp\u003eAccepts an optional 'order' argument to specify the expected values for the matrix axes.\u003c/p\u003e\n"],["\u003cp\u003eThe matrix rows represent actual values and columns represent predicted values, aiding in assessing classification accuracy.\u003c/p\u003e\n"],["\u003cp\u003eValues are expected to be small, contiguous integers starting from 0.\u003c/p\u003e\n"]]],["The `errorMatrix` method computes a 2D confusion matrix by comparing actual and predicted values from two columns within a FeatureCollection. It takes `actual` and `predicted` column names as inputs, and an optional `order` list to define the matrix's dimensions and included values. The function uses small contiguous integers starting from 0, and returns a `ConfusionMatrix` object that includes overall accuracy, consumer's accuracy, producer's accuracy and kappa.\n"],null,["# ee.FeatureCollection.errorMatrix\n\nComputes a 2D error matrix for a collection by comparing two columns of a collection: one containing the actual values, and one containing predicted values. The values are expected to be small contiguous integers, starting from 0. Axis 0 (the rows) of the matrix correspond to the actual values, and Axis 1 (the columns) to the predicted values.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|---------------------------------------------------------------|-----------------|\n| FeatureCollection.errorMatrix`(actual, predicted, `*order*`)` | ConfusionMatrix |\n\n| Argument | Type | Details |\n|--------------------|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `collection` | FeatureCollection | The input collection. |\n| `actual` | String | The name of the property containing the actual value. |\n| `predicted` | String | The name of the property containing the predicted value. |\n| `order` | List, default: null | A list of the expected values. If this argument is not specified, the values are assumed to be contiguous and span the range 0 to maxValue. If specified, only values matching this list are used, and the matrix will have dimensions and order matching this list. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n/**\n * Classifies features in a FeatureCollection and computes an error matrix.\n */\n\n// Combine Landsat and NLCD images using only the bands representing\n// predictor variables (spectral reflectance) and target labels (land cover).\nvar spectral =\n ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_038032_20160820').select('SR_B[1-7]');\nvar landcover =\n ee.Image('USGS/NLCD_RELEASES/2016_REL/2016').select('landcover');\nvar sampleSource = spectral.addBands(landcover);\n\n// Sample the combined images to generate a FeatureCollection.\nvar sample = sampleSource.sample({\n region: spectral.geometry(), // sample only from within Landsat image extent\n scale: 30,\n numPixels: 2000,\n geometries: true\n})\n// Add a random value column with uniform distribution for hold-out\n// training/validation splitting.\n.randomColumn({distribution: 'uniform'});\nprint('Sample for classifier development', sample);\n\n// Split out ~80% of the sample for training the classifier.\nvar training = sample.filter('random \u003c 0.8');\nprint('Training set', training);\n\n// Train a random forest classifier.\nvar classifier = ee.Classifier.smileRandomForest(10).train({\n features: training,\n classProperty: landcover.bandNames().get(0),\n inputProperties: spectral.bandNames()\n});\n\n// Classify the sample.\nvar predictions = sample.classify(\n {classifier: classifier, outputName: 'predicted_landcover'});\nprint('Predictions', predictions);\n\n// Split out the validation feature set.\nvar validation = predictions.filter('random \u003e= 0.8');\nprint('Validation set', validation);\n\n// Get a list of possible class values to use for error matrix axis labels.\nvar order = sample.aggregate_array('landcover').distinct().sort();\nprint('Error matrix axis labels', order);\n\n// Compute an error matrix that compares predicted vs. expected values.\nvar errorMatrix = validation.errorMatrix({\n actual: landcover.bandNames().get(0),\n predicted: 'predicted_landcover',\n order: order\n});\nprint('Error matrix', errorMatrix);\n\n// Compute accuracy metrics from the error matrix.\nprint(\"Overall accuracy\", errorMatrix.accuracy());\nprint(\"Consumer's accuracy\", errorMatrix.consumersAccuracy());\nprint(\"Producer's accuracy\", errorMatrix.producersAccuracy());\nprint(\"Kappa\", errorMatrix.kappa());\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\nfrom pprint import pprint\n\n# Classifies features in a FeatureCollection and computes an error matrix.\n\n# Combine Landsat and NLCD images using only the bands representing\n# predictor variables (spectral reflectance) and target labels (land cover).\nspectral = ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_038032_20160820').select(\n 'SR_B[1-7]')\nlandcover = ee.Image('USGS/NLCD_RELEASES/2016_REL/2016').select('landcover')\nsample_source = spectral.addBands(landcover)\n\n# Sample the combined images to generate a FeatureCollection.\nsample = sample_source.sample(**{\n # sample only from within Landsat image extent\n 'region': spectral.geometry(),\n 'scale': 30,\n 'numPixels': 2000,\n 'geometries': True\n })\n# Add a random value column with uniform distribution for hold-out\n# training/validation splitting.\nsample = sample.randomColumn(**{'distribution': 'uniform'})\nprint('Sample for classifier development:', sample.getInfo())\n\n# Split out ~80% of the sample for training the classifier.\ntraining = sample.filter('random \u003c 0.8')\nprint('Training set:', training.getInfo())\n\n# Train a random forest classifier.\nclassifier = ee.Classifier.smileRandomForest(10).train(**{\n 'features': training,\n 'classProperty': landcover.bandNames().get(0),\n 'inputProperties': spectral.bandNames()\n })\n\n# Classify the sample.\npredictions = sample.classify(\n **{'classifier': classifier, 'outputName': 'predicted_landcover'})\nprint('Predictions:', predictions.getInfo())\n\n# Split out the validation feature set.\nvalidation = predictions.filter('random \u003e= 0.8')\nprint('Validation set:', validation.getInfo())\n\n# Get a list of possible class values to use for error matrix axis labels.\norder = sample.aggregate_array('landcover').distinct().sort()\nprint('Error matrix axis labels:')\npprint(order.getInfo())\n\n# Compute an error matrix that compares predicted vs. expected values.\nerror_matrix = validation.errorMatrix(**{\n 'actual': landcover.bandNames().get(0),\n 'predicted': 'predicted_landcover',\n 'order': order\n })\nprint('Error matrix:')\npprint(error_matrix.getInfo())\n\n# Compute accuracy metrics from the error matrix.\nprint('Overall accuracy:', error_matrix.accuracy().getInfo())\nprint('Consumer\\'s accuracy:')\npprint(error_matrix.consumersAccuracy().getInfo())\nprint('Producer\\'s accuracy:')\npprint(error_matrix.producersAccuracy().getInfo())\nprint('Kappa:', error_matrix.kappa().getInfo())\n```"]]