有效地找到两个2-D numpy数组的行交叉点

Aka*_*all 6 python numpy

我试图找出一种找到两个行交叉点的有效方法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
Run Code Online (Sandbox Code Playgroud)

我想要的输出是: 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
Run Code Online (Sandbox Code Playgroud)

结果:

>>> get_intersect1ds(a, b)
array([1, 1, 3, 2])
Run Code Online (Sandbox Code Playgroud)

但是有更有效的方法吗?

Jai*_*ime 7

如果你在一行中没有重复项,你可以尝试复制np.intersect1d幕后的内容(请参阅此处的源代码):

>>> c = np.hstack((a, b))
>>> c
array([[2, 5, 6, 2, 3, 4],
       [8, 2, 3, 7, 4, 3],
       [4, 1, 5, 5, 4, 1],
       [1, 7, 9, 7, 6, 9]])
>>> c.sort(axis=1)
>>> c
array([[2, 2, 3, 4, 5, 6],
       [2, 3, 3, 4, 7, 8],
       [1, 1, 4, 4, 5, 5],
       [1, 6, 7, 7, 9, 9]])
>>> c[:, 1:] == c[:, :-1]
array([[ True, False, False, False, False],
       [False,  True, False, False, False],
       [ True, False,  True, False,  True],
       [False, False,  True, False,  True]], dtype=bool)
>>> np.sum(c[:, 1:] == c[:, :-1], axis=1)
array([1, 1, 3, 2])
Run Code Online (Sandbox Code Playgroud)