转载地址:https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html
Interpolation (scipy.interpolate)
Contents
There are several general interpolation facilities available in SciPy, for data in 1, 2, and higher dimensions:
- A class representing an interpolant (
interp1d) in 1-D, offering several interpolation methods. - Convenience function
griddataoffering a simple interface to interpolation in N dimensions (N = 1, 2, 3, 4, ...). Object-oriented interface for the underlying routines is also available. - Functions for 1- and 2-dimensional (smoothed) cubic-spline interpolation, based on the FORTRAN library FITPACK. There are both procedural and object-oriented interfaces for the FITPACK library.
- Interpolation using Radial Basis Functions.
1-D interpolation (interp1d)
The interp1d class
in scipy.interpolate is
a convenient method to create a function based on fixed data points which can be evaluated anywhere within the domain defined by the given data using linear interpolation. An instance of this class is created by passing the 1-d vectors comprising the data.
The instance of this class defines a __call__ method and can therefore by treated like a function which interpolates between known data values to obtain unknown values (it also has a docstring for help). Behavior at the boundary can be specified at instantiation
time. The following example demonstrates its use, for linear and cubic spline interpolation:
Multivariate data interpolation (griddata)
Suppose you have multidimensional data, for instance for an underlying function f(x, y) you only know the values at points (x[i], y[i]) that do not form a regular grid.
Suppose we want to interpolate the 2-D function
on a grid in [0, 1]x[0, 1]
but we only know its values at 1000 data points:
This can be done with griddata –
below we try out all of the interpolation methods:
One can see that the exact result is reproduced by all of the methods to some degree, but for this smooth function the piecewise cubic interpolant gives the best results:
Spline interpolation
Spline interpolation in 1-d: Procedural (interpolate.splXXX)
Spline interpolation requires two essential steps: (1) a spline representation of the curve is computed, and (2) the spline is evaluated at the desired points. In order to find the spline representation, there
are two different ways to represent a curve and obtain (smoothing) spline coefficients: directly and parametrically. The direct method finds the spline representation of a curve in a two- dimensional plane using the function splrep.
The first two arguments are the only ones required, and these provide the and components
of the curve. The normal output is a 3-tuple, ,
containing the knot-points, ,
the coefficients and
the order of
the spline. The default spline order is cubic, but this can be changed with the input keyword, k.
For curves in -dimensional
space the function splprep allows
defining the curve parametrically. For this function only 1 input argument is required. This input is a list of -arrays
representing the curve in -dimensional
space. The length of each array is the number of curve points, and each array provides one component of the -dimensional
data point. The parameter variable is given with the keyword argument, u, which defaults to an equally-spaced monotonic sequence between and .
The default output consists of two objects: a 3-tuple, ,
containing the spline representation and the parameter variable
The keyword argument, s , is used to specify the amount of smoothing to perform during the spline fit. The default value of is where is the number of data-points being fit. Therefore, if no smoothing is desired a value of should be passed to the routines.
Once the spline representation of the data has been determined, functions are available for evaluating the spline (splev)
and its derivatives (splev, spalde)
at any point and the integral of the spline between any two points ( splint).
In addition, for cubic splines ( )
with 8 or more knots, the roots of the spline can be estimated ( sproot).
These functions are demonstrated in the example that follows.
Cubic-spline
Derivative of spline
Integral of spline
Roots of spline
Notice that sproot failed
to find an obvious solution at the edge of the approximation interval, .
If we define the spline on a slightly larger interval, we recover both roots and :
Parametric spline
Spline interpolation in 1-d: Object-oriented (UnivariateSpline)
The spline-fitting capabilities described above are also available via an objected-oriented interface. The one dimensional splines are objects of the UnivariateSpline class,
and are created with the and components
of the curve provided as arguments to the constructor. The class defines __call__,
allowing the object to be called with the x-axis values at which the spline should be evaluated, returning the interpolated y-values. This is shown in the example below for the subclass InterpolatedUnivariateSpline.
The integral, derivatives,
androots methods
are also available on UnivariateSpline objects,
allowing definite integrals, derivatives, and roots to be computed for the spline.
The UnivariateSpline class can also be used to smooth data by providing a non-zero value of the smoothing parameter s,
with the same meaning as the s keyword
of the splrep function
described above. This results in a spline that has fewer knots than the number of data points, and hence is no longer strictly an interpolating spline, but rather a smoothing spline. If this is not desired, the InterpolatedUnivariateSpline class
is available. It is a subclass of UnivariateSpline that
always passes through all points (equivalent to forcing the smoothing parameter to 0). This class is demonstrated in the example below.
The LSQUnivariateSpline class
is the other subclass of UnivariateSpline.
It allows the user to specify the number and location of internal knots explicitly with the parameter t. This allows creation of customized splines with non-linear spacing, to interpolate in some
domains and smooth in others, or change the character of the spline.
InterpolatedUnivariateSpline
LSQUnivarateSpline with non-uniform knots