scipy.interpolate.RegularGridInterpolator¶
-
class
scipy.interpolate.
RegularGridInterpolator
(points, values, method='linear', bounds_error=True, fill_value=nan)[source]¶ Interpolation on a regular grid in arbitrary dimensions
The data must be defined on a regular grid; the grid spacing however may be uneven. Linear and nearest-neighbour interpolation are supported. After setting up the interpolator object, the interpolation method (linear or nearest) may be chosen at each evaluation.
Parameters: - points : tuple of ndarray of float, with shapes (m1, ), …, (mn, )
The points defining the regular grid in n dimensions.
- values : array_like, shape (m1, …, mn, …)
The data on the regular grid in n dimensions.
- method : str, optional
The method of interpolation to perform. Supported are “linear” and “nearest”. This parameter will become the default for the object’s
__call__
method. Default is “linear”.- bounds_error : bool, optional
If True, when interpolated values are requested outside of the domain of the input data, 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 None, values outside the domain are extrapolated.
See also
NearestNDInterpolator
- Nearest neighbour interpolation on unstructured data in N dimensions
LinearNDInterpolator
- Piecewise linear interpolant on unstructured data in N dimensions
Notes
Contrary to LinearNDInterpolator and NearestNDInterpolator, this class avoids expensive triangulation of the input data by taking advantage of the regular grid structure.
If any of points have a dimension of size 1, linear interpolation will return an array of nan values. Nearest-neighbor interpolation will work as usual in this case.
New in version 0.14.
References
[1] Python package regulargrid by Johannes Buchner, see https://pypi.python.org/pypi/regulargrid/ [2] Wikipedia, “Trilinear interpolation”, https://en.wikipedia.org/wiki/Trilinear_interpolation [3] Weiser, Alan, and Sergio E. Zarantonello. “A note on piecewise linear and multilinear table interpolation in many dimensions.” MATH. COMPUT. 50.181 (1988): 189-196. https://www.ams.org/journals/mcom/1988-50-181/S0025-5718-1988-0917826-0/S0025-5718-1988-0917826-0.pdf Examples
Evaluate a simple example function on the points of a 3D grid:
>>> from scipy.interpolate import RegularGridInterpolator >>> def f(x, y, z): ... return 2 * x**3 + 3 * y**2 - z >>> x = np.linspace(1, 4, 11) >>> y = np.linspace(4, 7, 22) >>> z = np.linspace(7, 9, 33) >>> data = f(*np.meshgrid(x, y, z, indexing='ij', sparse=True))
data
is now a 3D array withdata[i,j,k] = f(x[i], y[j], z[k])
. Next, define an interpolating function from this data:>>> my_interpolating_function = RegularGridInterpolator((x, y, z), data)
Evaluate the interpolating function at the two points
(x,y,z) = (2.1, 6.2, 8.3)
and(3.3, 5.2, 7.1)
:>>> pts = np.array([[2.1, 6.2, 8.3], [3.3, 5.2, 7.1]]) >>> my_interpolating_function(pts) array([ 125.80469388, 146.30069388])
which is indeed a close approximation to
[f(2.1, 6.2, 8.3), f(3.3, 5.2, 7.1)]
.Methods
__call__
(xi[, method])Interpolation at coordinates