SciPy Interpolation
scipy.interpolate's interp1d builds a continuous function from a handful of known data points so you can estimate values that fall in between them.
What Interpolation Solves
Suppose you record a measurement only at certain moments - temperature once an hour, a sensor reading once a minute - but later need an estimate for a moment in between. Interpolation builds a function that passes through your known data points and lets you evaluate it anywhere within that range. scipy.interpolate.interp1d takes your known x and y arrays and returns a callable function you can use just like any other Python function.
Example
import numpy as np
from scipy.interpolate import interp1d
hours = np.array([0, 1, 2, 3, 4, 5, 6])
temps = np.array([15.0, 15.5, 17.0, 20.0, 22.0, 21.0, 18.0])
linear_interp = interp1d(hours, temps, kind="linear")
# Estimate the temperature at 2.5 hours and 4.25 hours
print(linear_interp([2.5, 4.25]))Choosing an Interpolation Kind
The kind argument controls how interp1d fills the gaps between your known points. 'linear' (the default) simply draws straight lines between neighboring points - fast and predictable, but it can look a little angular. 'quadratic' and 'cubic' fit smooth curves through the data using second- and third-degree polynomials respectively, which usually look more natural for continuously changing quantities like temperature, but can occasionally overshoot between points that change direction sharply. 'nearest', 'previous', and 'next' don't smooth anything at all - they just repeat whichever known value is closest, useful for modeling values that only change at discrete moments, like a thermostat setting.
- linear - straight lines between neighboring points (the default)
- nearest - repeats the value of whichever known point is closest
- previous / next - steps to the value at the previous or next known point, useful for sample-and-hold signals
- quadratic - a smooth curve built from second-degree polynomials
- cubic - a smoother curve built from third-degree polynomials
Example
cubic_interp = interp1d(hours, temps, kind="cubic")
print("linear estimate:", linear_interp([2.5, 4.25]))
print("cubic estimate: ", cubic_interp([2.5, 4.25]))Handling Values Outside the Known Range
By default, interp1d only knows how to answer questions about x values that fall between the smallest and largest x values you gave it - ask it for something outside that range and it raises a ValueError, since it has no real basis for the answer. If you do need an estimate beyond the recorded range, pass bounds_error=False together with fill_value="extrapolate", which tells interp1d to continue the trend of the nearest segment instead of raising an error.
Example
safe_interp = interp1d(
hours, temps, kind="linear",
bounds_error=False, fill_value="extrapolate"
)
print(safe_interp(8)) # 2 hours past the last recorded reading
print(safe_interp(-1)) # 1 hour before the first recorded readingExercise: SciPy Interpolation
What problem does interpolation solve?