我试图通过两个模型函数来拟合两个数据集。我尝试使用symfit
. 这里的代码:
from symfit import variables, parameters, Fit, cos, sin, pi, sqrt, asin
import numpy as np
n0 = 1.5
data = np.genfromtxt('some data')
data = 1000 * data
pos=[]
for j in range( len(data) ):
pos.append( np.arcsin( np.sin( np.deg2rad( data[j,0]/1000 ) )/1.5 ) )
m1=[]
for j in range( len(data) ):
m1.append( data[j,1] )
p1=[]
for j in range( len(data) ):
p1.append(data[j,3])
zero=[]
for j in range( len(data) ):
zero.append(data[j,5])
y0, lam, d0, deltan, per = parameters('y0, lam, d0, deltan, per')
theta, rM1, rP1 = variables('theta, rM1, rP1')
model_dict={
rM1: y0+((pi*deltan*d0)/(lam*cos(theta)))**2.*sin(sqrt(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(-asin(lam/(2*per*n0))-theta))/per)**2.))**2./(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(-asin(lam/(2*per*n0))-theta))/per)**2.),
rP1: y0+((pi*deltan*d0)/(lam*cos(theta)))**2.*sin(sqrt(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(asin(lam/(2*per*n0))-theta))/per)**2.))**2./(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(asin(lam/(2*per*n0))-theta))/per)**2.)
}
fit = Fit(model_dict, theta=pos, rM1=m1, rP1=p1)
fit_result = fit.execute()
print(fit_result)
但是,我收到以下错误:
AttributeError: 'Variable' object has no attribute 'cos'
如果我删除 cos 函数,那么我会得到其他类似的错误sqrt
,sin
等等。我无法弄清楚代码有什么问题!
PS:使用symfit.cos
等后,我得到以下结果:
/anaconda2/lib/python2.7/site-packages/numpy/__init__.py:1: RuntimeWarning: invalid value encountered in arcsin
也
/anaconda2/lib/python2.7/site-packages/numpy/__init__.py:1: RuntimeWarning: invalid value encountered in sqrt
输出是:
Parameter Value Standard Deviation
d0 1.727015e+00 nan
deltan 1.880867e-02 3.534201e-03
lam 3.890715e-01 nan
per 6.123022e-01 nan
y0 -1.686541e-03 4.810316e-03
Fitting status message: Desired error not necessarily achieved due to precision loss.
Number of iterations: 105
Regression Coefficient: 0.100163679536
添加标准偏差后,我得到:
Parameter Value Standard Deviation
d0 nan nan
deltan nan nan
lam nan nan
per nan nan
y0 nan nan
Fitting status message: Desired error not necessarily achieved due to precision loss.
Number of iterations: 112
Regression Coefficient: nan