python 两个数组的交集_Python-2D numpy数组的交集

I'm desperately searching for an efficient way to check if two 2D numpy Arrays intersect.

So what I have is two arrays with an arbitrary amount of 2D arrays like:

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]])

All I need is a True if there is at least one vector intersecting with another one of the other array, otherwise a false. So it should give results like this:

f(A,B) -> True

f(A,C) -> False

I'm kind of new to python and at first I wrote my program with Python lists, which works but of course is very inefficient. The Program takes days to finish so I am working on a numpy.array solution now, but these arrays really are not so easy to handle.

Here's Some Context about my Program and the Python List Solution:

What i'm doing is something like a self-avoiding random walk in 3 Dimensions. http://en.wikipedia.org/wiki/Self-avoiding_walk. But instead of doing a Random walk and hoping that it will reach a desirable length (e.g. i want chains build up of 1000 beads) without reaching a dead end i do the following:

I create a "flat" Chain with the desired length N:

X=[]

for i in range(0,N+1):

X.append((i,0,0))

Now i fold this flat chain:

randomly choose one of the elements ("pivotelement")

randomly choose one direction ( either all elements to the left or to the right of the pivotelment)

randomly choose one out of 9 possible rotations in space (3 axes * 3 possible rotations 90°,180°,270°)

rotate all the elements of the chosen direction with the chosen rotation

Check if the new elements of the chosen direction intersect with the other direction

No intersection -> accept the new configuration, else -> keep the old chain.

Steps 1.-6. have to be done a large amount of times (e.g. for a chain of length 1000, ~5000 Times) so these steps have to be done efficiently. My List-based solution for this is the following:

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

The Function PivotFold(chain) will be used on the initially flat chain X a large amount of times. it's pretty naivly written so maybe you have some protips to improve this ^^ I thought that numpyarrays would be good because i can efficiently shift and rotate entire chains without looping over all the elements ...

解决方案

This should do it:

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

To find intersection? OK, set sounds like a logical choice.

But numpy.array or list are not hashable? OK, convert them to tuple.

That is the idea.

A numpy way of doing involves very unreadable boardcasting:

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

Some timeit result:

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

Also the numpy method will be RAM hungry, as A[...,np.newaxis]==B[...,np.newaxis].T step creates a 3D array.

numpy数组元素周围的操作可以通过以下几种方式实现: 1. 切片操作:可以使用numpy数组的切片操作来获取数组中元素的周围元素。 例如,对于一个二维数组arr,要获取第i行第j列元素周围的元素,可以使用如下切片操作: ```python arr[i-1:i+2, j-1:j+2] ``` 这将返回一个3x3的子数组,其中心元素为arr[i,j],周围的8个元素为该子数组的其余元素。 2. 使用numpy.pad()函数:numpy.pad()函数可以用来在数组的边缘添加一个或多个值,从而扩展数组的大小。可以使用该函数来添加额外的行和列,然后通过索引访问周围的元素。 例如,对于一个二维数组arr,要获取第i行第j列元素周围的元素,可以使用如下代码: ```python padded_arr = np.pad(arr, ((1, 1), (1, 1)), mode='constant') surrounding = padded_arr[i:i+3, j:j+3] ``` 这将在数组的边缘添加一行和一列,并使用常量值填充这些额外的元素。然后可以使用切片操作来获取中心元素周围的元素。 3. 使用numpy.roll()函数:numpy.roll()函数可以用来沿着给定轴滚动数组的元素。可以使用该函数来将数组的行和列进行滚动,从而获取周围的元素。 例如,对于一个二维数组arr,要获取第i行第j列元素周围的元素,可以使用如下代码: ```python rows, cols = arr.shape row_indices = np.arange(i-1, i+2) % rows col_indices = np.arange(j-1, j+2) % cols surrounding = arr[row_indices][:, col_indices] ``` 这将将第i行向上和向下滚动一行,并将第j列向左和向右滚动一列,从而获取中心元素周围的元素。使用模运算可以确保在数组的边缘滚动时正确处理索引。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值