Announcement: All noncommercial projects registered to use Earth Engine before April 15, 2025 must verify noncommercial eligibility to maintain Earth Engine access.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-13 UTC."],[[["\u003cp\u003eClassifies every feature within a given FeatureCollection using a specified classifier.\u003c/p\u003e\n"],["\u003cp\u003eReturns a new FeatureCollection with added classification results in a property specified by \u003ccode\u003eoutputName\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eRequires the input FeatureCollection to have properties matching the classifier's schema.\u003c/p\u003e\n"],["\u003cp\u003eThe default output property name is "classification" unless the classifier has multiple outputs.\u003c/p\u003e\n"]]],[],null,["Classifies each feature in a collection.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|----------------------------------------------------------|-------------------|\n| FeatureCollection.classify`(classifier, `*outputName*`)` | FeatureCollection |\n\n| Argument | Type | Details |\n|------------------|-----------------------------------|-------------------------------------------------------------------------------------------------------------------|\n| this: `features` | FeatureCollection | The collection of features to classify. Each feature must contain all the properties in the classifier's schema. |\n| `classifier` | Classifier | The classifier to use. |\n| `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. |\n\nExamples\n\nCode 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\nColab (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:', 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```"]]