scipy curve_fit错误:函数调用的结果不是正确的浮点数组

时间:2013-08-12 10:18:35

标签: scipy

我有一个[x,y]数据集,我想要一个函数。 这是x和y

parang = np.array([  61.1725  ,   62.140625,   62.93275 ,   63.701625,   65.89225 ,
     66.476875,   68.33525 ,   68.902375,   72.03975 ,   72.590375,
     73.144125,   73.670625,   80.36525 ,   80.80275 ,   87.505375,
     87.90375 ,  100.557875,  100.8915  ])

q      = np.array([-0.03699417, -0.03451252, -0.03851238, -0.0393034 , -0.04059193,
   -0.03941371, -0.04206476, -0.04153004, -0.04721763, -0.04667099,
   -0.03996427, -0.03872865, -0.05054322, -0.0466561 , -0.05476921,
   -0.05274144, -0.0474299 , -0.04974607])

然后我想为一个函数拟合如下数据:

def fq(x,bq,cuq):
    qval = bq*stndqu[0]*np.cos(np.radians(2*x))+cuq*stndqu[1]*np.sin(np.radians(2*x))
    print qval
    print qval.dtype
    return qval

其中'bq,cuq'是我需要拟合的参数,stndqu是我得到的全局参数:

stnd    = input(r'P ($\%$) and $\theta$ of pol. standard? (as tuple)')
p       = stnd[0]/100.
ang     = np.radians(stnd[1])  
x,y     = sympy.symbols('x y')  
stndqu  = sympy.solve([sympy.sqrt(x**2+y**2)-p,(0.5*sympy.atan(y/x))-ang],[x,y])[1] 

和P和theta是2.73和95.我从那个区块出来的stndqu [0]和stndqu [1]是 0.0272334985720932和0.00190435173321445

要找到适合我数据的函数的参数'bq'和'cuq',我会这样做:

qpopt,pconv   = scio.curve_fit(fq, parang, q)

这是结果:

[-0.0129614827538107 -0.0137658898997091 -0.0144124082012406
 -0.0150294169782742 -0.0167265263727253 -0.0171633151430064
 -0.0185034265676582 -0.0188971421096823 -0.0209373417940197
 -0.0212701779430718 -0.0215969783128203 -0.0219002154908251
 -0.0250793309165333 -0.0252411052388773 -0.0269646924974054
 -0.0270214005655701 -0.0260909416985902 -0.0259956074319825]
object
[-0.0129614827538107 -0.0137658898997091 -0.0144124082012406
 -0.0150294169782742 -0.0167265263727253 -0.0171633151430064
 -0.0185034265676582 -0.0188971421096823 -0.0209373417940197
 -0.0212701779430718 -0.0215969783128203 -0.0219002154908251
 -0.0250793309165333 -0.0252411052388773 -0.0269646924974054
 -0.0270214005655701 -0.0260909416985902 -0.0259956074319825]
object
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: array cannot be safely cast to required type
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/Users/mj/Documents/NACO/VLT/DataReduction/<ipython-input-57-cac353117232> in <module>()
----> 1 qpopt,pconv   = scio.curve_fit(fq, parang, q)

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scipy/optimize/minpack.pyc in curve_fit(f, xdata, ydata, p0, sigma, **kw)
    408    # Remove full_output from kw, otherwise we're passing it in twice.

    409    return_full = kw.pop('full_output', False)
--> 410    res = leastsq(func, p0, args=args, full_output=1, **kw)
    411    (popt, pcov, infodict, errmsg, ier) = res
    412 

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scipy/optimize/minpack.pyc in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag, warning)
    268    if (maxfev == 0):
    269        maxfev = 200*(n+1)
--> 270        retval = _minpack._lmdif(func,x0,args,full_output,ftol,xtol,gtol,maxfev,epsfcn,factor,diag)
    271    else:
    272        if col_deriv:

error: Result from function call is not a proper array of floats.

我尝试指定qval元素的类型

def fq(x,bq,cuq):
    qval = np.array(
           bq*stndqu[0]*np.cos(np.radians(2*x))+cuq*stndqu[1]*np.sin(np.radians(2*x)),
    dtype=float)

然后结果变为:

qpopt   = scio.curve_fit(fq, parang, q)
[-0.01296148 -0.01376589 -0.01441241 -0.01502942 -0.01672653 -0.01716332
 -0.01850343 -0.01889714 -0.02093734 -0.02127018 -0.02159698 -0.02190022
 -0.02507933 -0.02524111 -0.02696469 -0.0270214  -0.02609094 -0.02599561]
float64
[-0.01296148 -0.01376589 -0.01441241 -0.01502942 -0.01672653 -0.01716332
 -0.01850343 -0.01889714 -0.02093734 -0.02127018 -0.02159698 -0.02190022
 -0.02507933 -0.02524111 -0.02696469 -0.0270214  -0.02609094 -0.02599561]
float64
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: array cannot be safely cast to required type
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/Users/mj/Documents/NACO/VLT/DataReduction/<ipython-input-50-1f4d3764f7ae> in <module>()
----> 1 qpopt   = scio.curve_fit(fq, parang, q)

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scipy/optimize/minpack.pyc in curve_fit(f, xdata, ydata, p0, sigma, **kw)
    408    # Remove full_output from kw, otherwise we're passing it in twice.

    409    return_full = kw.pop('full_output', False)
--> 410    res = leastsq(func, p0, args=args, full_output=1, **kw)
    411    (popt, pcov, infodict, errmsg, ier) = res
    412 

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scipy/optimize/minpack.pyc in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag, warning)
    268     if (maxfev == 0):
    269         maxfev = 200*(n+1)
--> 270         retval = _minpack._lmdif(func,x0,args,full_output,ftol,xtol,gtol,maxfev,epsfcn,factor,diag)
    271     else:
    272         if col_deriv:

error: Result from function call is not a proper array of floats.

所以没有进展......

有人能告诉我哪里出错了吗?

非常感谢你!

微米。

1 个答案:

答案 0 :(得分:2)

由于stndqu是调用sympy.solve的结果,因此它仍然是一个符号对象。从函数中打印qval时看到的数字可能是sympy浮点数(因此通用对象为numpy)。在将stndquscipy.curve_fit一起使用之前,您应该将stndqun = numpy.array([sympy.N(i) for i in stndqu]) 转换为numpy数组:

{{1}}