[英]Fastest method for determining if 2 (vertically or horizontally) adjacent elements of a numpy array have the same value
I am looking for the fastest way of determining if 2 (vertically or horizontally) adjacent elements have the same value. 我正在寻找确定2(垂直或水平)相邻元素是否具有相同值的最快方法。
Let's say I have a numpy array of size 4x4. 假设我有一个大小为4x4的numpy数组。
array([
[8, 7, 4, 3],
[8, 4, 0, 4],
[3, 2, 2, 1],
[9, 8, 7, 6]])
I want to be able to identify that there are two adjacent 8s in the first column and there are two adjacent 2s in the third row. 我希望能够识别出第一列中有两个相邻的8,并且第三行中有两个相邻的2。 I could hard code a check but that would be ugly and I want to know if there is a faster way. 我可以硬编码支票,但那会很难看,我想知道是否有更快的方法。
All guidance is appreciated. 所有指导表示赞赏。 Thank you. 谢谢。
We would look for differentiation values along rows and columns for zeros
signalling repeated ones there. 我们将在行和列中寻找区分值,以便在那里用zeros
表示重复的值。 Thus, we could do - 因此,我们可以做 -
(np.diff(a,axis=0) == 0).any() | (np.diff(a,axis=1) == 0).any()
Or with slicing
for performance boost - 或者通过slicing
提升性能 -
(a[1:] == a[:-1]).any() | (a[:,1:] == a[:,:-1]).any()
So, (a[1:] == a[:-1]).any()
is the vertical adjacency, whereas the other one is for horizontal one. 所以, (a[1:] == a[:-1]).any()
是垂直邻接,而另一个是水平邻接。
n
adjacent ones (of same value) along rows or columns - 沿行或列扩展到n
相邻的(具有相同值) - from scipy.ndimage.filters import convolve1d as conv
def vert_horz_adj(a, n=1):
k = np.ones(n,dtype=int)
v = (conv((a[1:]==a[:-1]).astype(int),k,axis=0,mode='constant')>=n).any()
h = (conv((a[:,1:]==a[:,:-1]).astype(int),k,axis=1,mode='constant')>=n).any()
return v | h
Sample run - 样品运行 -
In [413]: np.random.seed(0)
...: a = np.random.randint(11,99,(10,4))
...: a[[2,3,4,6,7,8],0] = 1
In [414]: a
Out[414]:
array([[55, 58, 75, 78],
[78, 20, 94, 32],
[ 1, 98, 81, 23],
[ 1, 76, 50, 98],
[ 1, 92, 48, 36],
[88, 83, 20, 31],
[ 1, 80, 90, 58],
[ 1, 93, 60, 40],
[ 1, 30, 25, 50],
[43, 76, 20, 68]])
In [415]: vert_horz_adj(a, n=1)
Out[415]: True # Because of first col
In [416]: vert_horz_adj(a, n=2)
Out[416]: True # Because of first col
In [417]: vert_horz_adj(a, n=3)
Out[417]: False
In [418]: a[-1] = 10
In [419]: vert_horz_adj(a, n=3)
Out[419]: True # Because of last row
You can find the coordinates of the pairs with the following code: 您可以使用以下代码找到对的坐标:
import numpy as np
a = np.array([
[8, 7, 4, 3],
[8, 4, 0, 4],
[3, 2, 2, 1],
[9, 8, 7, 6]])
vertical = np.where((a == np.roll(a, 1, 0))[1:-1])
print(vertical) # (0,0) is the coordinate of the first of the repeating 8's
horizontal = np.where((a == np.roll(a, 1, 1))[:, 1:-1])
print(horizontal) # (2,1) is the coordinate of the first of the repeating 2's
which returns 返回
(array([0], dtype=int64), array([0], dtype=int64))
(array([2], dtype=int64), array([1], dtype=int64))
if you want to locate the first occurence of each pair : 如果你想找到每对的第一次出现:
A=array([
[8, 7, 4, 3],
[8, 4, 0, 4],
[3, 2, 2, 1],
[9, 8, 7, 6]])
x=(A[1:]==A[:-1]).nonzero()
y=(A[:,1:]==A[:,:-1]).nonzero()
In [45]: x
Out[45]: (array([0], dtype=int64), array([0], dtype=int64))
In [47]: y
Out[47]: (array([2], dtype=int64), array([1], dtype=int64))
In [48]: A[x]
Out[48]: array([8])
In [49]: A[y]
Out[49]: array([2])
x
and y
give respectively the locations of the first 8 and the first 2. x
和y
分别给出前8和前2的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.