How do you apply the covariance reducer to an array in Earth Engine? I have two lists containing values that I have sampled from an image and would like to calculate their covariance matrix. My attempt is

a1 = ee.Array([1,2,3,4,5]) 
a2 = ee.Array([5,4,3,2,1])
cov = ee.Array.cat([a1, a2], 1).tranpose() \
  .reduce(reducer = ee.Reducer.covariance(), axes = [0])

which would return

Array.reduce: Type must be a 1D EEArray.


The covariance reducer works on lists of 1D Arrays. So this code should work:

var a1 = ee.Array([1,2,3,4,5]) 
var a2 = ee.Array([5,4,3,2,1])
var cov = ee.List([a1, a2])
  • Thank you. How about their 2 x 2 covariance matrix? – korndog Jun 1 '20 at 16:39
  • Hi @korndog, it might be worth asking a second question for the 2 x 2 covariance matrix - it seems useful for the community and would be easier to find as a separate question (though might not be possible given the input is "some number of 1-D arrays"). Additionally, please accept this answer (click check mark below the vote tally) if it satisfies your question so that others looking can quickly identify this as a completed Q&A. – Justin Braaten Jun 1 '20 at 19:15
  • Thanks @JustinBraaten for the community tips and insight on my question. Currently settled on calculating it by definintion. – korndog Jun 2 '20 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.