本文整理汇总了Python中lmfit.Minimizer.least_squares方法的典型用法代码示例。如果您正苦于以下问题:Python Minimizer.least_squares方法的具体用法?Python Minimizer.least_squares怎么用?Python Minimizer.least_squares使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lmfit.Minimizer
的用法示例。
在下文中一共展示了Minimizer.least_squares方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bounds
# 需要导入模块: from lmfit import Minimizer [as 别名]
# 或者: from lmfit.Minimizer import least_squares [as 别名]
def test_bounds():
if not HAS_LEAST_SQUARES:
raise nose.SkipTest
p_true = Parameters()
p_true.add('amp', value=14.0)
p_true.add('period', value=5.4321)
p_true.add('shift', value=0.12345)
p_true.add('decay', value=0.01000)
def residual(pars, x, data=None):
amp = pars['amp']
per = pars['period']
shift = pars['shift']
decay = pars['decay']
if abs(shift) > pi/2:
shift = shift - sign(shift)*pi
model = amp*sin(shift + x/per) * exp(-x*x*decay*decay)
if data is None:
return model
return (model - data)
n = 1500
xmin = 0.
xmax = 250.0
random.seed(0)
noise = random.normal(scale=2.80, size=n)
x = linspace(xmin, xmax, n)
data = residual(p_true, x) + noise
fit_params = Parameters()
fit_params.add('amp', value=13.0, max=20, min=0.0)
fit_params.add('period', value=2, max=10)
fit_params.add('shift', value=0.0, max=pi/2., min=-pi/2.)
fit_params.add('decay', value=0.02, max=0.10, min=0.00)
min = Minimizer(residual, fit_params, (x, data))
out = min.least_squares()
assert(out.nfev > 10)
assert(out.nfree > 50)
assert(out.chisqr > 1.0)
print(fit_report(out, show_correl=True, modelpars=p_true))
assert_paramval(out.params['decay'], 0.01, tol=1.e-2)
assert_paramval(out.params['shift'], 0.123, tol=1.e-2)