Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy masked operations

I am a new python user and I am quite interesting on understanding in depth how works the NumPy module. I am writing on a function able to use both masked and unmasked arrays as data input. I have noticed that there are several numpy masked operations that look similar (and even work?) to its normal (unmasked) counterpart. One of such functions is numpy.zeros and numpy.ma.zeros. Could someone else tell me the advantage of, say, creating an array using numpy.ma.zeros vs. numpy.zeros? It makes an actual difference when you are using masked arrays? I have noticed that when I use numpy.zeros_like it works fine for both creating a masked or unmasked array.

like image 989
hurrdrought Avatar asked Oct 24 '25 13:10

hurrdrought


1 Answers

np.ma.zeros creates a masked array rather than a normal array which could be useful if some later operation on this array creates invalid values. An example from the manual:

Arrays sometimes contain invalid or missing data. When doing operations on such arrays, we wish to suppress invalid values, which is the purpose masked arrays fulfill (an example of typical use is given below).

For example, examine the following array:

>>> x = np.array([2, 1, 3, np.nan, 5, 2, 3, np.nan])

When we try to calculate the mean of the data, the result is undetermined:

>>> np.mean(x) nan

The mean is calculated using roughly np.sum(x)/len(x), but since any number added to NaN produces NaN, this doesn't work. Enter masked arrays:

>>> m = np.ma.masked_array(x, np.isnan(x))
>>> m
masked_array(data = [2.0 1.0 3.0 -- 5.0 2.0 3.0 --],
      mask = [False False False  True False False False  True],
      fill_value=1e+20)

Here, we construct a masked array that suppress all NaN values. We may now proceed to calculate the mean of the other values:

>>> np.mean(m)
2.6666666666666665
like image 68
Azad Avatar answered Oct 27 '25 03:10

Azad