python多項(xiàng)式擬合之np.polyfit 和 np.polyld詳解
python數(shù)據(jù)擬合主要可采用numpy庫,庫的安裝可直接用pip install numpy等。
1. 原始數(shù)據(jù):假如要擬合的數(shù)據(jù)yyy來自sin函數(shù),np.sin
import numpy as np import matplotlib.pyplot as plt xxx = np.arange(0, 1000) # x值,此時(shí)表示弧度 yyy = np.sin(xxx*np.pi/180) #函數(shù)值,轉(zhuǎn)化成度
2. 測試不同階的多項(xiàng)式,例如7階多項(xiàng)式擬合,使用np.polyfit擬合,np.polyld得到多項(xiàng)式系數(shù)
z1 = np.polyfit(xxx, yyy, 7) # 用7次多項(xiàng)式擬合,可改變多項(xiàng)式階數(shù); p1 = np.poly1d(z1) #得到多項(xiàng)式系數(shù),按照階數(shù)從高到低排列 print(p1) #顯示多項(xiàng)式
3. 求對應(yīng)xxx的各項(xiàng)擬合函數(shù)值
yvals=p1(xxx) # 可直接使用yvals=np.polyval(z1,xxx)
4. 繪圖如下
plt.plot(xxx, yyy, '*',label='original values')
plt.plot(xxx, yvals, 'r',label='polyfit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4) # 指定legend在圖中的位置,類似象限的位置
plt.title('polyfitting')
plt.show()
5. np.polyfit函數(shù):采用的是最小二次擬合,numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False),前三個(gè)參數(shù)是必須的
官方文檔:https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.polyfit.html
6. np.polyld函數(shù):得到多項(xiàng)式系數(shù),主要有三個(gè)參數(shù)
A one-dimensional polynomial class.
A convenience class, used to encapsulate "natural" operations on
polynomials so that said operations may take on their customary
form in code (see Examples).
Parameters
----------
c_or_r : array_like
The polynomial's coefficients, in decreasing powers, or if
the value of the second parameter is True, the polynomial's
roots (values where the polynomial evaluates to 0). For example,
``poly1d([1, 2, 3])`` returns an object that represents
:math:`x^2 + 2x + 3`, whereas ``poly1d([1, 2, 3], True)`` returns
one that represents :math:`(x-1)(x-2)(x-3) = x^3 - 6x^2 + 11x -6`.
r : bool, optional
If True, `c_or_r` specifies the polynomial's roots; the default
is False.
variable : str, optional
Changes the variable used when printing `p` from `x` to `variable`
(see Examples).
參數(shù)1表示:在沒有參數(shù)2(也就是參數(shù)2默認(rèn)False時(shí)),參數(shù)1是一個(gè)數(shù)組形式,且表示從高到低的多項(xiàng)式系數(shù)項(xiàng),例如參數(shù)1為[4,5,6]表示:
參數(shù)2表示:為True時(shí),表示將參數(shù)1中的參數(shù)作為根來形成多項(xiàng)式,即參數(shù)1為[4,5,6]時(shí)表示:(x-4)(x-5)(x-6)=0,也就是:
參數(shù)3表示:換參數(shù)標(biāo)識,用慣了x,可以用 t,s之類的
用法:
1. 直接進(jìn)行運(yùn)算,例如多項(xiàng)式的平方,分別得到
xx=np.poly1d([1,2,3]) print(xx) yy=xx**2 #求平方,或者用 xx * xx print(yy)
2. 求值:
yy(1) = 36
3. 求根:即等式為0時(shí)的未知數(shù)值
yy.r
4. 得到系數(shù)形成數(shù)組:
yy.c 為:array([ 1, 4, 10, 12, 9])
5. 返回最高次冪數(shù):
yy.order = 4
6. 返回系數(shù):
yy[0] —— 表示冪為0的系數(shù)
yy[1] —— 表示冪為1的系數(shù)
總結(jié)
以上所述是小編給大家介紹的python多項(xiàng)式擬合之np.polyfit 和 np.polyld詳解,希望對大家有所幫助,也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
使用python-magic和wxPython實(shí)現(xiàn)識別文檔類型
這篇文章主要介紹了如何使用python-magic模塊和wxPython庫創(chuàng)建一個(gè)簡單的文件列表應(yīng)用程序,該應(yīng)用程序可以顯示所選文件夾中文件的類型,需要的可以參考下2023-08-08
Python隨機(jī)生成均勻分布在單位圓內(nèi)的點(diǎn)代碼示例
這篇文章主要介紹了Python隨機(jī)生成均勻分布在單位圓內(nèi)的點(diǎn)代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
pandas 相關(guān)性和正態(tài)性分析的實(shí)踐
當(dāng)我們談?wù)撜龖B(tài)性(Normality)和相關(guān)性(Correlation)時(shí),我們實(shí)際上在嘗試?yán)斫鈹?shù)據(jù)的分布模式和不同變量之間的關(guān)系,本文就來介紹一下pandas 相關(guān)性和正態(tài)性的實(shí)踐,感興趣的可以了解一下2024-07-07
python使用ctypes模塊調(diào)用windowsapi獲取系統(tǒng)版本示例
這篇文章主要介紹了python使用ctypes模塊調(diào)用windowsapi獲取系統(tǒng)版本示例,需要的朋友可以參考下2014-04-04
Python Web框架Pylons中使用MongoDB的例子
這篇文章主要介紹了Python Web框架Pylons中使用MongoDB 的例子,大家參考使用2013-12-12
Python動(dòng)態(tài)演示旋轉(zhuǎn)矩陣的作用詳解
一個(gè)矩陣我們想讓它通過編程,實(shí)現(xiàn)各種花樣的變化怎么辦呢?下面這篇文章主要給大家介紹了關(guān)于Python動(dòng)態(tài)演示旋轉(zhuǎn)矩陣的作用,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
django文檔學(xué)習(xí)之a(chǎn)pplications使用詳解
這篇文章主要介紹了Python文檔學(xué)習(xí)之a(chǎn)pplications使用詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

