scipy curve_fit错误:函数调用结果不是有效的浮点数数组。

8

我有一个[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] 

我得到的stndqu[0]和stndqu[1]分别为0.0272334985720932和0.00190435173321445,其中P和theta均为2.73和95。为了找到适合我的数据的函数的参数'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.

所以并没有进展...

可以有人告诉我出了什么问题吗?

非常感谢您的帮助!

M.

1个回答

5

由于stndqu是对sympy.solve的调用结果,因此它仍然是符号对象。从您函数内部打印qval时看到的数字可能是sympy浮点数(因此是numpy的通用对象)。在使用scipy.curve_fit之前,您应将stndqu转换为numpy数组:

stndqun = numpy.array([sympy.N(i) for i in stndqu])

非常感谢!!!现在它可以工作了,只是我不得不指定数组中元素的类型,因为你的代码行导致numpy数组包含dtype=object的元素。这是我添加的内容:stndqun = np.array([sympy.N(i) for i in stndqu],dtype=float) - mjo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接