Pythin lmfit 库:如何使用最小化器来限制函数调用的数量 [英] Pythin lmfit library: How do I use the minimizer to limit the # of function calls
问题描述
我如何使用 Minimizer 对象来最小化函数调用的数量.我从日志中收到此消息:
函数调用过多(最大设置为 %i)!使用:minimize(func, params, ..., maxfev=NNN) 或设置 leastsq_kws[\'maxfev\'] 来增加这个最大值.'
from numpy import sqrt, pi, exp, loadtxt从 lmfit 导入模型从 lmfit 导入 Minimizer导入 matplotlib.pyplot 作为 pltdata = loadtxt('data/model1d_gauss.dat')x = 数据[:, 0]y = 数据[:, 1]def gaussian(x, amp, cen, wid):一维高斯:高斯(x,amp,cen,wid)"返回 (amp/(sqrt(2*pi)*wid)) * exp(-(x-cen)**2/(2*wid**2))gmodel = 模型(高斯)结果 = gmodel.fit(y, x=x, amp=5, cen=1, wid=1,method='least_squares')打印(result.fit_report())r = result.fit_report()plt.plot(x, y, 'bo')plt.plot(x, result.init_fit, 'k--')plt.plot(x, result.best_fit, 'r-')plt.show()
我想通过最小化函数调用来使拟合更快(无论如何,很多数据都是嘈杂的废话)
不确定我是否会推荐这个,但因为你使用的是 scipy 的 least_squares 在内部,您可以只传递所需的选项,例如:
result = gmodel.fit(y, x=x, amp=5, cen=1, wid=1,method='least_squares',fit_kws={'max_nfev': 100})
当然,这是假设,lmfit 会在观察到 maxiter 到达
状态后接受该状态.但从您的问题听起来,这仅被视为警告.
请记住,此优化器基于容差标准(请参阅文档),并且当给定 x
步数不收敛时,它实际上 认为它仍然可以改进最小化!
正如评论中所问:是的,您也可以更改这些标准,例如通过这样做:
result = gmodel.fit(y, x=x, amp=5, cen=1, wid=1,method='least_squares',fit_kws={'ftol': 1-07, # 默认 1e-08'xtol': 1-07, # 1e-08'gtol': 1-07}) # 1e-08
How do I use the Minimizer object to minimize the # of function calls. I get this message from the logs:
Too many function calls (max set to %i)! Use: minimize(func, params, ..., maxfev=NNN)or set leastsq_kws[\'maxfev\'] to increase this maximum.'
from numpy import sqrt, pi, exp, loadtxt
from lmfit import Model
from lmfit import Minimizer
import matplotlib.pyplot as plt
data = loadtxt('data/model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1]
def gaussian(x, amp, cen, wid):
"1-d gaussian: gaussian(x, amp, cen, wid)"
return (amp/(sqrt(2*pi)*wid)) * exp(-(x-cen)**2 /(2*wid**2))
gmodel = Model(gaussian)
result = gmodel.fit(y, x=x, amp=5, cen=1, wid=1,method= 'least_squares')
print(result.fit_report())
r = result.fit_report()
plt.plot(x, y, 'bo')
plt.plot(x, result.init_fit, 'k--')
plt.plot(x, result.best_fit, 'r-')
plt.show()
I want to make the fitting faster by minimizing the function calls (lots of the data is noisy crap anyway)
Not sure if i would recommend this, but as you are using scipy's least_squares internally, you could just pass the options needed, e.g.:
result = gmodel.fit(y, x=x, amp=5, cen=1, wid=1,method='least_squares',
fit_kws={'max_nfev': 100})
Of course this assumes, that lmfit will accept the state after observing the maxiter reached
status. But from your question it sounds, this is treated as warning only.
Keep in mind, that this optimizer is based on tolerance-criterions (see docs) and when not converging given x
number of steps, it actually thinks it can still improve the minimization!
As asked in the comment: yes you can change those criterions too, e.g. by doing:
result = gmodel.fit(y, x=x, amp=5, cen=1, wid=1,method='least_squares',
fit_kws={'ftol': 1-07, # default 1e-08
'xtol': 1-07, # 1e-08
'gtol': 1-07}) # 1e-08
这篇关于Pythin lmfit 库:如何使用最小化器来限制函数调用的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!