有效地找到两个二维numpy数组的行相交 [英] Efficiently find row intersections of two 2-D numpy arrays
本文介绍了有效地找到两个二维numpy数组的行相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找出找到两个np.arrays
的行交点的有效方法.
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.
例如:
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])
使用循环很容易做到这一点:
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
结果:
>>> get_intersect1ds(a, b)
array([1, 1, 3, 2])
但是有没有更有效的方法呢?
But is there a more efficient way to do it?
推荐答案
如果一行中没有重复项,则可以尝试复制np.intersect1d
的功能(请参见源代码
If you have no duplicates within a row you can try to replicate what np.intersect1d
does under the hood (see the source code here):
>>> 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])
这篇关于有效地找到两个二维numpy数组的行相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文