日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

python實(shí)現(xiàn)線性插值的示例_python

作者:初學(xué)小白Lu ? 更新時間: 2023-01-03 編程語言

線性插值

插值:是根據(jù)已知的數(shù)據(jù)序列(可以理解為你坐標(biāo)中一系列離散的點(diǎn)),找到其中的規(guī)律,然后根據(jù)找到的這個規(guī)律,來對其中尚未有數(shù)據(jù)記錄的點(diǎn)進(jìn)行數(shù)值估計。
線性插值:是針對一維數(shù)據(jù)的插值方法。它根據(jù)一維數(shù)據(jù)序列中需要插值的點(diǎn)的左右臨近兩個數(shù)據(jù)來進(jìn)行數(shù)值估計。當(dāng)然了它不是求這兩個點(diǎn)數(shù)據(jù)大小的平均值(在中心點(diǎn)的時候就等于平均值)。而是根據(jù)到這兩個點(diǎn)的距離來分配比重的。

python實(shí)現(xiàn)線性插值

numpy.interp

numpy.interp(x, xp, fp, left=None, right=None, period=None)

參數(shù):

  • x:類似數(shù)組,要插值點(diǎn)的橫坐標(biāo)
  • xp:一維浮點(diǎn)數(shù)序列,如果未指定參數(shù)周期,則數(shù)據(jù)點(diǎn)的x坐標(biāo)必須增加 . 否則,在用歸一化周期邊界之后對xp進(jìn)行內(nèi)部排序,xp = xp % period。
  • fp:一維浮點(diǎn)數(shù)或復(fù)數(shù)序列,數(shù)據(jù)點(diǎn)的y坐標(biāo),與xp的長度相同。
  • left:可選擇參數(shù)。x <xp [0]的返回值,默認(rèn)值為fp [0]。
  • right:可選擇參數(shù)。x> xp [-1]的返回值,默認(rèn)值為fp [-1]。
  • period:設(shè)定橫坐標(biāo)的周期,該選項(xiàng)打開時,則忽略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 坐標(biāo)的插值
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])

#復(fù)數(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坐標(biāo),求x點(diǎn)。

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ù)點(diǎn)。
  • y:數(shù)值數(shù)組。與x數(shù)據(jù)點(diǎn)對應(yīng)的y坐標(biāo),插值維的長度必須與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’,否則默認(rèn)越界時報錯。
  • fill_value:數(shù)組或’extrapolate’,指定不在x范圍內(nèi)時的填充值或填充方法. 當(dāng)為’extrapolate’時,返回的函數(shù)會對落在x范圍外的值進(jìn)行外插。
  • assume_sorted:bool

示例:

x = data['時間']
y = data['濃度']
# 構(gòu)建完整的時間序列 = [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])

原文鏈接:https://blog.csdn.net/weixin_43956958/article/details/128150723

欄目分類
最近更新