python實現(xiàn)線性插值的示例
線性插值
插值:是根據(jù)已知的數(shù)據(jù)序列(可以理解為你坐標中一系列離散的點),找到其中的規(guī)律,然后根據(jù)找到的這個規(guī)律,來對其中尚未有數(shù)據(jù)記錄的點進行數(shù)值估計。
線性插值:是針對一維數(shù)據(jù)的插值方法。它根據(jù)一維數(shù)據(jù)序列中需要插值的點的左右臨近兩個數(shù)據(jù)來進行數(shù)值估計。當然了它不是求這兩個點數(shù)據(jù)大小的平均值(在中心點的時候就等于平均值)。而是根據(jù)到這兩個點的距離來分配比重的。
python實現(xiàn)線性插值
numpy.interp
numpy.interp(x, xp, fp, left=None, right=None, period=None)
參數(shù):
- x:類似數(shù)組,要插值點的橫坐標
- xp:一維浮點數(shù)序列,如果未指定參數(shù)周期,則數(shù)據(jù)點的x坐標必須增加 . 否則,在用歸一化周期邊界之后對xp進行內(nèi)部排序,xp = xp % period。
- fp:一維浮點數(shù)或復數(shù)序列,數(shù)據(jù)點的y坐標,與xp的長度相同。
- left:可選擇參數(shù)。x <xp [0]的返回值,默認值為fp [0]。
- right:可選擇參數(shù)。x> xp [-1]的返回值,默認值為fp [-1]。
- period:設定橫坐標的周期,該選項打開時,則忽略left和right。
示例:
import numpy as np import matplotlib.pyplot as plt xp = [1, 2, 3] fp = [3, 2, 0] y = np.interp(2.5, xp, fp) #1.0 y = np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp) #array([3. , 3. , 2.5 , 0.56, 0. ]) UNDEF = -99.0 y = np.interp(3.14, xp, fp, right=UNDEF) #-99.0 #sine 函數(shù)插值 x = np.linspace(0, 2*np.pi, 10) y = np.sin(x) xvals = np.linspace(0, 2*np.pi, 50) yinterp = np.interp(xvals, x, y) plt.plot(x, y, 'o') plt.plot(xvals, yinterp, '-x') plt.show() #周期 x 坐標的插值 x = [-180, -170, -185, 185, -10, -5, 0, 365] xp = [190, -190, 350, -350] fp = [5, 10, 3, 4] y = np.interp(x, xp, fp, period=360) #array([7.5 , 5. , 8.75, 6.25, 3. , 3.25, 3.5 , 3.75]) #復數(shù)插值Complex interpolation: x = [1.5, 4.0] xp = [2,3,5] fp = [1.0j, 0, 2+3j] y = np.interp(x, xp, fp) #array([0.+1.j , 1.+1.5j])
示例:已知y坐標,求x點。
import numpy as np y = np.array([0, 38.39, 71.41, 99.66, 123.67, 143.88, 160.61, 174.03, 184.16, 190.8, 193.52]) x = np.array([0, 0.37, 0.74, 1.11, 1.48, 1.85, 2.22, 2.59, 2.96, 3.33, 3.7]) plt.plot(x, y, '-') y_val = 30 root = np.interp(y_val, y, x) print(root)
scipy.interpolate.interp1d
scipy.interpolate.interp1d(x, y, kind='linear', axis=- 1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)
參數(shù):
- x:數(shù)值數(shù)組。一般是升序排列的x數(shù)據(jù)點。
- y:數(shù)值數(shù)組。與x數(shù)據(jù)點對應的y坐標,插值維的長度必須與x長度相同。
- kind:字符串或整數(shù),給出插值的樣條曲線的階數(shù),線性插值用’linear’。‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’. ‘zero’, ‘slinear’, ‘quadratic’ ,‘cubic’。
- axis:int
- copy:bool
- bounds_error:布爾值,越界是否報錯,除非fill_value=‘extrapolate’,否則默認越界時報錯。
- fill_value:數(shù)組或’extrapolate’,指定不在x范圍內(nèi)時的填充值或填充方法. 當為’extrapolate’時,返回的函數(shù)會對落在x范圍外的值進行外插。
- assume_sorted:bool
示例:
x = data['時間'] y = data['濃度'] # 構建完整的時間序列 = [1,23,...23] xnew = np.linspace(1,23,num=23) # 線性插值 f1 = interp1d(x,y,kind='linear') ynew1 = f1(xnew) plt.scatter(x,y,zorder=3) plt.plot(xnew,ynew1,marker='s',ls='--',c='C1') plt.legend(['data','線性插值']) plt.xticks(range(0,24,1)) plt.grid(ls='--',alpha=0.5) plt.xlabel('A') plt.ylabel('B') plt.tight_layout() plt.show()
示例:
from scipy.interpolate import interp1d x = [1, 2, 3] y = [3, 2, 0] f = interp1d(x,y,fill_value=(3,0),bounds_error=False) # 線性內(nèi)插 out = f([0, 1, 1.5, 2.72, 3.14]) print(out) #array([3. , 3. , 2.5 , 0.56, 0. ]) fe = interp1d(x,y, fill_value='extrapolate') # 線性內(nèi)插+外插 out = fe([0, 1, 1.5, 2.72, 3.14]) print(out) #array([ 4. , 3. , 2.5 , 0.56, -0.28])
到此這篇關于python實現(xiàn)線性插值的文章就介紹到這了,更多相關python線性插值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
以911新聞為例演示Python實現(xiàn)數(shù)據(jù)可視化的教程
這篇文章主要介紹了以911新聞為例演示Python實現(xiàn)數(shù)據(jù)可視化的教程,主要基于NMF主題模型,需要的朋友可以參考下2015-04-04python在前端頁面使用?MySQLdb?連接數(shù)據(jù)
這篇文章主要介紹了MySQLdb?連接數(shù)據(jù)的使用,文章主要介紹的相關內(nèi)容又插入數(shù)據(jù),刪除數(shù)據(jù),更新數(shù)據(jù),搜索數(shù)據(jù),需要的小伙伴可以參考一下2022-03-03簡介Python的collections模塊中defaultdict類型的用法
這里我們來簡介Python的collections模塊中defaultdict類型的用法,與內(nèi)置的字典類最大的不同在于初始化上,一起來看一下:2016-07-07Python 中urls.py:URL dispatcher(路由配置文件)詳解
這篇文章主要介紹了Python 中urls.py:URL dispatcher(路由配置文件)詳解的相關資料,需要的朋友可以參考下2017-03-03