gpt4 book ai didi

scipy 曲线拟合错误 : Result from function call is not a proper array of floats

转载 作者:行者123 更新时间:2023-12-04 12:26:46 25 4
gpt4 key购买 nike

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

所以没有进展...

有人能告诉我这是哪里出了问题吗?

非常感谢您提前!

M。

最佳答案

stndqu是调用 sympy.solve 的结果,它仍然是一个象征性的对象。打印时看到的数字 qval您的函数中可能是 sympy 浮点数(因此是 numpy 的通用对象)。您应该转换 stndqu在与 scipy.curve_fit 一起使用之前放入一个 numpy 数组:

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

关于scipy 曲线拟合错误 : Result from function call is not a proper array of floats,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18184541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com