SciPy 全局最小曲线拟合
我正在使用 scipy.optimize.curve_fit,但我怀疑它收敛到局部最小值而不是全局最小值。
我尝试按以下方式使用模拟退火:
def fit(params):
return np.sum((ydata - specf(xdata,*params))**2)
p = scipy.optimize.anneal(fit,[1000,1E-10])
其中 specf
是我试图拟合的曲线。尽管返回值表明已达到全局最小值,但 p
中的结果显然比 curve_fit
返回的最小值更差(参见退火)。
我怎样才能改善结果? SciPy 中有全局曲线拟合器吗?
I'm using scipy.optimize.curve_fit
, but I suspect it is converging to a local minimum and not the global minimum.
I tried using simulated annealing in the following way:
def fit(params):
return np.sum((ydata - specf(xdata,*params))**2)
p = scipy.optimize.anneal(fit,[1000,1E-10])
where specf
is the curve I am trying to fit. The results in p
though are clearly worse than the minimum returned by curve_fit
even when the return value indicates the global minimum was reached (see anneal).
How can I improve the results? Is there a global curve fitter in SciPy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是对的,它只收敛到局部最小值(当它收敛时),因为它使用 Levenburg-Marquardt 算法。 SciPy 中没有全局曲线拟合器,您必须使用 编写自己的曲线拟合器现有的全局优化器。但请注意,这仍然不必收敛到您想要的值。在大多数情况下,这是不可能的。
改善结果的唯一方法是很好地猜测起始参数。
You're right, it only converges towards a local minimum (when it converges) since it uses the Levenburg-Marquardt algorithm. There is no global curve fitter in SciPy, you have to write you own using the existing global optimizers . But be aware, that this still don't have to converge to the value you want. That's impossible in most cases.
The only method to improve your result is to guess the starting parameters quite well.
您可能想尝试使用 leastsq() (curve_fit 实际上使用了这个,但你没有得到完整的输出)或 ODR 包< /a> 而不是 curve_fit。
lesssq() 的完整输出为您提供了更多信息,例如卡方值(如果您想将其用作快速而肮脏的拟合优度检验)。
如果您需要权衡适合度,您可以这样做:
You might want to try using leastsq() (curve_fit actually uses this, but you dont get the full output) or the ODR package instead of curve_fit.
The full output of leastsq() gives you a lot more information, such as the chisquared value (if you want to use that as a quick and dirty goodness of fit test).
If you need to weight the fit you can just that this way:
这是一个不平凡的问题。您是否考虑过使用进化策略?我在 ecspy 方面取得了巨大成功(请参阅 http://code.google.com/p/ecspy/< /a>),社区虽小但非常有帮助。
This is a nontrivial problem. Have you considered using Evolutionary Strategies? I have had great success with ecspy (see http://code.google.com/p/ecspy/) and the community is small but very helpful.