问题描述
我有一个形状为(50,3)的数组'A'和另一个形状为(1,3)的数组'B'.
I have an array 'A' of shape(50,3) and another array 'B' of shape (1,3).
实际上,这个B是A中的一行.因此,我需要找到它的行位置.
Actually this B is a row in A. So I need to find its row location.
我使用了np.where(A==B)
,但是它给出了明智的搜索位置.例如,下面是我得到的结果:
I used np.where(A==B)
, but it gives the locations searched element wise. For example, below is the result i got :
>>> np.where(A == B)
(array([ 3, 3, 3, 30, 37, 44]), array([0, 1, 2, 1, 2, 0]))
实际上B是A中的第四行(以我为例).但是上面的结果给出了(3,0)(3,1)(3,2)和其他元素,它们在元素方面是匹配的.
Actually B is the 4th row in A (in my case). But above result gives (3,0)(3,1)(3,2) and others, which are matched element-wise.
相反,我需要答案"3",这是B整体搜索A时获得的答案,它还删除了部分匹配项(30,1)(37,2)...之类的其他答案.
Instead of this, i need an answer '3' which is the answer obtained when B searched in A as a whole and it also removes others like (30,1)(37,2)... which are partial matches.
我如何在Numpy中做到这一点?
How can i do this in Numpy?
谢谢.
推荐答案
您可以指定轴:
numpy.where((A == B).all(axis=1))
这篇关于将数组匹配到Numpy中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!