David Ferris David Ferris - 1 year ago 74
Python Question

A masked array indexing issue

I have a numpy array with some

NaN
values:

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
indeces
in this new array:

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


The
indeces
in the valid array are 1,3 (corresponding to values 2 and 6). I need to know what
indeces
these correspond to in the original array (
arr
). 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
mask
, 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]

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