TransWikia.com

Earth Engine error with global variables - using to calculate matrix error

Geographic Information Systems Asked by Lilian Guimarães on October 10, 2020

After sorting my image using training data,

//***********************************************************************
//                    SEPARATE TEST AND TRAIN SAMPLES
//***********************************************************************
// delimitate samples to test and train 

var trainSize = dataset.size().multiply(0.7).int()
var testSize = dataset.size().multiply(0.3).int()

var dataTrain = dadosRandomizados
  .sort("randomPreClassificacao")
  .toList(trainSize, 1)

var dataTest = dadosRandomizados
  .sort("randomPreClassificacao")
  .toList(testSize, trainSize)

//***********************************************************************
//       CREATE CLASSIFIER - create an choosen classifier - ten trees
//***********************************************************************
var classificador = ee.Classifier.randomForest(10)


//***********************************************************************
//                    TRAIN CLASSIFIER
//***********************************************************************


//train the classifier
var classificadorTreinado = classificador.train({
  features: dataTrain,
  classProperty: 'CLASS',
  inputProperties: bands
})


var imagemClassificada = addndvi_2018.select(bands).classify(classificadorTreinado)

I want to use my test data to calculate the error matrix, the accuracy of the consumer and the producer.

var testClassificador = testSize.classify(classificadorTreinado)
var errorMatrix = testClassificado.errorMatrix('CLASS', 'classification')
print('errorMatrix:', errorMatrix)
print('accuracy:',  errorMatrix.accuracy())
print('consumersAccuracy:', errorMatrix.consumersAccuracy())
print('producersAccuracy:', errorMatrix.producersAccuracy())

However, I am having a global variable declaration error, whereas for Earth Engine there are reserved objects.

testSize.classify is not a function

Here is my code in Earth Engine

One Answer

You are trying to classify a ee.Number (testSize).

This is not how it's usually done, but to keep your logic:

// create FeatureCollections with the Lists
var dataTrain = ee.FeatureCollection(dadosRandomizados
  .sort("randomPreClassificacao")
  .toList(trainSize, 1))

var dataTest = ee.FeatureCollection(dadosRandomizados
  .sort("randomPreClassificacao")
  .toList(testSize, trainSize))

// classify the FeatueCollection
var testClassificador = dataTest.classify(classificadorTreinado)
var errorMatrix = testClassificador.errorMatrix('CLASS', 'classification')
print(errorMatrix)

The usual way is to use the random column generated by FeatureCollection.randomColumns

Answered by Rodrigo E. Principe on October 10, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP