如何用Python实现插值?如何实现线性插值?

2021年9月12日19:51:26 发表评论 2,158 次浏览

Python里怎么进行线性插值?线性插值是在已知两个相邻点的值时确定任何中间点的函数值的技术。线性插值基本上是对落在两个已知值内的未知值的估计。线性插值用于各种学科,如统计、经济学、价格确定等。它用于填补统计数据中的空白,以保证信息的连续性。 

如何用Python实现插值?通过使用以下公式,我们可以对给定的数据点进行线性插值,后面会给出Python线性插值代码

线性插值:y(x) = y1 + (x - x1) \frac{(y2 - y1) }{ (x2 - x1)}

这里(x1, y1)是第一个数据点的坐标。并且(x2,y2 ) 是第二个数据点的坐标,其中x是我们执行插值的点,y是插值值。

如何实现线性插值?示例问题:

让我们举一个例子来更好地理解。我们有以下数据值,其中 x 表示数字,y 是 x 平方根的函数。我们的任务是找到5.5 (x) 的平方根。

X123456
y ( f(x) = √x ) 11.41421.732022.23602.4494

我们可以在这里使用线性插值方法。

1. 从 xie (5,2.2360) 和 (6,2.4494) 中找出两个相邻的 (x1, y1) ,(x2,y2) 。

其中 x1 = 5, x2= 6, y1 = 2.2360, y2 = 2.4494,我们在 x = 5.5 点进行插值。

2. 使用公式 y(x) = y1 + (x – x1) \frac{(y2 – y1) }{ (x2 – x1)}y(x) = y1 + (x - x1) \frac{(y2 - y1) }{ (x2 - x1)}

3. 将数值代入上述等式后。 

y = 2.2360+ (5.5-5) \ frac {(2.4494-2.2360)} {(6 - 5)}
y = 2.3427

在 x = 5.5 时,Y 的值为2.3427。因此,通过使用线性插值,我们可以轻松确定两个区间之间的函数值。如何用Python实现插值?下面结果两个实现方法。

方法一:使用公式 线性插值:y(x) = y1 + (x - x1) \frac{(y2 - y1) }{ (x2 - x1)}

示例:如何实现线性插值?假设我们有一个城市和年份的人口数据集。

X(年)20162017201820192021
Y(人口)100011234574851121245700

这里,X 是年份,Y 是任何城市的人口。我们的任务是在2020 年找到该城市的人口

我们选择我们的 (x1, y1) ,(x2,y2) 作为 x1=2019 , y1=12124, x2=2021, y2=5700, x = 2020, y = ?

这里 (x1, y1) 和 (x2, y2) 是两个相邻的点,x 是我们想要预测 y 总体值的年份。

# Python3 code
# Implementing Linear interpolation
# Creating Function to calculate the
# linear interpolation
  
def interpolation(d, x):
    output = d[0][1] + (x - d[0][0]) * ((d[1][1] - d[0][1])/(d[1][0] - d[0][0]))
  
    return output
  
# Driver Code
data=[[2019, 12124],[2021, 5700]]
  
year_x=2020
  
# Finding the interpolation
print("Population on year {} is".format(year_x),
             interpolation(data, year_x))

输出

Population on year 2020 is 8912.0

方法二:使用 scipy.interpolate.interp1d

Python里怎么进行线性插值?同样,我们可以使用名为interpolate.interp1d的 scipy 库函数来实现线性插值。

语法:scipy.interpolate.interp1d(x, y, kind='linear', axis=- 1, copy=True, bounds_error=None,fill_value=nan,assume_sorted=False)

编号          参数                        描述
1.X实数值的一维数组。
2.一个 ND 实数值数组。
3.种类即你想要的那种插值可以是“线性”、“最近”、“最近”、“零”、“线性”、“二次”、“三次”、“上一个”或“下一个”。'zero'、'slinear'、'quadratic' 和 'cubic',默认情况下它是linear
4.指定沿其进行插值的 y 轴。
5.复制它保存布尔值,如果为 True,则该类制作 x 和 y 的内部副本。
6.bounds_error它保存布尔值 如果为 True,则在尝试对 x 范围之外的值进行插值时会引发 ValueError。

如何实现线性插值?下面是一个示例:

例子:

假设我们有一个随机数据集:

X = [1,2,3,4,5], Y = [11,2.2,3.5,-88,1],我们想在点 2.5 处找到 Y 的值。

  • Python线性插值代码(Python 3)如下:
# Implementation of Linear Interpolation using Python3 code
# Importing library
from scipy.interpolate import interp1d
  
X = [1,2,3,4,5] # random x values
Y = [11,2.2,3.5,-88,1] # random y values
  
# test value
interpolate_x = 2.5
  
# Finding the interpolation
y_interp = interp1d(X, Y)
print("Value of Y at x = {} is".format(interpolate_x),
      y_interp(interpolate_x))

输出

Value of y at x = 2.5 is 2.85

如何用Python实现插值?以上就是Python线性插值代码和实现的全部内容,希望可以帮助到你,若有任何问题,请在下方评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: