I am trying to figure out an efficient way of finding row intersections of two np.arrays.
Two arrays have the same shapes, and duplicate values in each row cannot happen.
For example:
import numpy as np
a = np.array([[2,5,6],
[8,2,3],
[4,1,5],
[1,7,9]])
b = np.array([[2,3,4], # one element(2) in common with a[0] -> 1
[7,4,3], # one element(3) in common with a[1] -> 1
[5,4,1], # three elements(5,4,1) in common with a[2] -> 3
[7,6,9]]) # two element(9,7) in common with a[3] -> 2
My desired output is : np.array([1,1,3,2])
It is easy to do this with a loop:
def get_intersect1ds(a, b):
result = np.empty(a.shape[0], dtype=np.int)
for i in xrange(a.shape[0]):
result[i] = (len(np.intersect1d(a[i], b[i])))
return result
Result:
>>> get_intersect1ds(a, b)
array([1, 1, 3, 2])
But is there a more efficient way to do it?
aandbhave duplicated values in each row?