Python: optimize.leastsq. ValueError: The truth value of an array with more than one element is ambiguous
Python: optimize.leastsq. ValueError: The truth value of an array with more than one element is ambiguous
除最后一行外,一切正常。
我的目标是通过卡方检验计算最佳拟合。 leastsq函数的应用有问题。
z,d 和 d_err 是相同长度的数组,给定(实验数据)。
def df(z,omega_m,omega_l):
return 1/(np.sqrt(omega_m*(1+z)**3+(1-omega_m-omega_l)*(1+z)**2+omega_l))
def DL(z,omega_m,omega_l,H_0): # checked with Hubble's law with low z, it is consistent
f,err_f=scipy.integrate.quad(df,0,z,args=(omega_m,omega_l)) # it's evident err_f it's irrelevant
if omega_m+omega_l==1:
return 299792./H_0*(1+z)*f
elif omega_m+omega_l<1:
fk=np.sin(np.sqrt(np.absolute(1-omega_l-omega_m))*f)
return 299792./H_0*(1+z)/np.sqrt(np.absolute(1-omega_m-omega_l))*fk
elif omega_m+omega_l>1:
fk=np.sinh(np.sqrt(np.absolute(1-omega_l-omega_m))*f)
return 299792./H_0*(1+z)/np.sqrt(np.absolute(1-omega_m-omega_l))*fk
params=(0.3,0.7,73) # starting values for minimization omega_m, omega_l, H_0
def chi(params,z,d,d_err): # checked, this function works
return (d-DL(z,params[0],params[1],params[2]))**2/d_err
minimization,minimization_cov=optimize.leastsq(chi,params,args=(z,d,d_err))
这是错误的完整信息:
File "C:\Python34\lib\site-packages\scipy\integrate\quadpack.py", line 360, in _quad
if (b != Inf and a != -Inf): ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
scipy.integrate.quad()
的第三个参数是上限,必须是浮点数。您使用 z
作为 NumPy 数组的第三个参数。
Signature: scipy.integrate.quad(func, a, b, ...)
Integrate func from a
to b
(possibly infinite interval) using a
technique from the Fortran library QUADPACK.
...
a : float
Lower limit of integration (use -numpy.inf for -infinity).
b : float
Upper limit of integration (use numpy.inf for +infinity).
除最后一行外,一切正常。 我的目标是通过卡方检验计算最佳拟合。 leastsq函数的应用有问题。 z,d 和 d_err 是相同长度的数组,给定(实验数据)。
def df(z,omega_m,omega_l):
return 1/(np.sqrt(omega_m*(1+z)**3+(1-omega_m-omega_l)*(1+z)**2+omega_l))
def DL(z,omega_m,omega_l,H_0): # checked with Hubble's law with low z, it is consistent
f,err_f=scipy.integrate.quad(df,0,z,args=(omega_m,omega_l)) # it's evident err_f it's irrelevant
if omega_m+omega_l==1:
return 299792./H_0*(1+z)*f
elif omega_m+omega_l<1:
fk=np.sin(np.sqrt(np.absolute(1-omega_l-omega_m))*f)
return 299792./H_0*(1+z)/np.sqrt(np.absolute(1-omega_m-omega_l))*fk
elif omega_m+omega_l>1:
fk=np.sinh(np.sqrt(np.absolute(1-omega_l-omega_m))*f)
return 299792./H_0*(1+z)/np.sqrt(np.absolute(1-omega_m-omega_l))*fk
params=(0.3,0.7,73) # starting values for minimization omega_m, omega_l, H_0
def chi(params,z,d,d_err): # checked, this function works
return (d-DL(z,params[0],params[1],params[2]))**2/d_err
minimization,minimization_cov=optimize.leastsq(chi,params,args=(z,d,d_err))
这是错误的完整信息:
File "C:\Python34\lib\site-packages\scipy\integrate\quadpack.py", line 360, in _quad
if (b != Inf and a != -Inf): ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
scipy.integrate.quad()
的第三个参数是上限,必须是浮点数。您使用 z
作为 NumPy 数组的第三个参数。
Signature: scipy.integrate.quad(func, a, b, ...)
Integrate func from
a
tob
(possibly infinite interval) using a technique from the Fortran library QUADPACK.
...
a : float
Lower limit of integration (use -numpy.inf for -infinity).
b : float
Upper limit of integration (use numpy.inf for +infinity).