Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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/0/performance/5.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 二维阵列中四邻域最大值的求法_Python_Performance_Numpy_Scipy - Fatal编程技术网

Python 二维阵列中四邻域最大值的求法

Python 二维阵列中四邻域最大值的求法,python,performance,numpy,scipy,Python,Performance,Numpy,Scipy,我想找到2D numpy数组中项的四个相邻项的最大值。我提出的第一个解决方案是使用scipy.ndimage.generic_filter: import numpy as np import scipy.ndimage a = np.random.uniform(low=10, high=100, size=(6500,6500)).astype(np.float) def filter2d(footprint_elements): return max(footprint_ele

我想找到2D numpy数组中项的四个相邻项的最大值。我提出的第一个解决方案是使用
scipy.ndimage.generic_filter

import numpy as np
import scipy.ndimage

a = np.random.uniform(low=10, high=100, size=(6500,6500)).astype(np.float)

def filter2d(footprint_elements):
    return max(footprint_elements)

footprint = np.array([[0, 1, 0],
                       [1, 0, 1],
                       [0, 1, 0]])

maxs = scipy.ndimage.generic_filter(a,filter2d, footprint=footprint)
这里使用通用过滤器的问题是速度非常慢,因此我提出了一个更快的解决方案(请注意,边并不重要):

我正在寻找任何可能更快的方法

我不确定考虑这个是否可以提高速度,但我只对特定的项目感兴趣(由另一个数组决定)。例如,在数组
a
中查找这些项的最大邻居,其中数组
b
大于
0

b = np.random.uniform(low=-10, high=10, size=(6500,6500)).astype(np.float)

# need to find maximum neighbour of array a where b > 0

 maxs[b > 0]

您可以使用。它还接受
footprint
参数:

from scipy.ndimage import maximum_filter

maxs = maximum_filter(a, footprint=footprint)
时间:

In [105]: a = np.random.uniform(low=10, high=100, size=(6500,6500))

In [106]: %timeit maxs = maximum_filter(a, footprint=footprint)
858 ms ± 2.13 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [107]: %timeit maxs = np.maximum.reduce([a[:-2, 1:-1], a[1:-1, 2:], a[2:, 1:-1], a[1:-1,:-2]])
1.34 s ± 12.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

因此
maximum\u filter
比对切片应用
np.maximum.reduce
要快一点。

FYI:
np.random.uniform()
返回一个浮点值数组,因此以后无需使用
astype(np.float)
。将filter2d函数编译为c回调函数也值得一试。请看以下示例:
In [105]: a = np.random.uniform(low=10, high=100, size=(6500,6500))

In [106]: %timeit maxs = maximum_filter(a, footprint=footprint)
858 ms ± 2.13 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [107]: %timeit maxs = np.maximum.reduce([a[:-2, 1:-1], a[1:-1, 2:], a[2:, 1:-1], a[1:-1,:-2]])
1.34 s ± 12.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)