我试图找出一种有效的方法来找到两个的行交叉点np.arrays
。
两个数组具有相同的形状,并且每行中不会发生重复值。
例如:
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
我想要的输出是:np.array([1,1,3,2])
使用循环很容易做到这一点:
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
结果:
>>> get_intersect1ds(a, b)
array([1, 1, 3, 2])
但是有没有更有效的方法呢?