如何找到 2d numpy 数组的行交集?

How to find a row-wise intersection of 2d numpy arrays?

我正在寻找一种有效的方法来获得两个二维 numpy ndarray 的按行交集。每行只有一个交叉点。例如:

[[1, 2], ∩ [[0, 1], -> [1,
 [3, 4]]    [0, 3]]     3]

在最好的情况下,应该忽略零:

[[1, 2, 0], ∩ [[0, 1, 0], -> [1,
 [3, 4, 0]]    [0, 3, 0]]     3]

我的解决方案:

import numpy as np

arr1 = np.array([[1, 2],
                 [3, 4]])
arr2 = np.array([[0, 1],
                 [0, 3]])
arr3 = np.empty(len(arr1))

for i in range(len(arr1)):
    arr3[i] = np.intersect1d(arr1[i], arr2[i])

print(arr3)
# [ 1.  3.]

我有大约 100 万行,所以向量化操作是首选。欢迎您使用其他 python 软件包。

我不知道在 numpy 中有什么优雅的方法可以做到这一点,但是一个简单的列表理解就可以做到这一点:

[list(set.intersection(set(_x),set(_y)).difference({0})) for _x,_y in zip(x,y)]

您可以使用 np.apply_along_axis。 我写了一个解决方案来填充 arr1 的大小。 没测试效率。

    import numpy as np

    def intersect1d_padded(x):
        x, y = np.split(x, 2)
        padded_intersection = -1 * np.ones(x.shape, dtype=np.int)
        intersection = np.intersect1d(x, y)
        padded_intersection[:intersection.shape[0]] = intersection
        return padded_intersection

    def rowwise_intersection(a, b):
        return np.apply_along_axis(intersect1d_padded,
                        1, np.concatenate((a, b), axis=1))

    result = rowwise_intersection(arr1,arr2)

    >>> array([[ 1, -1],
               [ 3, -1]])

如果您知道交叉点中只有一个元素,您可以使用

    result = rowwise_intersection(arr1,arr2)[:,0]

    >>> array([1, 3])

您还可以将 intersect1d_padded 修改为 return 具有交集值的标量。