获取交叉numpy的二维数组的行索引 [英] Get indices of intersecting rows of Numpy 2d Array
问题描述
我希望得到一个主numpy的二维数组A的相交行的索引,另有一架B。
I want to get the indices of the intersecting rows of a main numpy 2d array A, with another one B.
A=array([[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10]])
B=array([[1, 4],
[1, 2],
[5, 6],
[6, 3]])
result=[0,2]
如果本应返回[0,2]基于阵列A的指数。
Where this should return [0,2] based on the indices of array A.
如何才能实现这一二维数组有效地完成?
How can this be done efficiently for 2d arrays?
感谢您!
修改
我已经试过功能:
k[np.in1d(k.view(dtype='i,i').reshape(k.shape[0]),k2.view(dtype='i,i').
reshape(k2.shape[0]))]
为in1d二维数组?但我得到一个重塑错误。我的数据类型是浮点数(有两位小数)。
此外,我也试图与套,但表现也是相当缓慢的。
from numpy in1d for 2D arrays? but I get a reshape error. My datatype is floats (with two decimals). Moreover, I also tried with sets but the performance is quite slow.
推荐答案
通过最小的变化,你可以得到你的工作方式:
With minimal changes, you can get your approach to work:
In [15]: A
Out[15]:
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]])
In [16]: B
Out[16]:
array([[1, 4],
[1, 2],
[5, 6],
[6, 3]])
In [17]: np.in1d(A.view('i,i').reshape(-1), B.view('i,i').reshape(-1))
Out[17]: array([ True, False, True, False, False], dtype=bool)
In [18]: np.nonzero(np.in1d(A.view('i,i').reshape(-1), B.view('i,i').reshape(-1)))
Out[18]: (array([0, 2], dtype=int64),)
In [19]: np.nonzero(np.in1d(A.view('i,i').reshape(-1), B.view('i,i').reshape(-1)))[0]
Out[19]: array([0, 2], dtype=int64)
如果您的数组不是花车,和都是连续的,那么下面会更快:
If your arrays are not floats, and are both contiguous, then the following will be faster:
In [21]: dt = np.dtype((np.void, A.dtype.itemsize * A.shape[1]))
In [22]: np.nonzero(np.in1d(A.view(dt).reshape(-1), B.view(dt).reshape(-1)))[0]
Out[22]: array([0, 2], dtype=int64)
和快速时间:
In [24]: %timeit np.nonzero(np.in1d(A.view('i,i').reshape(-1), B.view('i,i').reshape(-1)))[0]
10000 loops, best of 3: 75 µs per loop
In [25]: %timeit np.nonzero(np.in1d(A.view(dt).reshape(-1), B.view(dt).reshape(-1)))[0]
10000 loops, best of 3: 29.8 µs per loop
这篇关于获取交叉numpy的二维数组的行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!