本文實(shí)例講述了Python實(shí)現(xiàn)的擬合二元一次函數(shù)功能。分享給大家供大家參考,具體如下:

背景:
使用scipy擬合一元二次函數(shù)。
參考:
HYRY Studio-《用Python做科學(xué)計(jì)算》
代碼:
# -*- coding:utf-8 -*-
#! python3
import numpy as np
from scipy.optimize import leastsq
import pylab as pl
def func(x,p):
"""
數(shù)組擬合函數(shù)
"""
A,k,theta = p
return A*(x-k)**2+theta
def residuals(p,y,x):
"""
殘差
"""
return y-func(x,p)
x = np.linspace(0,2,100)
A,k,theta = 10.,1,2. #真實(shí)數(shù)據(jù)參數(shù)
y0 = func(x,[A,k,theta]) #真實(shí)數(shù)據(jù)
y1 = y0 + 2 * np.random.randn(len(x)) #加入噪聲序列
p0 = [7.,0.2,1.]
plsq = leastsq(residuals,p0,args = (y1,x))
print("真實(shí)參數(shù):",[A,k,theta])
print("擬合參數(shù):",plsq[0]) #試驗(yàn)數(shù)據(jù)擬合后的參數(shù)
pl.plot(x,y0,label = "real")
pl.plot(x,y1,label = "real+noise")
pl.plot(x,func(x,plsq[0]),label = "fitting")
pl.legend()
pl.show()
當(dāng)前文章:Python實(shí)現(xiàn)的擬合二元一次函數(shù)功能示例【基于scipy模塊】-創(chuàng)新互聯(lián)
文章起源:http://www.chinadenli.net/article44/deceee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、響應(yīng)式網(wǎng)站、靜態(tài)網(wǎng)站、網(wǎng)站制作、建站公司、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容