简体   繁体   English

如何(稳健地)在 python 的二维数组上找到局部最小值?

[英]How to (robustly) find a local minimum on a 2D array in python?

I have a 2D array which represent the value of a certain function f(x, y), and I would like to detect the minimum on the array.我有一个二维数组,它代表某个 function f(x, y) 的值,我想检测数组上的最小值。 Generally it just looks like that, so it's easy to spot the minimum.通常它看起来像那样,所以很容易发现最小值。

Example normal minimum示例正常最小值

But sometimes there is a kind of drift, which means that the actual minimum is not the one I'm looking for.但有时会有一种漂移,这意味着实际的最小值不是我正在寻找的那个。

Example failed minimum示例失败的最小值

On the above image the minimum I'm looking for is on the left, but the right part of the image has smaller values.在上图中,我要查找的最小值在左侧,但图像右侧的值较小。

It is really important for me that I get an exact value, precise to the pixel , which is why I can't really use a maximum filter or stuff like that.对我来说,获得一个精确到像素的精确值非常重要,这就是为什么我不能真正使用最大过滤器或类似的东西。 I'm looking for a computationally efficient way to detect this minimum, so I'd rather use an existing method instead of doing my own code.我正在寻找一种计算有效的方法来检测这个最小值,所以我宁愿使用现有的方法而不是编写自己的代码。

To get the index of the smallest value in a 2D array I would suggest something like this:要获取二维数组中最小值的索引,我建议如下:

find_smallest = lambda arr: np.unravel_index(np.argmin(arr),arr.shape)

Maybe you could try some of the solvers in scipy.optimize (for example Nelder-Mead ).也许您可以尝试scipy.optimize中的一些求解器(例如Nelder-Mead )。 These will look for local minima.这些将寻找局部最小值。

Then, to get the minimum that you're looking for, you could try to launch several optimizations from different random points and discard the solutions that end up close to the border of the image.然后,为了获得您正在寻找的最小值,您可以尝试从不同的随机点启动多个优化,并丢弃最终接近图像边界的解决方案。 This is, supposing that your minimum is somewhere away from the border.也就是说,假设您的最小值在远离边界的某个地方。 It's an ugly method and probably not very efficient, but I have a little hope that it could work.这是一种丑陋的方法,可能效率不高,但我有点希望它可以工作。 Also, you'll probably have to tune the parameters.此外,您可能必须调整参数。

Otherwise maybe there is some magic that can be done with the gradient function.否则, 渐变function 可能会带来一些魔力。 I guess you'll have to look for some point that has positive derivatives all around it.我想你必须寻找一些周围都有正导数的点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM