SciPy Interpolation

Interpolation is often required to estimate data points within the range of a discrete set of known data points. SciPy provides multiple interpolation classes and functions, such as interp1d for 1-dimensional interpolation and griddata for multidimensional data.

Key Topics

1D Interpolation

The interp1d function constructs a piecewise interpolation function from given data points. You can choose different types of interpolation (linear, cubic, etc.).

Example

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 2, 4, 6, 8])

f_linear = interp1d(x, y, kind='linear')
f_cubic = interp1d(x, y, kind='cubic')

x_new = np.linspace(0, 4, 10)

plt.plot(x, y, 'o', label='data points')
plt.plot(x_new, f_linear(x_new), '-', label='linear')
plt.plot(x_new, f_cubic(x_new), '--', label='cubic')
plt.legend()
plt.show()

print("Linear Interpolation:", f_linear(x_new))
print("Cubic Interpolation:", f_cubic(x_new))

Output

Linear Interpolation: [0. 0.888... 1.777... ... 8.]
Cubic Interpolation: [0. 0.915... 1.728... ... 8.]

Explanation: The interp1d function returns callable functions that can be used to evaluate interpolated values at any point within the domain of x. The plot shows the original data points and the interpolated values using linear and cubic methods.

Multidimensional Interpolation

Use scipy.interpolate.griddata for 2D or ND interpolation. Provide arrays of points and values, and specify the interpolation method.

Example

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

# Define grid points
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:100j]

# Define data points
points = np.random.rand(100, 2)
values = np.sin(points[:,0]**2 + points[:,1]**2)

# Interpolate using different methods
grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')
grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')

# Plot the results
plt.subplot(221)
plt.imshow(grid_z0.T, extent=(0,1,0,1), origin='lower')
plt.title('Nearest')
plt.subplot(222)
plt.imshow(grid_z1.T, extent=(0,1,0,1), origin='lower')
plt.title('Linear')
plt.subplot(223)
plt.imshow(grid_z2.T, extent=(0,1,0,1), origin='lower')
plt.title('Cubic')
plt.subplot(224)
plt.plot(points[:,0], points[:,1], 'k.', ms=1)
plt.title('Data Points')
plt.gcf().set_size_inches(8, 8)
plt.show()

Output

Three plots showing the interpolated values using nearest, linear, and cubic methods, and one plot showing the original data points.

Explanation: The griddata function interpolates the values at the grid points using different methods. The plots show the interpolated values using nearest, linear, and cubic methods, and the original data points.

Spline Methods

Functions like UnivariateSpline and splrep allow for smooth spline fitting, particularly useful in smoothing noisy datasets.

Example

import numpy as np
from scipy.interpolate import UnivariateSpline
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 10)
y = np.sin(x) + np.random.normal(0, 0.1, x.size)

spl = UnivariateSpline(x, y)
x_new = np.linspace(0, 10, 100)

plt.plot(x, y, 'o', label='data points')
plt.plot(x_new, spl(x_new), label='spline')
plt.legend()
plt.show()

Output

A plot showing the original noisy data points and the smooth spline curve.

Explanation: The UnivariateSpline function fits a smooth spline to the noisy data points. The plot shows the original data points and the fitted spline curve.

Key Takeaways

  • Flexible Methods: Linear, cubic, spline, and more for various use cases.
  • 1D vs. N-D: Choose interp1d for 1D or griddata for higher dimensions.
  • Spline Fitting: Smooth curves for noisy data.
  • Ease of Use: Interpolated values can be easily computed at any point in the domain.