使用np。在2D数组中哪里可以找到匹配行

时间:2022-02-16 21:33:09

I would like to know how I use np.where with 2D array

我想知道如何使用np。用二维数组在哪里

I have the following array:

我有以下数组:

arr1 = np.array([[ 3.,  0.],
                 [ 3.,  1.],
                 [ 3.,  2.],
                 [ 3.,  3.],
                 [ 3.,  6.],
                 [ 3.,  5.]])

I want to find this array:

我想找到这个数组:

arr2 = np.array([3.,0.])

But when I use np.where():

但当我使用np.where()时:

np.where(arr1 == arr2)

It returns:

它返回:

(array([0, 0, 1, 2, 3, 4, 5]), array([0, 1, 0, 0, 0, 0, 0]))

I can't understand what it means. Can someone explain this for me?

我不明白这是什么意思。有人能给我解释一下吗?

1 个解决方案

#1


4  

You probably wanted all rows that are equal to your arr2:

你可能想要所有的行都等于arr2

>>> np.where(np.all(arr1 == arr2, axis=1))
(array([0], dtype=int64),)

Which means that the first row (zeroth index) matched.

这意味着第一行(第零索引)匹配。


The problem with your approach is that numpy broadcasts the arrays (visualized with np.broadcast_arrays):

您的方法的问题是numpy广播数组(用np.broadcast_arrays可视化):

>>> arr1_tmp, arr2_tmp = np.broadcast_arrays(arr1, arr2)
>>> arr2_tmp
array([[ 3.,  0.],
       [ 3.,  0.],
       [ 3.,  0.],
       [ 3.,  0.],
       [ 3.,  0.],
       [ 3.,  0.]]) 

and then does elementwise-comparison:

然后elementwise-comparison:

>>> arr1 == arr2
array([[ True,  True],
       [ True, False],
       [ True, False],
       [ True, False],
       [ True, False],
       [ True, False]], dtype=bool)

and np.where then gives you the coordinates of every True:

和np。然后给出所有真值的坐标:

>>> np.where(arr1 == arr2)
(array([0, 0, 1, 2, 3, 4, 5], dtype=int64),
 array([0, 1, 0, 0, 0, 0, 0], dtype=int64))
#       ^---- first match (0, 0)
#          ^--- second match (0, 1)
#             ^--- third match (1, 0)
#  ...

Which means (0, 0) (first row left item) is the first True, then 0, 1 (first row right item), then 1, 0 (second row, left item), ....

这意味着(0,0)(第一行左项)是第一个真实的,那么0,1(第一行),然后1,0(第二行,左项),....


If you use np.all along the first axis you get all rows that are completly equal:

如果你用np。沿着第一个轴你会得到完全相等的所有行:

>>> np.all(arr1 == arr2, axis=1)
array([ True, False, False, False, False, False], dtype=bool)

Can be better visualized if one keeps the dimensions:

如果一个维度保持维度,则可以更好地可视化:

>>> np.all(arr1 == arr2, axis=1, keepdims=True)
array([[ True],
       [False],
       [False],
       [False],
       [False],
       [False]], dtype=bool)

#1


4  

You probably wanted all rows that are equal to your arr2:

你可能想要所有的行都等于arr2

>>> np.where(np.all(arr1 == arr2, axis=1))
(array([0], dtype=int64),)

Which means that the first row (zeroth index) matched.

这意味着第一行(第零索引)匹配。


The problem with your approach is that numpy broadcasts the arrays (visualized with np.broadcast_arrays):

您的方法的问题是numpy广播数组(用np.broadcast_arrays可视化):

>>> arr1_tmp, arr2_tmp = np.broadcast_arrays(arr1, arr2)
>>> arr2_tmp
array([[ 3.,  0.],
       [ 3.,  0.],
       [ 3.,  0.],
       [ 3.,  0.],
       [ 3.,  0.],
       [ 3.,  0.]]) 

and then does elementwise-comparison:

然后elementwise-comparison:

>>> arr1 == arr2
array([[ True,  True],
       [ True, False],
       [ True, False],
       [ True, False],
       [ True, False],
       [ True, False]], dtype=bool)

and np.where then gives you the coordinates of every True:

和np。然后给出所有真值的坐标:

>>> np.where(arr1 == arr2)
(array([0, 0, 1, 2, 3, 4, 5], dtype=int64),
 array([0, 1, 0, 0, 0, 0, 0], dtype=int64))
#       ^---- first match (0, 0)
#          ^--- second match (0, 1)
#             ^--- third match (1, 0)
#  ...

Which means (0, 0) (first row left item) is the first True, then 0, 1 (first row right item), then 1, 0 (second row, left item), ....

这意味着(0,0)(第一行左项)是第一个真实的,那么0,1(第一行),然后1,0(第二行,左项),....


If you use np.all along the first axis you get all rows that are completly equal:

如果你用np。沿着第一个轴你会得到完全相等的所有行:

>>> np.all(arr1 == arr2, axis=1)
array([ True, False, False, False, False, False], dtype=bool)

Can be better visualized if one keeps the dimensions:

如果一个维度保持维度,则可以更好地可视化:

>>> np.all(arr1 == arr2, axis=1, keepdims=True)
array([[ True],
       [False],
       [False],
       [False],
       [False],
       [False]], dtype=bool)