David Ferris David Ferris - 1 year ago 74
Python Question

A masked array indexing issue

I have a numpy array with some


arr = [ 0, NaN, 2, NaN, NaN, 5, 6, 7 ]

Using some logic (outside of the question scope), I generate a mask of the NaN locations:

mask = [ True, False, True, False, False, True, True, True ]

I use this mask to select only the valid data:

valid_arr = arr[mask] # [ 0, 2, 5, 6, 7 ]

I then perform an arbitrary algorithm which selects several
in this new array:

indeces = myAlgo(valid_arr) # [ 1, 3 ]

in the valid array are 1,3 (corresponding to values 2 and 6). I need to know what
these correspond to in the original array (
). In the above example, this is obviously 2 and 6.

The array is time series data, not sorted. One solution is to iterate over the
, incrementing a counter only when valid numbers are found. Can this be done more efficiently using numpy?

Answer Source

You can flat the mask which returns indices from the original array, and then use the new indices to subset the mask indices:

mask = np.array([ True, False, True, False, False, True, True, True ])
indices = [1,3]

# array([2, 6])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download