python拟合曲线误差分析_python曲线拟合不能给出合理的拟合结果
您錯誤地調(diào)用了curve_fit,下面是用法curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, **kw)f是您的函數(shù),它的第一個參數(shù)是一組自變量,其后續(xù)參數(shù)是函數(shù)參數(shù)(如振幅、中心等)
擴展數(shù)據(jù)是自變量
ydata是依賴變量
p0是對函數(shù)參數(shù)的初始猜測(對于Guassian,這是振幅、寬度和中心)
默認情況下,p0被設(shè)置為一個1的列表[1,1,…],這可能就是為什么您得到的結(jié)果是,fit永遠不會執(zhí)行,因為您調(diào)用不正確。在
嘗試從數(shù)據(jù)中估計振幅、中心和寬度,然后制作一個p0對象(詳見下文)
^{pr2}$
下面是一個簡短的例子xdata = np.linspace(0, 4, 50)
mygauss = ( 10,2,0.5) #( amp, center, width)
y = func(xdata, *mygauss ) # using your func defined above
ydata = y + 2*(np.random.random(50)- 0.5) # add some noise to create fake data
現(xiàn)在我能猜出合適的參數(shù)了ai = np.max( ydata) # guess the amplitude
xi = xdata[ np.argmax( ydata)] # guess the position of center
猜測寬度是很棘手的,我首先會找到半最大值的位置(有兩個,但您只需要找到一個,因為高斯是對稱的):pos_half = argmin( np.abs( ydata-ao/2 ) ) # subtract half the amplitude and find the minimum
現(xiàn)在評估這是從高斯中心(席)的距離:sig_i = np.abs( xi - xdata[ pos_half] ) # estimate the width
現(xiàn)在你可以做出初步的猜測了init_guess = (ai, xi sig_i)
合身params, variance = curve_fit( func, xdata=xdata, ydata=ydata, p0=init_guess)
print params
#array([ 9.99457443, 2.01992858, 0.49599629])
非常接近mygauss。希望有幫助。在
總結(jié)
以上是生活随笔為你收集整理的python拟合曲线误差分析_python曲线拟合不能给出合理的拟合结果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站改成静态页面打不开_稳定网站排名的基
- 下一篇: python能够接收由键盘输入的函数是_