You really have a special case where it would be easier and more efficient to do the following:
Create data:
>>> arr array([[[ 6, 9, 4], [ 5, 2, 1], [10, 15, 30]], [[ 9, 0, 1], [ 4, 6, 4], [ 8, 3, 9]], [[ 6, 7, 4], [ 0, 1, 6], [ 4, 0, 1]]])
Expected Value:
>>> index_pos = np.where((arr[:,:,0]==10) & (arr[:,:,1]==15) & (arr[:,:,2]==30)) >>> index_pos (array([0]), array([2]))
Use broadcast to do this:
>>> arr == np.array([10,15,30]) array([[[False, False, False], [False, False, False], [ True, True, True]], [[False, False, False], [False, False, False], [False, False, False]], [[False, False, False], [False, False, False], [False, False, False]]], dtype=bool) >>> np.where( np.all(arr == np.array([10,15,30]), axis=-1) ) (array([0]), array([2]))
If the indexes you want are not contiguous, you can do something like this:
ind_vals = np.array([0,2]) where_mask = (arr[:,:,ind_vals] == values)
Broadcast whenever you can.
Influenced by @Jamie's comment, some interesting things to consider:
arr = np.random.randint(0,100,(5000,5000,3)) %timeit np.all(arr == np.array([10,15,30]), axis=-1) 1 loops, best of 3: 614 ms per loop %timeit ((arr[:,:,0]==10) & (arr[:,:,1]==15) & (arr[:,:,2]==30)) 1 loops, best of 3: 217 ms per loop %timeit tmp = (arr == np.array([10,15,30])); (tmp[:,:,0] & tmp[:,:,1] & tmp[:,:,2]) 1 loops, best of 3: 368 ms per loop
The question is, why is this ?:
First learn:
%timeit (arr[:,:,0]==10) 10 loops, best of 3: 51.2 ms per loop %timeit (arr == np.array([10,15,30])) 1 loops, best of 3: 300 ms per loop
One would expect that arr == np.array([10,15,30]) would in the worst case be 1/3 of the speed arr[:,:,0]==10 . Does anyone have an idea why this is not so?
Then when combining the final axis, there are many ways to do this.
tmp = (arr == np.array([10,15,30])) method1 = np.all(tmp,axis=-1) method2 = (tmp[:,:,0] & tmp[:,:,1] & tmp[:,:,2]) method3 = np.einsum('ij,ij,ij->ij',tmp[:,:,0] , tmp[:,:,1] , tmp[:,:,2]) np.allclose(method1,method2) True np.allclose(method1,method3) True %timeit np.all(tmp,axis=-1) 1 loops, best of 3: 318 ms per loop %timeit (tmp[:,:,0] & tmp[:,:,1] & tmp[:,:,2]) 10 loops, best of 3: 68.2 ms per loop %timeit np.einsum('ij,ij,ij->ij',tmp[:,:,0] , tmp[:,:,1] , tmp[:,:,2]) 10 loops, best of 3: 38 ms per loop
The einsum speed is well defined elsewhere , but it seems strange to me that there is a difference between all and consecutive & .