Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 确定numpy数组的2个(垂直或水平)相邻元素是否具有相同值的最快方法_Python_Arrays_Numpy_Multidimensional Array_Scipy - Fatal编程技术网

Python 确定numpy数组的2个(垂直或水平)相邻元素是否具有相同值的最快方法

Python 确定numpy数组的2个(垂直或水平)相邻元素是否具有相同值的最快方法,python,arrays,numpy,multidimensional-array,scipy,Python,Arrays,Numpy,Multidimensional Array,Scipy,我正在寻找确定两个(垂直或水平)相邻元素是否具有相同值的最快方法 假设我有一个4x4大小的numpy数组 array([ [8, 7, 4, 3], [8, 4, 0, 4], [3, 2, 2, 1], [9, 8, 7, 6]]) 我希望能够识别第一列中有两个相邻的8,第三行中有两个相邻的2。我可以硬编码一个检查,但这将是丑陋的,我想知道是否有一个更快的方法 感谢您的指导。谢谢。我们将沿着零的行和列查找微分值

我正在寻找确定两个(垂直或水平)相邻元素是否具有相同值的最快方法

假设我有一个4x4大小的numpy数组

array([             
[8, 7, 4, 3],    
[8, 4, 0, 4],          
[3, 2, 2, 1],              
[9, 8, 7, 6]])
我希望能够识别第一列中有两个相邻的8,第三行中有两个相邻的2。我可以硬编码一个检查,但这将是丑陋的,我想知道是否有一个更快的方法


感谢您的指导。谢谢。

我们将沿着
零的行和列查找微分值。因此,我们可以这样做-

(np.diff(a,axis=0) == 0).any() | (np.diff(a,axis=1) == 0).any()
或使用
切片
提高性能-

(a[1:] == a[:-1]).any() | (a[:,1:] == a[:,:-1]).any()
所以,
(a[1:::==a[:-1])。any()
是垂直邻接,而另一个是水平邻接

沿行或列扩展到相邻的(值相同的)列- 样本运行-

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

您可以使用以下代码找到对的坐标:

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 
返回

(array([0], dtype=int64), array([0], dtype=int64))
(array([2], dtype=int64), array([1], dtype=int64))

如果要定位每对的第一次出现:

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
y
分别给出前8个和前2个的位置。

您希望输出什么?第1栏;第3行?我只是不想知道是否有水平或垂直邻接的二进制输出。发布的解决方案对你有用吗?考虑接受一个+ 1我以前见过可怕的切片转移技巧之前,认为这是非常聪明的,但忘记了如果这个完美的情况下,你的“n个相邻的”实际上是“n-1对相邻的”。这不是同一件事-
11100111
只有三个相邻的,但是你声称有4个。当然
vert_horz_adj(a,n=3)
应该是
True
,因为有3个
1
s?但除此之外,这是一个很好的解决方案。你能在这里使用
np.convolve
而不是使用scipy吗(只需将内核
(1,n)
然后
(n,1)
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])