[英]difference between scipy.optimize.leastsq and scipy.optimize.least_squares?
I've been running an optimization process using the legacy scipy.optimize.leastsq
Now I want to switch to scipy.optimize.least_squares
(I need to introduce bounds).我一直在使用旧版
scipy.optimize.leastsq
运行优化过程,现在我想切换到scipy.optimize.least_squares
(我需要引入边界)。 But least_squares throws an error which I can't debug.但是least_squares 会抛出一个我无法调试的错误。 Below my code, I am doing exactly the same with
least_squares
as with leastsq
.下面我的代码,我做的完全一样
least_squares
与leastsq
。
import scipy
from scipy.optimize import leastsq, least_squares
print(scipy.__version__)
def residuals_cmrset_as_2009JoH(x0, df):
k_max= x0[0]
a= x0[1]
alpha= x0[2]
b= x0[3]
beta= x0[4]
k_Ei_max= x0[5]
k_CMI= x0[6]
C_CMI= x0[7]
CMI_max= x0[8]
EVI_min= x0[9]
EVI_max= x0[10]
df['aet_cmrset'] = aet_cmrset_as_2009JoH(df.evi, df.gvmi, df.pet, df.rain,
k_max, a, alpha, b, beta, k_Ei_max, k_CMI, C_CMI, CMI_max, EVI_min, EVI_max)
return(df.aet_cmrset - df.AET_observed)
print('run calibration with leastsq')
x, flag = leastsq(residuals_cmrset_as_2009JoH,
np.transpose(x0),
args=(df_calibration))
print('this is the result from leastsq')
print(x)
print('run calibration with least_squares')
x, flag = least_squares(residuals_cmrset_as_2009JoH,
np.transpose(x0),
args=(df_calibration))
print('this is the result from least_squares')
print(x)
and this is the output:这是输出:
1.2.0
run calibration with leastsq
this is the result from leastsq
[ 0.99119625 1.44145154 1.12799561 27.41023799 2.60102797 0.09771226
1.14979708 -0.24298292 1. 0. 0.9 ]
run calibration with least_squares
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-bc305703822b> in <module>
30 x, flag = least_squares(residuals_cmrset_as_2009JoH,
31 np.transpose(x0),
---> 32 args=(df_calibration))
33 print('this is the result from least_squares')
34 print(x)
/apps/python/3.7.2/lib/python3.7/site-packages/scipy-1.2.0-py3.7-linux-x86_64.egg/scipy/optimize/_lsq/least_squares.py in least_squares(fun, x0, jac, bounds, method, ftol, xtol, gtol, x_scale, loss, f_scale, diff_step, tr_solver, tr_options, jac_sparsity, max_nfev, verbose, args, kwargs)
796 x0 = make_strictly_feasible(x0, lb, ub)
797
--> 798 f0 = fun_wrapped(x0)
799
800 if f0.ndim != 1:
/apps/python/3.7.2/lib/python3.7/site-packages/scipy-1.2.0-py3.7-linux-x86_64.egg/scipy/optimize/_lsq/least_squares.py in fun_wrapped(x)
791
792 def fun_wrapped(x):
--> 793 return np.atleast_1d(fun(x, *args, **kwargs))
794
795 if method == 'trf':
TypeError: residuals_cmrset_as_2009JoH() takes 2 positional arguments but 11 were given
Any help will be welcome欢迎任何帮助
Both functions specify that args
is supposed to be a tuple.这两个函数都指定
args
应该是一个元组。 But leastsq
has, near the start this但是
leastsq
有,接近开始这个
if not isinstance(args, tuple):
args = (args,)
I don't see something equivalent in least_squares
.我在
least_squares
没有看到等效的东西。 That step "protects" leastsq
in case the user makes a mistake and passes an array instead of the specified tuple.这一步“保护”了
leastsq
以防用户犯错并传递一个数组而不是指定的元组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.