我有以下代码:
import numpy as np
import scipy.integrate as spi
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import math as mh
def GUFunction(z, Omega_Lambda):
integral = spi.quad(lambda zvar: AuxIntegrandum(zvar, Omega_Lambda), 0.0, z)[0]
DL = (1+z) * c/H0 * integral *1000000
return (5*(mh.log(DL,10)-1))
def AuxIntegrandum(z, Omega_Lambda):
Omega_m = 1 - Omega_Lambda
return 1 / mh.sqrt(Omega_m*(1+z)**3 + Omega_Lambda)
def DataFit(filename):
print curve_fit(GUFunction, ComputeData(filename)[0], ComputeData(filename)[1])
DataFit("data.dat")
data.dat在第一列中有z值,在第二列中有GUF(z)值。
执行此代码时,编译器告诉我将数组与值(+ inf或-inf)进行比较是不明确的。
我认为这是指集成边界,它看起来是否要集成到无穷大。由于某种原因,它显然将数据文件中的所有z值放入集成边界
是否有一些技巧我不知道哪个允许你将曲线拟合到数值积分函数中?
这是确切的错误:
Traceback (most recent call last):
File "plot.py", line 83, in <module>
DataFit("data.dat")
File "plot.py", line 67, in DataFit
print curve_fit(GUFunction, ComputeData(filename)[0], ComputeData(filename)[1])
File "/home/joshua/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 736, in curve_fit
res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
File "/home/joshua/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 377, in leastsq
shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
File "/home/joshua/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 26, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "/home/joshua/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 454, in func_wrapped
return func(xdata, *params) - ydata
File "plot.py", line 57, in GUFunction
integral = spi.quad(lambda zvar: AuxIntegrandum(zvar, Omega_Lambda), 0.0, z)[0]
File "/home/joshua/anaconda2/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 323, in quad
points)
File "/home/joshua/anaconda2/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 372, 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()
答案 0 :(得分:1)
简答:curve_fit
尝试评估xdata数组上的目标函数,但quad
不能接受vector参数。您需要通过以下方式定义目标函数:对输入数组的列表理解。
让我们做一个可重复的最小例子:
In [33]: xdata = np.linspace(0, 3, 11)
In [34]: ydata = xdata**3
In [35]: def integr(x):
...: return quad(lambda t: t**2, 0, x)[0]
...:
In [36]: def func(x, a):
...: return integr(x) * a
...:
In [37]: curve_fit(func, xdata, ydata)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-37-4660c65f85a2> in <module>()
----> 1 curve_fit(func, xdata, ydata)
[... removed for clarity ...]
~/virtualenvs/py35/lib/python3.5/site-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
370 def _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points):
371 infbounds = 0
--> 372 if (b != Inf and a != -Inf):
373 pass # standard integration
374 elif (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()
这正是您所看到的错误。好的,错误来自quad
,它会尝试评估func(xdata, a)
,其归结为integr(xdata)
,但不起作用。 (我是如何找到它的?我将import pdb; pdf.set_trace()
置于func
函数内并在调试器中调试。)
然后,让我们使目标函数处理数组参数:
In [38]: def func2(x, a):
...: return np.asarray([integr(xx) for xx in x]) * a
...:
In [39]: curve_fit(func2, xdata, ydata)
Out[39]: (array([ 3.]), array([[ 3.44663413e-32]]))