SciPy Tutorial menu

SciPy Interpolation

Interpolation estimates unknown values that lie between values you already know.

What is Interpolation?

Suppose you measured the temperature in Karachi at 6 am, 9 am, 12 pm and 3 pm. What was it at 10:30 am? You did not measure it, but you can estimate it from the nearby readings. That is interpolation.

TermMeaning
InterpolationEstimating a value between known points
ExtrapolationGuessing a value outside the known range (riskier)

Linear Interpolation With NumPy

For a quick straight-line estimate, NumPy's interp() is enough:

Example

python
import numpy as np

hours = [6, 9, 12, 15]
temps = [27, 31, 35, 34]
print(np.interp(10.5, hours, temps))
Output
33.0

At 10:30 (10.5 hours), halfway between 31 and 35 degrees, the estimate is 33.

Interpolation With SciPy

scipy.interpolate.interp1d builds a function from your points. You can then call it with any value in the range:

Example

python
import numpy as np
from scipy.interpolate import interp1d

hours = np.array([6, 9, 12, 15])
temps = np.array([27, 31, 35, 34])

f = interp1d(hours, temps)
print(f(10.5))
print(f([7, 8, 13]))
Output
33.0
[28.33333333 29.66666667 34.66666667]

interp1d still works, but the SciPy team calls it "legacy". For new code, use np.interp for straight lines and the spline classes below for smooth curves.

Smooth Curves With Cubic Splines

Straight lines between points make sharp corners. A cubic spline draws a smooth curve through every point, which is often more realistic for things like temperature:

Example

python
import numpy as np
from scipy.interpolate import CubicSpline

hours = np.array([6, 9, 12, 15])
temps = np.array([27, 31, 35, 34])

cs = CubicSpline(hours, temps)
print(np.round(cs([7, 10.5, 13]), 2))
Output
[28.02 33.31 35.47]

Compare with the straight-line answers above. The spline estimates that the temperature kept rising a little after 12 pm before falling, which a straight line cannot show.

Filling Gaps in a Data Series

A sensor missed some readings. Fill them in with interpolation:

Example

python
import numpy as np

days = np.arange(1, 8)
readings = np.array([20.0, 22.0, np.nan, 25.0, np.nan, np.nan, 31.0])

known = ~np.isnan(readings)
filled = readings.copy()
filled[~known] = np.interp(days[~known], days[known], readings[known])
print(filled)
Output
[20.  22.  23.5 25.  27.  29.  31. ]

The empty days now have values that fit smoothly between their neighbours.

Extrapolation Is Risky

By default, interp1d refuses values outside the range of your data:

Example

python
from scipy.interpolate import interp1d

f = interp1d([6, 9, 12, 15], [27, 31, 35, 34])
f(18)
Output
ValueError: A value (18.0) in x_new is above the interpolation range's maximum value (15).

This protects you. Guessing far outside the known data can give very wrong answers.

Exercise

Exercise

You know the price of petrol in January and March. Estimating the February price is an example of what?

Learn SciPy with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp