簡體   English   中英

有沒有一種方法可以在Python的2D數組中查找整行數字的索引?

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

對於2D數組,Python中是否有像MATLAB中的“ find”命令一樣的命令?

如何在numpy數組中找到行[0.5795946,0.24307856,0.56676058,0.08502582]的位置

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]]) 

不使用for循環?

我嘗試了以下方法:

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

我收到以下錯誤:

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

因此,我想知道是否有更高效或更快速的方法來做到這一點。

查找數組中元素的索引

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)

輸出量

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

返回的第一個數組表示找到該值的行索引,第二個數組表示找到該值的列索引。


查找整個子數組

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)

輸出量

[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]                                      

如果將(5,4)形狀Atarget (4,)形狀進行比較,則會得到(5,4)布爾數組。 將目標與行A ,結果為4元素數組。 你得到的錯誤,因為這樣的陣列中不標工作if上下文。

(比較這兩個數組時,將應用廣播規則。要測試列,我們必須使用(5,1)形狀目標。)

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]])

這里==起作用; 但更一般而言,我們在測試浮點數時希望使用isclose

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]])

現在,我們可以將all應用於行,以獲得True / False數組,每行一個值:

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

以及該行的索引:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM