gpt4 book ai didi

python - 使用 lmfit 进行最小化时如何修复 'The array returned by a function changed size between calls'?

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:09 26 4
gpt4 key购买 nike

在使用 lmfit 进行最小化时,如何修复出现错误“函数返回的数组在调用之间更改了大小”的代码?

请在下面找到我的代码:

import numpy as np
import pandas as pd
import lmfit as lf

#model needs to be fitted
x0 = 75
def func(params, x, Tsky):
A = params['amp']
w = params['width']
t = params['thickness']
v0 = params['mid_freq']
b0 = params['b0']
b1 = params['b1']
b2 = params['b2']
b3 = params['b3']
b4 = params['b4']
B = (4 * (x - v0)**2. / w**2.) * np.log(-1./t * np.log((1 + np.exp(-t))/2))
T21 = -A * (1 - np.exp(-t * np.exp(B)))/(1 - np.exp(-t))
model = T21 + b0 * ((x/x0)**(-2.5 + b1 + b2 * np.log(x/x0))) * np.exp(-b3*(x/x0)**-2.) + b4 * (x/x0)**-2.
return (Tsky-model)

#read the data
df = pd.read_csv('figure1_plotdata.csv')
data_list = df.T.values.tolist()
xdata = np.array(data_list[0])
Tsky = np.array(data_list[2])

#initial value of the parameters
params = lf.Parameters()
params.add('amp', value=0.2)
params.add('width', value=10)
params.add('thickness', value=5)
params.add('mid_freq', value=70)
params.add('b0', value=500)
params.add('b1', value=-0.5)
params.add('b2', value=-0.5)
params.add('b3', value=-0.5)
params.add('b4', value=500)

#minimize the function
out = lf.minimize(func, params, args=(xdata, Tsky), method='leastsq', kws=None, iter_cb=None, scale_covar=True, nan_policy='omit', calc_covar=True)

print(lf.fit_report(out))

这是错误消息:

File "/home/ankita/Dropbox/Python/Bowman_work/min.py", line 81, in <module>
out = lf.minimize(func, params, args=(xdata, Tsky), method='leastsq', kws=None, iter_cb=None, scale_covar=True, nan_policy='omit', calc_covar=True)

File "/home/ankita/anaconda3/lib/python3.7/site-packages/lmfit-0.9.13-py3.7.egg/lmfit/minimizer.py", line 2300, in minimize
return fitter.minimize(method=method)

File "/home/ankita/anaconda3/lib/python3.7/site-packages/lmfit-0.9.13-py3.7.egg/lmfit/minimizer.py", line 1949, in minimize
return function(**kwargs)

File "/home/ankita/anaconda3/lib/python3.7/site-packages/lmfit-0.9.13-py3.7.egg/lmfit/minimizer.py", line 1492, in leastsq
lsout = scipy_leastsq(self.__residual, variables, **lskws)

File "/home/ankita/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py", line 394, in leastsq
gtol, maxfev, epsfcn, factor, diag)

**ValueError: The array returned by a function changed size between calls**

最佳答案

如果您使用过

out = lf.minimize(func, params,...,nan_policy='raise')

您会看到引发异常,告诉您存在 NaN。当您使用nan_policy='omit'时,模型生成的任何此类 NaN 都会从残差数组中删除,因此数组的大小在调用之间会发生变化。拟合无法处理 NaN 或数组大小的更改 - 您必须消除它们。

特别是,np.log(x)x<0 时为 NaN 。您有np.log()具有复杂的参数,该参数取决于参数 t 的值。如果该参数对于 t 的某个值低于 0 ,模型有 NaN 并且没有意义。您必须确保该参数不能低于 0。可能是这样使用

params.add('thickness', value=5, min=0)

就足够了。但您应该更详细地检查您的模型并确定这是否有意义。

你的模型对我来说看起来相当复杂。我无法猜测这样的模型从何而来。取多个np.exp()np.log()有点要求数值不稳定性。所以,我不知道简单地强制 t积极的态度会带来良好的契合度,但它可能会为您指明正确的方向。

关于python - 使用 lmfit 进行最小化时如何修复 'The array returned by a function changed size between calls'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56966899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com