How could I get numpy array indices by some conditions

numpy select rows by condition
numpy where multiple conditions
numpy where 2d array
index in numpy array
numpy filter 2d array by condition
numpy array replace values by index
numpy argwhere
numpy conditional indexing

I come to a problem like this: suppose I have arrays like this: a = np.array([[1,2,3,4,5,4,3,2,1],]) label = np.array([[1,0,1,0,0,1,1,0,1],]) I need to obtain the indices of a at which position the element value of label is 1 and the value of a is the largest amount all that causing label to be 1.

It maybe confusing, in the above example, the indices where label is 1 are: 0, 2, 5, 6, 8, their corresponding values of a are thus: 1, 3, 4, 3, 1, among which 4 is the larges, thus I need to get the result of 5 which is the index of number 4 in a. How could I do this with numpy ?

Get the 1s indices say as idx, then index into a with it, get max index and finally trace it back to the original order by indexing into idx -

idx = np.flatnonzero(label==1)
out = idx[a[idx].argmax()]

Sample run -

# Assuming inputs to be 1D
In [18]: a
Out[18]: array([1, 2, 3, 4, 5, 4, 3, 2, 1])

In [19]: label
Out[19]: array([1, 0, 1, 0, 0, 1, 1, 0, 1])

In [20]: idx = np.flatnonzero(label==1)

In [21]: idx[a[idx].argmax()]
Out[21]: 5

For a as ints and label as an array of 0s and 1s, we could optimize further as we could scale a based on the range of values in it, like so -

(label*(a.max()-a.min()+1) + a).argmax()

Furthermore, if a has positive numbers only, it would simplify to -

(label*(a.max()+1) + a).argmax()

Timings for positive ints largish a -

In [115]: np.random.seed(0)
     ...: a = np.random.randint(0,10,(100000))
     ...: label = np.random.randint(0,2,(100000))

In [117]: %%timeit
     ...: idx = np.flatnonzero(label==1)
     ...: out = idx[a[idx].argmax()]
1000 loops, best of 3: 592 µs per loop

In [116]: %timeit (label*(a.max()-a.min()+1) + a).argmax()
1000 loops, best of 3: 357 µs per loop

# @coldspeed's soln
In [120]: %timeit np.ma.masked_where(~label.astype(bool), a).argmax()
1000 loops, best of 3: 1.63 ms per loop

# won't work with negative numbers in a
In [119]: %timeit (label*(a.max()+1) + a).argmax()
1000 loops, best of 3: 292 µs per loop

# @klim's soln (won't work with negative numbers in a)
In [121]: %timeit np.argmax(a * (label == 1))
1000 loops, best of 3: 229 µs per loop

How do I select elements of an array given condition?, indices (rather than the actual values of array), the following code will do: For satisfying multiple (all) conditions: select_indices = np.where( np.logical_and( x >​  Write a NumPy program to select indices satisfying multiple conditions in a numpy array. Sample array: a = np.array([97, 101, 105, 111, 117]) b = np.array(['a','e','i','o','u']) Note: Select the elements from the second array corresponding to elements in the first array that are greater than 100 and less than 110.

You can use masked arrays:

>>> np.ma.masked_where(~label.astype(bool), a).argmax()
5

Numpy, or any other sequence with the exception of tuples. If one wants to get the corresponding indices (rather than the actual values of array), the following code will do: For satisfying multiple (all) conditions: select_indices = np.where( np.logical_and( x > 1, x < 5) )[0] # 1 < x <5 For satisfying multiple (or) conditions: select_indices = np.where( np.logical_or( x < 1, x > 5 ) )[0] # x <1 or x >5

Here is one of the simplest ways.

>>> np.argmax(a * (label == 1))
5
>>> np.argmax(a * (label == 1), axis=1)
array([5])

Coldspeed's method may take more time.

Slice (or Select) Data From Numpy Arrays, How do you select an element from a Numpy array? Let’s find the indices of element with value 15 in this 2D numpy array i.e. Output: It returns a tuple of arrays one for each dimension. Like in our case it’s a two dimension array, so numpy.where() will returns a tuple of two arrays.

IndexError: too many indices for array, returned that is either 1D, or even empty ( np. If you don't supply enough indices to an array, an ellipsis is silently appended. This means that in some sense you can view a two-dimensional array as an array of one-dimensional arrays. In combination with numpy's array-wise operations, this means that functions written for one-dimensional arrays can often just work for two-dimensional arrays.

numpy.where(): Process elements depending on conditions, NumPy Array Object Exercises, Practice and Solution: Write a NumPy program to select indices satisfying multiple conditions in a NumPy array. Using numpy.where(), elements of the NumPy array ndarray that satisfy the conditions can be replaced or performed specified processing. Here, the following contents will be described. If you want to extract or delete elements, rows and columns that satisfy the conditions, see the following post.

np.where - Numpy and Scipy, numpy.where(condition[, x, y]) function returns the indices of elements in an input which to choose. x, y and condition need to be broadcastable to some shape. the output array contains elements of x where condition is True, and elements  I have a NumPy array that looks like this: arr = np.array([100.10, 200.42, 4.14, 89.00, 34.55, 1.12]) How can I get multiple values from this array by index? For example, how can I get the values at the index positions 1, 4, and 5? I was trying something like this, which is incorrect: arr[1, 4, 5]

Comments
  • Nice answer! :-)
  • What if there are negative numbers in a?
  • Divakar. Yes this will not work if there are negatives numbers in a and if there is no match in label.
  • Still pretty fast under the constraints, going by the timings just added.