scipy-样条插值interpolate

[TOC]

python scipy样条插值函数:interpolate

interp1d 函数

class scipy.interpolate.interp1d(x, y,kind=’linear’, axis=- 1**, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)**[source]

样条插值

  • 样条插值法是一种以可变样条来作出一条经过一系列点的光滑曲线的数学方法。插值样条是由一些多项式组成的,每一个多项式都是由相邻的两个数据点决定的,这样,任意的两个相邻的多项式以及它们的导数(不包括仇阶导数)在连接点处都是连续的。 连接点的光滑与连续是样条插值和前边分段多项式插值的主要区别。
  • 在Scipy里可以用**scipy.interpolate模块下的interpld函数**实现样条插值。 SciPy的0.14.0版本里样条插值方式有:'linear','zero', 'slinear', 'quadratic'(2次), 'cubic'(3次), 4, 5等。
  • scipy多次样条插值的应用格式如下所示:
1
2
3
4
5
6
7
8
9
10
import matplotlib.pyplot as plt
from scipy import interpolate
x = np.arange(0, 10)
y = np.exp(-x/3.0)
f = interpolate.interp1d(x, y)

xnew = np.arange(0, 9, 0.1)
ynew = f(xnew) # use interpolation function returned by `interp1d`
plt.plot(x, y, 'o', xnew, ynew, '-')
plt.show()
1
2
3
4
5
6
7
8
9
10
11
import numpy as np, matplotlib.pyplot as plt
from scipy.interpolate import interpld #导入scipy里interpolate模块中的interpld插值模块
x= np.array([0, 1, 2, 3, 4, 5, 6, 7])
y= np.array([3, 4, 3.5, 2, 1, 1.5, 1.25, 0.9]) #离散点的分布
xx = np.linspace(x.min(), x.max(), 100) #新的插值区间及其点的个数
plt.scatter(x, y) #散点图
\#for n in ['linear','zero', 'slinear', 'quadratic', 'cubic', 4, 5]: #python scipy里面的各种插值函数
f = interp1d(x, y,kind="cubic") #编辑插值函数格式
ynew=f(xx) #通过相应的插值函数求得新的函数点
plt.plot(xx,ynew,"g") #输出新的函数点的图像
plt.show()

scipy.interpolate.interp2d

  • class scipy.interpolate.``interp2d(x, y, z, kind=’linear’, copy=True, bounds_error=False, fill_value=None)[source]

    Interpolate over a 2-D grid.

    x, y and z are arrays of values used to approximate some function f: z = f(x, y). This class returns a function whose call method uses spline interpolation to find the value of new points.If x and y represent a regular grid, consider using RectBivariateSpline.Note that calling interp2d with NaNs present in input values results in undefined behaviour.

    Parametersx, yarray_like

    • Arrays defining the data point coordinates.If the points lie on a regular grid, x can specify the column coordinates and y the row coordinates, for example:

      x = [0,1,2]; y = [0,3]; z = [[1,2,3], [4,5,6]]Otherwise, x and y must specify the full coordinates for each point, for example:

      x = [0,1,2,0,1,2];  y = [0,0,0,3,3,3]; z = [1,2,3,4,5,6] `If x and y are multidimensional, they are flattened before use.`
      
      • z array_like

        The values of the function to interpolate at the data points. If z is a multidimensional array, it is flattened before use. The length of a flattened z array is either len(x)len(y) if x and y specify the column and row coordinates or len(z) == len(x) == len(y) if x and y* specify coordinates for each point.

      • kind{‘linear’, ‘cubic’, ‘quintic’}, optional

        The kind of spline interpolation to use. Default is ‘linear’.

      • copybool, optionalIf True, the class makes internal copies of x, y and z.

             If False, references may be used. The default is to copy.
        
      • bounds_errorbool, optional

        If True, when interpolated values are requested outside of the domain of the input data (x,y), a ValueError is raised. If False, then fill_value is used.

      • fill_value number, optional

              If provided, the value to use for points outside of the interpolation domain. If omitted (None), values outside the domain are extrapolated via nearest-neighbor extrapolation.
        
1
2
3
4
5
6
7
8
9
10
11
12
13
from scipy import interpolate
x = np.arange(-5.01, 5.01, 0.25)
y = np.arange(-5.01, 5.01, 0.25)
xx, yy = np.meshgrid(x, y)
z = np.sin(xx**2+yy**2)
f = interpolate.interp2d(x, y, z, kind='cubic')

import matplotlib.pyplot as plt
xnew = np.arange(-5.01, 5.01, 1e-2)
ynew = np.arange(-5.01, 5.01, 1e-2)
znew = f(xnew, ynew)
plt.plot(x, z[0, :], 'ro-', xnew, znew[0, :], 'b-')
plt.show()