0

I want to compute the median of an array of masked array. No problem to compute the mean but an error rising when I want to compute the median and I don't know why:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Here is a minimal example which reproduce the problem:

import numpy as np
import numpy.mask as ma

test = ma.masked_array([[1,2,3,4],[5,6,7,8]], mask = [[False,False,False,False],[False,False,False,False]])

test_tot= np.zeros(4,dtype='object')

test_tot[0]=test
test_tot[1]=test
test_tot[2]=test
test_tot[3]=test

np.mean(test_tot) # OK
np.median(test_tot) # PROBLEM ?

Thank you in advance for your advices

Julien Drevon
  • 322
  • 2
  • 17

1 Answers1

2

Your test_tot array is a 1D array of 2D arrays, rather than a 3D array.

As such, in trying to find the median, you're asking the interpreter to do a series of comparisons of the form "Is this 2D array bigger than this other 2D array?". To which the interpreter is replying "What do you mean by 'bigger' ? I don't know how to compare the absolute size of two such objects"

If you were using a 3D array, you could specify which axis you want to median along (or specify nothing, in which case numpy will calculate the median of the flattened array and give you that):

import numpy as np
import numpy.mask as ma


test = ma.masked_array([[1,2,3,4],[5,6,7,8]], mask = [[False,False,False,False],[False,False,False,False]])

test_tot= np.array([test,test,test,test])

Then you can ask for the median, specifying axis = None, 0, 1, or 2, depending what you want.

SimonR
  • 1,774
  • 1
  • 4
  • 10
  • I see thank you for your answer. So since in my code I am looping over several file with a for loop. The best way to do it so is to append the several masked array every iteration in a list and then tranform the list in numpy array? – Julien Drevon Dec 28 '20 at 10:45
  • If I can add something on your code, if I have some flag which are True, putting np.array will completly destroy the flag bring by the `ma.masked_array` so in order to make the computation we have to concatenate the whole list by replacing `np.array` by: `ma.array(your_list)`. And if we want to compute the median: `ma.median(ma.array(your_list),axis=0)`. Along the axis in which you're interested in. – Julien Drevon Dec 28 '20 at 13:12