我正在尝试将一些数据拟合到 python 中的方程中,但我遇到了一些困难。我有等式:
y(t)=yo+a(t-ti)^b+kt
其中a
、和是拟合参数,ti
和是分别表示时间和位移的数组变量。该等式将通过一些迭代很好地适合 gnuplot,但在 python 中适合它会引发以下错误:-b
k
t
disp
ValueError: array must not contain infs or NaNs
完整的堆栈跟踪是:
creep_test.py:246: RuntimeWarning: invalid value encountered in power
fitfunc = lambda p, t: disp_list[0]+(p[0]*(t-p[1])**p[2])+p[3]*t # Target function
Traceback (most recent call last):
File "creep_test.py", line 374, in <module>
main()
File "creep_test.py", line 368, in main
python_fit(filename)
File "creep_test.py", line 256, in python_fit
out = optimize.leastsq(errfunc, p0[:], args=(t, disp,err), full_output=1)
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 338, in leastsq
cov_x = inv(dot(transpose(R),R))
File "/usr/lib/python2.7/dist-packages/scipy/linalg/basic.py", line 285, in inv
a1 = asarray_chkfinite(a)
File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 590, in asarray_chkfinite
"array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs
我发现它ti
是导致问题的术语,如果你已经ti
固定在周围,那么配件就可以工作35.5
。我使用了电子表格,对于t
下的任何值ti
,等式抛出一个#VALUE
(可能是因为它是虚构的)
基本上有没有办法让python像gnuplot一样适应曲线(我假设它忽略了无效的结果)?我用于我的程序的fittiong部分的代码如下:
fitfunc = lambda p, t: disp_list[0]+(p[0]*(t-p[1])**p[2])+p[3]*t # Target function
errfunc = lambda p, t, y, err: (fitfunc(p, t) - y)/(err) # Distance to the target function
err=0.01
p0 = [ 50, 35.5,0.005, 0.001] # Initial guess for the parameters
out = optimize.leastsq(errfunc, p0[:], args=(t, disp,err), full_output=1)
print out[0]
print out[1]
谢谢!!