繁体   English   中英

如何找到二维 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 包。

您可以使用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 以返回具有交集值的标量。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM