利用python做數據擬合詳情_第1頁
利用python做數據擬合詳情_第2頁
利用python做數據擬合詳情_第3頁
利用python做數據擬合詳情_第4頁
利用python做數據擬合詳情_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第利用python做數據擬合詳情目錄1、例子:擬合一種函數Func,此處為一個指數函數。2.例子:擬合一個Gaussian函數3.用一個lmfit的包來實現(xiàn)2中的Gaussian函數擬合

1、例子:擬合一種函數Func,此處為一個指數函數。

出處:

SciPyv1.1.0ReferenceGuide

#Header

importnumpyasnp

importmatplotlib.pyplotasplt

fromscipy.optimizeimportcurve_fit

#Defineafunction(hereaexponentialfunctionisused)

deffunc(x,a,b,c):

returna*np.exp(-b*x)+c

#Createthedatatobefitwithsomenoise

xdata=np.linspace(0,4,50)

y=func(xdata,2.5,1.3,0.5)

np.random.seed(1729)

y_noise=0.2*np.random.normal(size=xdata.size)

ydata=y+y_noise

plt.plot(xdata,ydata,'bo',label='data')

#Fitfortheparametersa,b,cofthefunctionfunc:

popt,pcov=curve_fit(func,xdata,ydata)

popt#output:array([2.55423706,1.35190947,0.47450618])

plt.plot(xdata,func(xdata,*popt),'r-',

label='fit:a=%5.3f,b=%5.3f,c=%5.3f'%tuple(popt))

#Inthecaseofparametersa,b,cneedbeconstrainted

#Constraintheoptimizationtotheregionof

#0=a=3,0=b=1and0=c=0.5

popt,pcov=curve_fit(func,xdata,ydata,bounds=(0,[3.,1.,0.5]))

popt#output:array([2.43708906,1.,0.35015434])

plt.plot(xdata,func(xdata,*popt),'g--',

label='fit:a=%5.3f,b=%5.3f,c=%5.3f'%tuple(popt))

#Labels

plt.title("ExponentialFunctionFitting")

plt.xlabel('xcoordinate')

plt.ylabel('ycoordinate')

plt.legend()

leg=plt.legend()#removetheframeofLegend,personalchoice

leg.get_frame().set_linewidth(0.0)#removetheframeofLegend,personalchoice

#leg.get_frame().set_edgecolor('b')#changethecolorofLegendframe

#plt.show()

#Exportfigure

#plt.savefig('fit1.eps',format='eps',dpi=1000)

plt.savefig('fit1.pdf',format='pdf',dpi=1000,figsize=(8,6),facecolor='w',edgecolor='k')

plt.savefig('fit1.jpg',format='jpg',dpi=1000,figsize=(8,6),facecolor='w',edgecolor='k')

上面一段代碼可以直接在spyder中運行。得到的JPG導出圖如下:

2.例子:擬合一個Gaussian函數

出處:LMFIT:Non-LinearLeast-SquaresMinimizationandCurve-FittingforPython

#Header

importnumpyasnp

importmatplotlib.pyplotasplt

fromnumpyimportexp,linspace,random

fromscipy.optimizeimportcurve_fit

#DefinetheGaussianfunction

defgaussian(x,amp,cen,wid):

returnamp*exp(-(x-cen)**2/wid)

#Createthedatatobefitted

x=linspace(-10,10,101)

y=gaussian(x,2.33,0.21,1.51)+random.normal(0,0.2,len(x))

np.savetxt('data.dat',[x,y])#[x,y]isissavedasamatrixof2lines

#Settheinitial(init)valuesofparametersneedtooptimize(best)

init_vals=[1,0,1]#for[amp,cen,wid]

#Definetheoptimizedvaluesofparameters

best_vals,covar=curve_fit(gaussian,x,y,p0=init_vals)

print(best_vals)#output:array[2.273172560.206822761.64512305]

#Plotthecurvewithinitialparametersandoptimizedparameters

y1=gaussian(x,*best_vals)#best_vals,'*'isusedtoread-outthevaluesinthearray

y2=gaussian(x,*init_vals)#init_vals

plt.plot(x,y,'bo',label='rawdata')

plt.plot(x,y1,'r-',label='best_vals')

plt.plot(x,y2,'k--',label='init_vals')

#plt.show()

#Labels

plt.title("GaussianFunctionFitting")

plt.xlabel('xcoordinate')

plt.ylabel('ycoordinate')

plt.legend()

leg=plt.legend()#removetheframeofLegend,personalchoice

leg.get_frame().set_linewidth(0.0)#removetheframeofLegend,personalchoice

#leg.get_frame().set_edgecolor('b')#changethecolorofLegendframe

#plt.show()

#Exportfigure

#plt.savefig('fit2.eps',format='eps',dpi=1000)

plt.savefig('fit2.pdf',format='pdf',dpi=1000,figsize=(8,6),facecolor='w',edgecolor='k')

plt.savefig('fit2.jpg',format='jpg',dpi=1000,figsize=(8,6),facecolor='w',edgecolor='k')

上面一段代碼可以直接在spyder中運行。得到的JPG導出圖如下:

3.用一個lmfit的包來實現(xiàn)2中的Gaussian函數擬合

需要下載lmfit這個包,下載地址:

/project/lmfit/#files

下載下來的文件是.tar.gz格式,在MacOS及Linux命令行中解壓,指令:

將其中的lmfit文件夾復制到當前project目錄下。

上述例子2中生成了data.dat,用來作為接下來的方法中的原始數據。

出處:

ModelingDataandCurveFitting

#Header

importnumpyasnp

importmatplotlib.pyplotasplt

fromnumpyimportexp,loadtxt,pi,sqrt

fromlmfitimportModel

#Importthedataanddefinex,yandthefunction

data=loadtxt('data.dat')

x=data[0,:]

y=data[1,:]

defgaussian1(x,amp,cen,wid):

return(amp/(sqrt(2*pi)*wid))*exp(-(x-cen)**2/(2*wid**2))

#Fitting

gmodel=Model(gaussian1)

result=gmodel.fit(y,x=x,amp=5,cen=5,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論