Interpolation means to fill in a function between known values. The data for interpolation are a set of points x and a set of function values y, and the result is a function f from some function class so that f(x) = y. Typically this function class is something simple, like Polynomials of bounded degree, piecewise constant functions, or splines.
Let’s look at a few examples
import numpy as np
from scipy.interpolate import BarycentricInterpolator, RegularGridInterpolator, griddata, interp1d
import matplotlib.pyplot as pltHere are a couple of functions we’ll look at:
fs = [
lambda x : (x - 3) * (x + 3) * x, # cubic
lambda x : np.exp(-x**2 / 2), # gaussian
lambda x : np.sin(3*x) / (3*x), # sinc function
lambda x : 1 / (np.exp(-2*x) + 1) # logistic
]x = np.linspace(-4,4,500)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, f in enumerate(fs):
y = f(x)
ax[i].plot(x, y)
plt.show()If we sample a couple of points on each function, we would see something like the following:
x = np.linspace(-4,4,500)
x6 = np.linspace(-4,4,6)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, f in enumerate(fs):
y = f(x)
# ax[i].plot(x, y)
y = f(x6)
ax[i].scatter(x6, y)
plt.show()An interpolation just uses the sampled points and function values to try to reconstruct the original function.
Scipy provides a high-level interface for doing this with scipy.interpolate.interp1d. You can pass in a variety of arguments, and the output is an interpolation object, which implements the __call__ method, so you can evaluate the interpolation.
from scipy.interpolate import interp1dx = np.linspace(-4,4,500)
xk = np.linspace(-4,4,6)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, f in enumerate(fs):
y = f(x)
ax[i].plot(x, y)
yk = f(xk)
ax[i].scatter(xk, yk)
interp = interp1d(xk, yk, kind='linear')
ax[i].plot(x, interp(x), '--')
plt.show()If we increase the number of points used for interpolation, then we can get better approximations to the function.
x = np.linspace(-4,4,500)
xk = np.linspace(-4,4,10)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, f in enumerate(fs):
y = f(x)
ax[i].plot(x, y)
yk = f(xk)
ax[i].scatter(xk, yk)
interp = interp1d(xk, yk, kind='linear')
ax[i].plot(x, interp(x), '--')
plt.show()There are a variety of options for the kind keyword.
kind='linear': piecewise linear interpolationkind='nearest': nearest-neighbor interpolation (piecewise constant)kind='quadratic': quadratic spline (piecewise quadratic)kind='cubic': cubic spline (piecewise cubic)
and more. See the documentation for details
kinds = ('linear', 'nearest', 'quadratic', 'cubic')
f = fs[2]
x = np.linspace(-4,4,500)
xk = np.linspace(-4,4,10)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, kind in enumerate(kinds):
y = f(x)
ax[i].plot(x, y)
yk = f(xk)
ax[i].scatter(xk, yk)
interp = interp1d(xk, yk, kind=kind)
ax[i].plot(x, interp(x), '--')
ax[i].set_title(kind)
plt.show()
Barycentric Interpolation¶
There are also several interpolation methods not covered by interp1d, or which offer more customization. These include:
BarycentricInterpolatorKroghInterpolatorPchipInterpolator
and so on. See the interpolation documentation for more.
Barycentric interpolation can be done using BarycentricInterpolator, which constructs a Lagrange Polynomial of minimum degree which interpolates the points.
See Berrut and Trefethen, 2004 for details.
Lagrange interpolation: given n data points , the Lagrange interpolating polynomial of degree at most is defined as:
You can verify that and for all , and that .
Barycentric interpolation is an improved way of Lagrange interpolation, given by
with weights
from scipy.interpolate import BarycentricInterpolatorx = np.linspace(-4,4,500)
xk = np.linspace(-4,4,10)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, f in enumerate(fs):
y = f(x)
ax[i].plot(x, y)
yk = f(xk)
ax[i].scatter(xk, yk)
interp = BarycentricInterpolator(xk, yk)
ax[i].plot(x, interp(x), '--')
plt.show()a big difference between a Barycentric interpolator and the sorts of interpolants you contruct using interp1d is that it is not defined piecewise - it is a degree k-1 polynomial which fits each of the k interpolation points exactly. If the function being interpolated is not a lower-degree polynomial, this can lead to large fluctuations away from the origin, which is called the Runge phenomenon.
You can minimize the effects of the Runge phenomenon by using Chebyshev interpolation points:
def chebyshev(k, scale=1):
"""
return k Chebyshev interpolation points in the range [-scale, scale]
"""
return scale*np.cos(np.arange(k) * np.pi / (k-1))xk = chebyshev(10, scale=4)
plt.plot(np.linspace(-4, 4, 10), np.ones(10), '.', label='Uniform')
plt.plot(xk, np.zeros_like(xk), '.', label = 'chebyshev')
plt.ylim(-1, 2)
plt.legend()
plt.show()x = np.linspace(-4,4,500)
xk = chebyshev(10, scale=4)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, f in enumerate(fs):
y = f(x)
ax[i].plot(x, y)
yk = f(xk)
ax[i].scatter(xk, yk)
interp = BarycentricInterpolator(xk, yk)
ax[i].plot(x, interp(x), '--')
plt.show()Higher-Dimensional Interpolation¶
Higher dimensional interpolations can be computed using either scipy.interpolate.griddata (for scattered data), or scipy.interpolate.RegularGridInterpolator (for data sampled on a regular grid). Both work in any number of dimensions. There are also a variety of more specific options which you can find in the documentation.
We consider the following function on :
f = lambda x, y : x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2We’ll want to interpolate onto a grid covering the unit square. The following two cells generate equivalent data:
gx, gy = np.mgrid[0:1:100j, 0:1:100j]gx, gy = np.meshgrid(np.linspace(0,1,100), np.linspace(0,1,100)) # x and y coordinates of a 100x100 gridWe’ll sample the function at some random points
from scipy.interpolate import griddata
n = 500
x = np.random.rand(n, 2)
z = f(x[:,0], x[:,1])
fig, ax = plt.subplots(2, 2, figsize=(10,10))
ax = ax.flatten()
# keyword arguments for imshow
imkw = {'extent': (0,1,0,1), 'origin': 'lower'}
ax[0].scatter(x[:,0], x[:,1], marker='x', color='k')
ax[0].imshow(f(gx, gy), **imkw)
ax[0].set_title('samples')
for i, method in enumerate(['nearest', 'linear', 'cubic']):
grid_interpolation = griddata(x, z, (gx, gy), method=method, fill_value=0)
ax[i+1].imshow(grid_interpolation, **imkw)
ax[i+1].set_title(method)
plt.show()RegularGridInterpolator is used when the samples lie on a regular grid. It takes the 1-dimensional arrays that define that grid, and can then be evaluated at arbitrary points - here, the points of a finer grid.
from scipy.interpolate import RegularGridInterpolator
xk = np.linspace(0,1,20)
yk = np.linspace(0,1,20)
gx, gy = np.meshgrid(xk, yk, indexing='ij')
z = f(gx, gy)
# points to evaluate the interpolant on
ex, ey = np.meshgrid(np.linspace(0,1,100), np.linspace(0,1,100), indexing='ij')
fig, ax = plt.subplots(2, 2, figsize=(10,10))
ax = ax.flatten()
# keyword arguments for imshow
imkw = {'extent': (0,1,0,1), 'origin': 'lower'}
ax[0].scatter(gx.flatten(), gy.flatten(), marker='x', color='k')
ax[0].imshow(z.T, **imkw)
ax[0].set_title('samples')
for i, method in enumerate(['linear', 'cubic', 'quintic']):
interp = RegularGridInterpolator((xk, yk), z, method=method, bounds_error=False, fill_value=0)
grid_interpolation = interp((ex, ey))
ax[i+1].imshow(grid_interpolation.T, **imkw)
ax[i+1].set_title(method)
plt.show()Extrapolate¶
After SciPy version 0.17.0, scipy.interpolate.interp1d allows extrapolation, by setting fill_value='extrapolate'. Please see documentations here for reference.
x = np.linspace(-8,8,500)
xk = np.linspace(-4,4,10)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, f in enumerate(fs):
y = f(x)
ax[i].plot(x, y)
yk = f(xk)
ax[i].scatter(xk, yk)
interp = interp1d(xk, yk, kind='linear', fill_value='extrapolate')
ax[i].plot(x, interp(x), '--')
plt.show()kinds = ('linear', 'nearest', 'quadratic', 'cubic')
f = fs[2]
x = np.linspace(-8,8,500)
xk = np.linspace(-4,4,10)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, kind in enumerate(kinds):
y = f(x)
ax[i].plot(x, y)
yk = f(xk)
ax[i].scatter(xk, yk)
interp = interp1d(xk, yk, kind=kind, fill_value='extrapolate')
ax[i].plot(x, interp(x), '--')
ax[i].set_title(kind)
plt.show()
Barycentric interpolation with BarycentricInterpolator natively supports extrapolation.
x = np.linspace(-6,6,500)
xk = chebyshev(10, scale=4)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
ax = ax.flatten()
for i, f in enumerate(fs):
y = f(x)
ax[i].plot(x, y)
yk = f(xk)
ax[i].scatter(xk, yk)
interp = BarycentricInterpolator(xk, yk)
ax[i].plot(x, interp(x), '--')
plt.show()