Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching an array to a row in Numpy

I have an array 'A' of shape(50,3) and another array 'B' of shape (1,3).

Actually this B is a row in A. So I need to find its row location.

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

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.

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.

How can i do this in Numpy?

Thank you.

like image 290
Abid Rahman K Avatar asked Dec 16 '22 22:12

Abid Rahman K


1 Answers

You can specify the axis:

numpy.where((A == B).all(axis=1))
like image 128
Benjamin Avatar answered Dec 27 '22 15:12

Benjamin