[英]Python - Intersection of 2D Numpy Arrays
我正在拼命寻找一种有效的方法来检查两个 2D numpy 数组是否相交。
所以我所拥有的是两个数组,其中包含任意数量的二维数组,例如:
A=np.array([[2,3,4],[5,6,7],[8,9,10]])
B=np.array([[5,6,7],[1,3,4]])
C=np.array([[1,2,3],[6,6,7],[10,8,9]])
如果至少有一个向量与另一个数组中的另一个向量相交,我只需要一个 True ,否则为 false。 所以它应该给出这样的结果:
f(A,B) -> True
f(A,C) -> False
我对 python 有点陌生,起初我用 python 列表编写了我的程序,它可以工作,但当然效率非常低。 该程序需要数天才能完成,所以我现在正在研究numpy.array
解决方案,但这些数组确实不是那么容易处理。
这是有关我的程序和 Python 列表解决方案的一些上下文:
我正在做的是类似于 3 维中的自我避免随机游走。 http://en.wikipedia.org/wiki/Self-avoiding_walk 。 但是,我没有进行随机游走并希望它达到理想的长度(例如,我希望链由 1000 个珠子组成)而不会走到死胡同,我执行以下操作:
我创建了一个具有所需长度 N 的“扁平”链:
X=[]
for i in range(0,N+1):
X.append((i,0,0))
现在我折叠这条扁平链:
步骤 1.-6。 必须执行大量的时间(例如,对于长度为 1000,~5000 次的链),因此必须有效地完成这些步骤。 我的基于列表的解决方案如下:
def PivotFold(chain):
randPiv=random.randint(1,N) #Chooses a random pivotelement, N is the Chainlength
Pivot=chain[randPiv] #get that pivotelement
C=[] #C is going to be a shifted copy of the chain
intersect=False
for j in range (0,N+1): # Here i shift the hole chain to get the pivotelement to the origin, so i can use simple rotations around the origin
C.append((chain[j][0]-Pivot[0],chain[j][1]-Pivot[1],chain[j][2]-Pivot[2]))
rotRand=random.randint(1,18) # rotRand is used to choose a direction and a Rotation (2 possible direction * 9 rotations = 18 possibilitys)
#Rotations around Z-Axis
if rotRand==1:
for j in range (randPiv,N+1):
C[j]=(-C[j][1],C[j][0],C[j][2])
if C[0:randPiv].__contains__(C[j])==True:
intersect=True
break
elif rotRand==2:
for j in range (randPiv,N+1):
C[j]=(C[j][1],-C[j][0],C[j][2])
if C[0:randPiv].__contains__(C[j])==True:
intersect=True
break
...etc
if intersect==False: # return C if there was no intersection in C
Shizz=C
else:
Shizz=chain
return Shizz
函数 PivotFold(chain) 将在最初平坦的链 X 上大量使用。 它写得非常天真,所以也许你有一些技巧可以改进这个 ^^ 我认为 numpyarrays 会很好,因为我可以有效地移动和旋转整个链而无需循环遍历所有元素......
这应该这样做:
In [11]:
def f(arrA, arrB):
return not set(map(tuple, arrA)).isdisjoint(map(tuple, arrB))
In [12]:
f(A, B)
Out[12]:
True
In [13]:
f(A, C)
Out[13]:
False
In [14]:
f(B, C)
Out[14]:
False
找交点? 好的, set
听起来是一个合乎逻辑的选择。 但是numpy.array
或list
不是可散列的吗? 好的,将它们转换为tuple
。 这就是想法。
一种numpy
的做法涉及非常难以阅读的广播:
In [34]:
(A[...,np.newaxis]==B[...,np.newaxis].T).all(1)
Out[34]:
array([[False, False],
[ True, False],
[False, False]], dtype=bool)
In [36]:
(A[...,np.newaxis]==B[...,np.newaxis].T).all(1).any()
Out[36]:
True
一些时间结果:
In [38]:
#Dan's method
%timeit set_comp(A,B)
10000 loops, best of 3: 34.1 µs per loop
In [39]:
#Avoiding lambda will speed things up
%timeit f(A,B)
10000 loops, best of 3: 23.8 µs per loop
In [40]:
#numpy way probably will be slow, unless the size of the array is very big (my guess)
%timeit (A[...,np.newaxis]==B[...,np.newaxis].T).all(1).any()
10000 loops, best of 3: 49.8 µs per loop
此外, numpy
方法将A[...,np.newaxis]==B[...,np.newaxis].T
RAM,因为A[...,np.newaxis]==B[...,np.newaxis].T
步骤创建了一个 3D 数组。
使用此处概述的相同想法,您可以执行以下操作:
def make_1d_view(a):
a = np.ascontiguousarray(a)
dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
return a.view(dt).ravel()
def f(a, b):
return len(np.intersect1d(make_1d_view(A), make_1d_view(b))) != 0
>>> f(A, B)
True
>>> f(A, C)
False
这不适用于浮点类型(它不会将 +0.0 和 -0.0 视为相同的值),并且np.intersect1d
使用排序,因此它具有线性而非线性的性能。 您可以通过在代码中复制np.intersect1d
的源来压缩一些性能,而不是检查返回数组的长度, np.any
在布尔索引数组上调用np.any
。
您还可以通过一些np.tile
和np.swapaxes
业务完成工作!
def intersect2d(X, Y):
"""
Function to find intersection of two 2D arrays.
Returns index of rows in X that are common to Y.
"""
X = np.tile(X[:,:,None], (1, 1, Y.shape[0]) )
Y = np.swapaxes(Y[:,:,None], 0, 2)
Y = np.tile(Y, (X.shape[0], 1, 1))
eq = np.all(np.equal(X, Y), axis = 1)
eq = np.any(eq, axis = 1)
return np.nonzero(eq)[0]
要更具体地回答这个问题,您只需要检查返回的数组是否为空。
这应该快得多,它不像 for 循环解决方案那样 O(n^2),但它不是完全 numpythonic。 不确定在这里如何更好地利用 numpy
def set_comp(a, b):
sets_a = set(map(lambda x: frozenset(tuple(x)), a))
sets_b = set(map(lambda x: frozenset(tuple(x)), b))
return not sets_a.isdisjoint(sets_b)
如果两个数组设置了子数组,我认为你想要真! 你可以使用这个:
def(A,B):
for i in A:
for j in B:
if i==j
return True
return False
这个问题可以使用numpy_indexed包有效地解决(免责声明:我是它的作者):
import numpy_indexed as npi
len(npi.intersection(A, B)) > 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.