Search code examples
numpymasked-array

Why does a dot product of two masked vectors in numpy return an oddly shaped array?


I have the following code:

result = np.ma.dot( array1, masked_array2 )

Which gives something like this:

masked_array(data = 24.681441709536468,
         mask = False,
         fill_value = 1e+20)

result.data.shape gives:

()

I can access the value by converting it to a float, like

float(result.data)

Is this the correct way of accessing the data?


Solution

  • The result is a 0D tensor.

    Typically numpy converts 0D tensor to native type

    type(np.dot([1,2], [3,4])) # gives 'int'
    

    However, when the result is masked array, due to the existence of mask, there's no way to convert it directly to a native type without losing information. Thus you get a "oddly shaped" 0D tensor as result.

    Yes, you can access it by converting it to float.