Python 官方文档:入门教程 => 点击学习
目录线性插值python实现线性插值numpy.interpscipy.interpolate.interp1d线性插值 插值:是根据已知的数据序列(可以理解为你坐标中一系列离散的点
插值:是根据已知的数据序列(可以理解为你坐标中一系列离散的点),找到其中的规律,然后根据找到的这个规律,来对其中尚未有数据记录的点进行数值估计。
线性插值:是针对一维数据的插值方法。它根据一维数据序列中需要插值的点的左右临近两个数据来进行数值估计。当然了它不是求这两个点数据大小的平均值(在中心点的时候就等于平均值)。而是根据到这两个点的距离来分配比重的。
numpy.interp(x, xp, fp, left=None, right=None, period=None)
参数:
示例:
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 函数插值
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])
#复数插值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(x, y, kind='linear', axis=- 1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)
参数:
示例:
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) # 线性内插
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') # 线性内插+外插
out = fe([0, 1, 1.5, 2.72, 3.14])
print(out)
#array([ 4. , 3. , 2.5 , 0.56, -0.28])
到此这篇关于python实现线性插值的文章就介绍到这了,更多相关python线性插值内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: python实现线性插值的示例
本文链接: https://www.lsjlt.com/news/174398.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0