我想使用model找到參數A,B,C到數據x,y使用model y= Ax^2 sin(x)/cos(x)^C + B
我想從scipy.optimize使用leastsq,但出現錯誤。 這裏是我的嘗試:使用Levenberg-Marquardt算法查找模型的參數leastsq
x=n.array(x)
y=n.array(y)
model=lambda tpl,x :(tpl[0]*x**2 * n.sin(x))/((n.cos(x)**tpl[2]) *tpl[1])
func=model
err=lambda tpl,x,y: func(tpl,x)-y
init=(3.0,8.0,4.0)
param=scipy.optimize.leastsq(err,init[:],args=(x,y))
print(param[o])
初始化哪裏是我的 「第一次的猜測」 參數A,B,C
錯誤:
Warning (from warnings module):
File "D:/programs/levenberg.pyw", line 21
model=lambda tpl,x :(tpl[0]*(x**2) * n.sin(x))/((n.cos(x)**tpl[2]) *tpl[1])
RuntimeWarning: invalid value encountered in power
Warning (from warnings module):
File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 419
warnings.warn(errors[info][0], RuntimeWarning)
RuntimeWarning: Number of calls to function has reached maxfev = 800.
(array([ nan, nan, nan]), 5)
你會得到什麼錯誤?在哪一行? – matiasg
@matiasq巨大的一個,其中之一:Traceback(最近調用最後一個): 文件「D:/programs/levenberg.pyw」,第30行,在 param = scipy.optimize.leastsq(err,init,args =(x,y)) –
wiedzminYo
或文件「D:/programs/levenberg.pyw」,第22行,在 model = lambda tpl,x:(tpl [0] * x^2 * n.sin(x) )/((n.cos(x)^ tpl [2])* tpl [1]) TypeError:輸入類型不支持ufunc'bitwise_xor',並且輸入不能被安全強制轉換爲任何受支持的類型鑄造規則''安全'' –
wiedzminYo