如何在NumPy中找到光滑多维数组的局部极小值
假设我有一个NumPy数组,里面存的是一个连续可微函数的评估值,我想找出局部最小值。因为没有噪声,所以每个值比它周围所有邻居的值都小的点,都可以算作局部最小值。
我有一个列表推导式,可以用来处理二维数组,但它忽略了边界上的潜在最小值:
import numpy as N
def local_minima(array2d):
local_minima = [ index
for index in N.ndindex(array2d.shape)
if index[0] > 0
if index[1] > 0
if index[0] < array2d.shape[0] - 1
if index[1] < array2d.shape[1] - 1
if array2d[index] < array2d[index[0] - 1, index[1] - 1]
if array2d[index] < array2d[index[0] - 1, index[1]]
if array2d[index] < array2d[index[0] - 1, index[1] + 1]
if array2d[index] < array2d[index[0], index[1] - 1]
if array2d[index] < array2d[index[0], index[1] + 1]
if array2d[index] < array2d[index[0] + 1, index[1] - 1]
if array2d[index] < array2d[index[0] + 1, index[1]]
if array2d[index] < array2d[index[0] + 1, index[1] + 1]
]
return local_minima
不过,这个方法速度比较慢。我还想让它能适用于任意维度的数组。比如,有没有简单的方法可以获取任意维度数组中某个点的所有邻居?或者我是不是在用错方法?我应该使用 numpy.gradient()
吗?
2 个回答
5
试试这个方法来处理二维数据:
import numpy as N
def local_minima(array2d):
return ((array2d <= N.roll(array2d, 1, 0)) &
(array2d <= N.roll(array2d, -1, 0)) &
(array2d <= N.roll(array2d, 1, 1)) &
(array2d <= N.roll(array2d, -1, 1)))
这个方法会给你返回一个类似二维数组的结果,里面会标记出局部最小值的位置,标记为真(True)或假(False),这些局部最小值是指它周围四个邻居的值都比它大。
21
要找到一个任意维度数组中的局部最小值位置,可以使用Ivan的detect_peaks函数,只需要稍微修改一下:
import numpy as np
import scipy.ndimage.filters as filters
import scipy.ndimage.morphology as morphology
def detect_local_minima(arr):
# https://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array/3689710#3689710
"""
Takes an array and detects the troughs using the local maximum filter.
Returns a boolean mask of the troughs (i.e. 1 when
the pixel's value is the neighborhood maximum, 0 otherwise)
"""
# define an connected neighborhood
# http://www.scipy.org/doc/api_docs/SciPy.ndimage.morphology.html#generate_binary_structure
neighborhood = morphology.generate_binary_structure(len(arr.shape),2)
# apply the local minimum filter; all locations of minimum value
# in their neighborhood are set to 1
# http://www.scipy.org/doc/api_docs/SciPy.ndimage.filters.html#minimum_filter
local_min = (filters.minimum_filter(arr, footprint=neighborhood)==arr)
# local_min is a mask that contains the peaks we are
# looking for, but also the background.
# In order to isolate the peaks we must remove the background from the mask.
#
# we create the mask of the background
background = (arr==0)
#
# a little technicality: we must erode the background in order to
# successfully subtract it from local_min, otherwise a line will
# appear along the background border (artifact of the local minimum filter)
# http://www.scipy.org/doc/api_docs/SciPy.ndimage.morphology.html#binary_erosion
eroded_background = morphology.binary_erosion(
background, structure=neighborhood, border_value=1)
#
# we obtain the final mask, containing only peaks,
# by removing the background from the local_min mask
detected_minima = local_min ^ eroded_background
return np.where(detected_minima)
你可以这样使用它:
arr=np.array([[[0,0,0,-1],[0,0,0,0],[0,0,0,0],[0,0,0,0],[-1,0,0,0]],
[[0,0,0,0],[0,-1,0,0],[0,0,0,0],[0,0,0,-1],[0,0,0,0]]])
local_minima_locations = detect_local_minima(arr)
print(arr)
# [[[ 0 0 0 -1]
# [ 0 0 0 0]
# [ 0 0 0 0]
# [ 0 0 0 0]
# [-1 0 0 0]]
# [[ 0 0 0 0]
# [ 0 -1 0 0]
# [ 0 0 0 0]
# [ 0 0 0 -1]
# [ 0 0 0 0]]]
这段代码的意思是,局部最小值出现在索引 [0,0,3]、[0,4,0]、[1,1,1] 和 [1,3,3] 这些位置:
print(local_minima_locations)
# (array([0, 0, 1, 1]), array([0, 4, 1, 3]), array([3, 0, 1, 3]))
print(arr[local_minima_locations])
# [-1 -1 -1 -1]