




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第利用python做數(shù)據(jù)擬合詳情目錄1、例子:擬合一種函數(shù)Func,此處為一個(gè)指數(shù)函數(shù)。2.例子:擬合一個(gè)Gaussian函數(shù)3.用一個(gè)lmfit的包來實(shí)現(xiàn)2中的Gaussian函數(shù)擬合
1、例子:擬合一種函數(shù)Func,此處為一個(gè)指數(shù)函數(shù)。
出處:
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中運(yùn)行。得到的JPG導(dǎo)出圖如下:
2.例子:擬合一個(gè)Gaussian函數(shù)
出處: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中運(yùn)行。得到的JPG導(dǎo)出圖如下:
3.用一個(gè)lmfit的包來實(shí)現(xiàn)2中的Gaussian函數(shù)擬合
需要下載lmfit這個(gè)包,下載地址:
/project/lmfit/#files
下載下來的文件是.tar.gz格式,在MacOS及Linux命令行中解壓,指令:
將其中的lmfit文件夾復(fù)制到當(dāng)前project目錄下。
上述例子2中生成了data.dat,用來作為接下來的方法中的原始數(shù)據(jù)。
出處:
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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 氣球充值活動(dòng)方案
- 愛情、親情、友情課件
- 2025市政工程節(jié)后復(fù)工方案
- 油水分離研究
- 樹莓組織培養(yǎng)技術(shù)體系構(gòu)建及優(yōu)化研究
- FLAC3D中錨桿支護(hù)數(shù)值模擬的研究綜述及其在實(shí)際工程中的應(yīng)用探討
- 分析新媒體時(shí)代大眾羽毛球賽事的發(fā)展趨勢(shì)和組織策略
- 數(shù)能金融產(chǎn)品的營(yíng)銷策略矩陣研究
- 提升革命文物保護(hù)與利用的成效
- AI生成圖像版權(quán)侵權(quán)的法律困境與解決路徑探索
- 2025年4月自考03346項(xiàng)目管理試題
- 艾梅乙反歧視培訓(xùn)課件
- 2025安全生產(chǎn)月一把手講安全公開課三十二(91P)
- 2025課件:紅色基因作風(fēng)建設(shè)七一黨課
- 在線網(wǎng)課學(xué)習(xí)課堂《人工智能(北理 )》單元測(cè)試考核答案
- 康復(fù)科護(hù)理管理制度
- 《中國(guó)近現(xiàn)代史綱要(2023版)》課后習(xí)題答案合集匯編
- 國(guó)家綜合性消防救援隊(duì)伍消防員管理規(guī)定
- 第6章_懸移質(zhì)泥沙運(yùn)動(dòng)2014
- 湘美版美術(shù)四年級(jí)下冊(cè)教案-12.色彩取樣1
- (完整版)印章及文件管理指南
評(píng)論
0/150
提交評(píng)論