Interpolation Methods

Estimate values between known data points.

SciPy Interpolation

 1from scipy import interpolate
 2import numpy as np
 3
 4x = np.array([0, 1, 2, 3, 4])
 5y = np.array([0, 1, 4, 9, 16])
 6
 7# Linear
 8f_linear = interpolate.interp1d(x, y, kind='linear')
 9
10# Cubic spline
11f_cubic = interpolate.interp1d(x, y, kind='cubic')
12
13# Evaluate
14x_new = np.linspace(0, 4, 100)
15y_new = f_cubic(x_new)

2D Interpolation

1# 2D regular grid (interp2d was removed in SciPy 1.14); z has shape (len(y), len(x))
2f = interpolate.RectBivariateSpline(x, y, z.T, kx=3, ky=3)
3z_new = f(x_new, y_new).T
4
5# 2D scattered data
6f = interpolate.Rbf(x, y, z)
7z_new = f(x_new, y_new)

Further Reading

Related Snippets