简体   繁体   中英

Is there a way to find the indices of an entire row of numbers in a 2D array in Python?

For a 2D array, is there a command in Python like the "find" command in MATLAB?

How do I find the location of the row [ 0.5795946 , 0.24307856, 0.56676058, 0.08502582] in a numpy array

A = array([[ 0.57383254,  0.10132767,  0.86211639,  0.35402222],
       [ 0.20238346,  0.93204519,  0.84563318,  0.68373515],
       [ 0.5795946 ,  0.24307856,  0.56676058,  0.08502582],
       [ 0.27188428,  0.0630682 ,  0.9762359 ,  0.50456657],
       [ 0.6522969 ,  0.85018875,  0.22728716,  0.82851854]]) 

without using a for loop?

I tried the following:

for i in range(A.shape[0]):
    if (A[i]==[ 0.5795946 ,  0.24307856,  0.56676058,  0.08502582]):
        print(i) 

I got the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

So, was wondering if there's a more efficient or faster way to do it.

To find the index of the element in the array

import numpy as np
A = np.array([[ 0.57383254,  0.10132767,  0.86211639,  0.35402222],
       [ 0.20238346,  0.93204519,  0.84563318,  0.68373515],
       [ 0.5795946 ,  0.24307856,  0.56676058,  0.08502582],
       [ 0.27188428,  0.0630682 ,  0.9762359 ,  0.50456657],
       [ 0.6522969 ,  0.85018875,  0.22728716,  0.82851854]]) 
target = np.array([ 0.57959463 ,  0.24307856,  0.56676058,  0.08502582])
np.where(A == target)

Output

(array([2, 2, 2]), array([1, 2, 3]))

The returned first array represents the row indices where this value is found, the second array represents the column indices where this value is found.


To find the whole subarray

A = np.array([[ 0.57383254,  0.10132767,  0.86211639,  0.35402222],
       [ 0.20238346,  0.93204519,  0.84563318,  0.68373515],
       [ 0.5795946 ,  0.24307856,  0.56676058,  0.08502582],
       [ 0.27188428,  0.0630682 ,  0.9762359 ,  0.50456657],
       [ 0.6522969 ,  0.85018875,  0.22728716,  0.82851854]]) 
target = np.array([ 0.5795946 ,  0.24307856,  0.56676058,  0.08502582])
result, = np.where(np.all(A == target, axis=1))
print(result)

Output

[2]
In [147]: A = np.array([[ 0.57383254,  0.10132767,  0.86211639,  0.35402222], 
     ...:        [ 0.20238346,  0.93204519,  0.84563318,  0.68373515], 
     ...:        [ 0.5795946 ,  0.24307856,  0.56676058,  0.08502582], 
     ...:        [ 0.27188428,  0.0630682 ,  0.9762359 ,  0.50456657], 
     ...:        [ 0.6522969 ,  0.85018875,  0.22728716,  0.82851854]])                                      
In [148]: target = [ 0.5795946 ,  0.24307856,  0.56676058,  0.08502582]                                      

If you compare the (5,4) shape A the target (4,) shape, you get a (5,4) boolean array. When you compared the target with a row a A the result is a 4 element array. You get the error because such an array does not work in a scalar if context.

(Broadcasting rules apply when comparing these two arrays. Testing for columns we'd have to use a (5,1) shape target.)

In [149]: A==target                                                                                          
Out[149]: 
array([[False, False, False, False],
       [False, False, False, False],
       [ True,  True,  True,  True],
       [False, False, False, False],
       [False, False, False, False]])

Here the == works; but more generally we want to use isclose when testing floats:

In [152]: np.isclose(A,target)                                                                               
Out[152]: 
array([[False, False, False, False],
       [False, False, False, False],
       [ True,  True,  True,  True],
       [False, False, False, False],
       [False, False, False, False]])

Now we can apply all to the rows, to get a True/False array, with one value per row:

In [153]: np.all(np.isclose(A,target), axis=1)                                                               
Out[153]: array([False, False,  True, False, False])

and the index of that row(s):

In [154]: np.nonzero(np.all(np.isclose(A,target), axis=1))                                                   
Out[154]: (array([2]),)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM