網(wǎng)站首頁 編程語言 正文
線性插值
插值:是根據(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
相關(guān)推薦
- 2022-06-25 Android開發(fā)壁紙的驗(yàn)證設(shè)置和確認(rèn)功能實(shí)現(xiàn)demo_Android
- 2022-07-13 數(shù)據(jù)結(jié)構(gòu)之冒泡排序
- 2022-04-19 idea如何解決jar包沖突
- 2022-05-15 Python語言實(shí)現(xiàn)二分法查找_python
- 2022-05-18 centos?自動運(yùn)行python腳本和配置?Python?定時任務(wù)_python
- 2022-08-04 Python?編程操作連載之字符串,列表,字典和集合處理_python
- 2023-01-11 解決?Redis?數(shù)據(jù)傾斜、熱點(diǎn)等問題_Redis
- 2023-01-19 利用Python構(gòu)建Flutter應(yīng)用的教程詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支