pythonarrayspython-3.xmasked-array

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


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


Solution

  • 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.