Professional Documents
Culture Documents
Interpolation (Scipy - Interpolate) - SciPy v1.9.3 Manual
Interpolation (Scipy - Interpolate) - SciPy v1.9.3 Manual
Interpolation (Scipy - Interpolate) - SciPy v1.9.3 Manual
3 Manual
Interpolation (scipy.interpolate)
Search the docs ...
Contents
Introduction
( scipy.linalg ) ) A class representing an interpolant (interp1d) in 1-D, offering several interpolation methods.
Sparse eigenvalue problems Convenience function griddata offering a simple interface to interpolation in N dimensions (N = 1, 2, 3, 4,
with ARPACK
…). Object-oriented interface for the underlying routines is also available.
Compressed Sparse Graph RegularGridInterpolator provides several interpolation methods on a regular grid in arbitrary (N)
Routines ( dimensions,
( scipy.sparse.csgraph ) Functions for 1- and 2-D (smoothed) cubic-spline interpolation, based on the FORTRAN library FITPACK.
They are both procedural and object-oriented interfaces for the FITPACK library.
Spatial data structures and Interpolation using radial basis functions.
algorithms (
( scipy.spatial ) )
(
Statistics ( scipy.stats ) ) 1-D interpolation (interp1d)
Multidimensional image
processing ( The interp1d class in scipy.interpolate is a convenient method to create a function based on fixed data
( scipy.ndimage ) ) points, which can be evaluated anywhere within the domain defined by the given data using linear interpolation.
(
File IO ( scipy.io ) ) 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:
https://docs.scipy.org/doc/scipy/tutorial/interpolate.html#multivariate-data-interpolation-griddata 1/12
11/23/22, 1:14 AM Interpolation (scipy.interpolate) — SciPy v1.9.3 Manual
Another set of interpolations in interp1d is nearest, previous, and next, where they return the nearest, previous,
or next point along the x-axis. Nearest and next can be thought of as a special case of a causal interpolating filter.
The following example demonstrates their use, using the same data as in the previous example:
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:
We can creat interpolator and interpolate test points using each method.
As expected, the higher degree spline interpolations are closest to the true values, though are more expensive to
compute than with linear or nearest. The slinear interpolation also matches the linear interpolation.
Spline interpolation
https://docs.scipy.org/doc/scipy/tutorial/interpolate.html#multivariate-data-interpolation-griddata 4/12
11/23/22, 1:14 AM Interpolation (scipy.interpolate) — SciPy v1.9.3 Manual
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 2-D 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 N-D 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 N-D space. The length of
each array is the number of curve points, and each array provides one component of the N-D 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
>>> plt.figure()
>>> plt.plot(x, y, 'x', xnew, ynew, xnew, np.sin(xnew), x, y, 'b')
>>> plt.legend(['Linear', 'Cubic Spline', 'True'])
>>> plt.axis([-0.05, 6.33, -1.05, 1.05])
>>> plt.title('Cubic-spline interpolation')
>>> plt.show()
Derivative of spline
Integral of spline
https://docs.scipy.org/doc/scipy/tutorial/interpolate.html#multivariate-data-interpolation-griddata 6/12
11/23/22, 1:14 AM Interpolation (scipy.interpolate) — SciPy v1.9.3 Manual
Roots of spline
>>> interpolate.sproot(tck)
array([3.1416]) # may vary
Notice that sproot may fail 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
example below for the subclass InterpolatedUnivariateSpline. The integral, derivatives, and
roots 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 for the 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
>>> plt.figure()
>>> plt.plot(x, y, 'x', xnew, ynew, xnew, np.sin(xnew), x, y, 'b')
>>> plt.legend(['Linear', 'InterpolatedUnivariateSpline', 'True'])
>>> plt.axis([-0.05, 6.33, -1.05, 1.05])
>>> plt.title('InterpolatedUnivariateSpline')
>>> plt.show()
>>> plt.figure()
>>> plt.plot(x, y, 'x', xnew, ynew, xnew, np.sin(xnew), x, y, 'b')
>>> plt.legend(['Linear', 'LSQUnivariateSpline', 'True'])
>>> plt.axis([-0.05, 6.33, -1.05, 1.05])
>>> plt.title('Spline with Specified Interior Knots')
>>> plt.show()
https://docs.scipy.org/doc/scipy/tutorial/interpolate.html#multivariate-data-interpolation-griddata 8/12
11/23/22, 1:14 AM Interpolation (scipy.interpolate) — SciPy v1.9.3 Manual
To evaluate the 2-D spline and its partial derivatives (up to the order of the spline), the function bisplev is
required. This function takes as the first two arguments two 1-D arrays whose cross-product specifies the
domain over which to evaluate the spline. The third argument is the tck list returned from bisplrep. If desired,
the fourth and fifth arguments provide the orders of the partial derivative in the and direction, respectively.
It is important to note that 2-D interpolation should not be used to find the spline representation of images. The
algorithm used is not amenable to large numbers of input points. The signal-processing toolbox contains more
appropriate algorithms for finding the spline representation of an image. The 2-D interpolation commands are
intended for use when interpolating a 2-D function as shown in the example that follows. This example uses the
mgrid command in NumPy which is useful for defining a “mesh-grid” in many dimensions. (See also the ogrid
command if the full-mesh is not needed). The number of output arguments and the number of dimensions of each
argument is determined by the number of indexing objects passed in mgrid.
>>> plt.figure()
>>> lims = dict(cmap='RdBu_r', vmin=-0.25, vmax=0.25)
>>> plt.pcolormesh(x_edges, y_edges, z, shading='flat', **lims)
>>> plt.colorbar()
>>> plt.title("Sparsely sampled function.")
>>> plt.show()
https://docs.scipy.org/doc/scipy/tutorial/interpolate.html#multivariate-data-interpolation-griddata 9/12
11/23/22, 1:14 AM Interpolation (scipy.interpolate) — SciPy v1.9.3 Manual
>>> plt.figure()
>>> plt.pcolormesh(xnew_edges, ynew_edges, znew, shading='flat', **lims)
>>> plt.colorbar()
>>> plt.title("Interpolated function.")
>>> plt.show()
1-D Example
This example compares the usage of the Rbf and UnivariateSpline classes from the scipy.interpolate
module.
>>> plt.subplot(2, 1, 1)
>>> plt.plot(x, y, 'bo')
>>> plt.plot(xi, yi, 'g')
>>> plt.plot(xi, np.sin(xi), 'r')
>>> plt.title('Interpolation using univariate spline')
>>> plt.subplot(2, 1, 2)
>>> plt.plot(x, y, 'bo')
>>> plt.plot(xi, fi, 'g')
>>> plt.plot(xi, np.sin(xi), 'r')
>>> plt.title('Interpolation using RBF - multiquadrics')
>>> plt.show()
2-D Example
This example shows how to interpolate scattered 2-D data:
https://docs.scipy.org/doc/scipy/tutorial/interpolate.html#multivariate-data-interpolation-griddata 11/12
11/23/22, 1:14 AM Interpolation (scipy.interpolate) — SciPy v1.9.3 Manual
Previous Next
Optimization (scipy.optimize) Fourier Transforms (scipy.fft)
https://docs.scipy.org/doc/scipy/tutorial/interpolate.html#multivariate-data-interpolation-griddata 12/12