ee.FeatureCollection.classify Stay organized with collections Save and categorize content based on your preferences.
Page Summary
This function classifies each feature within a collection.
It takes a FeatureCollection and a Classifier as input.
The output is a FeatureCollection with an added classification property.
The name of the output property can be specified, defaulting to "classification".
The examples demonstrate classifying features derived from satellite imagery and computing an error matrix to evaluate the classification accuracy.
| Usage | Returns |
|---|---|
FeatureCollection.classify(classifier,outputName) | FeatureCollection |
| Argument | Type | Details |
|---|---|---|
this:features | FeatureCollection | The collection of features to classify. Each feature must contain all the properties in the classifier's schema. |
classifier | Classifier | The classifier to use. |
outputName | String, default: "classification" | The name of the output property to be added. This argument is ignored if the classifier has more than one output. |
Examples
Code Editor (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).varspectral=ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_038032_20160820').select('SR_B[1-7]');varlandcover=ee.Image('USGS/NLCD_RELEASES/2016_REL/2016').select('landcover');varsampleSource=spectral.addBands(landcover);// Sample the combined images to generate a FeatureCollection.varsample=sampleSource.sample({region:spectral.geometry(),// sample only from within Landsat image extentscale: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.vartraining=sample.filter('random < 0.8');print('Training set',training);// Train a random forest classifier.varclassifier=ee.Classifier.smileRandomForest(10).train({features:training,classProperty:landcover.bandNames().get(0),inputProperties:spectral.bandNames()});// Classify the sample.varpredictions=sample.classify({classifier:classifier,outputName:'predicted_landcover'});print('Predictions',predictions);// Split out the validation feature set.varvalidation=predictions.filter('random >= 0.8');print('Validation set',validation);// Get a list of possible class values to use for error matrix axis labels.varorder=sample.aggregate_array('landcover').distinct().sort();print('Error matrix axis labels',order);// Compute an error matrix that compares predicted vs. expected values.varerrorMatrix=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 setup
See the Python Environment page for information on the Python API and usinggeemap for interactive development.
importeeimportgeemap.coreasgeemap
Colab (Python)
# 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'})display('Sample for classifier development:',sample)# Split out ~80% of the sample for training the classifier.training=sample.filter('random < 0.8')display('Training set:',training)# 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'})display('Predictions:',predictions)# Split out the validation feature set.validation=predictions.filter('random >= 0.8')display('Validation set:',validation)# Get a list of possible class values to use for error matrix axis labels.order=sample.aggregate_array('landcover').distinct().sort()display('Error matrix axis labels:',order)# Compute an error matrix that compares predicted vs. expected values.error_matrix=validation.errorMatrix(**{'actual':landcover.bandNames().get(0),'predicted':'predicted_landcover','order':order})display('Error matrix:',error_matrix)# Compute accuracy metrics from the error matrix.display('Overall accuracy:',error_matrix.accuracy())display('Consumer\'s accuracy:',error_matrix.consumersAccuracy())display('Producer\'s accuracy:',error_matrix.producersAccuracy())display('Kappa:',error_matrix.kappa())
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-13 UTC.