I would like to iterate over only unmasked values in a np.ma.ndarray
.
With the following:
import numpy as np
a = np.ma.array([1, 2, 3], mask = [0, 1, 0])
for i in a:
print i
I get:
1
--
3
I would like to get the following:
1
3
It seems like np.nditer()
may be the way to go, but I don't find any flags that might specify this. How might I do this? Thanks!
Iterating Arrays Iterating means going through elements one by one. As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python. If we iterate on a 1-D array it will go through each element one by one.
Accessing the maskgetmask(x) outputs the mask of x if x is a masked array, and the special value nomask otherwise. getmaskarray(x) outputs the mask of x if x is a masked array. If x has no invalid entry or is not a masked array, the function outputs a boolean array of False with as many elements as x .
ndenumerate(arr)[source] Multidimensional index iterator. Return an iterator yielding pairs of array coordinates and values.
you want to use a.compressed()
import numpy as np
a = np.ma.array([1, 2, 3], mask = [0, 1, 0])
for i in a.compressed():
print i
which gives:
1
3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With