Interpolation in Python with SciPy: A Complete Guide

Last updated 3 weeks, 3 days ago | 112 views 75     5

Tags:- Python SciPy

Interpolation is a technique for estimating values between two known data points. It is widely used in scientific computing, engineering, and data analysis when you want to "fill in the gaps" in data.

In Python, SciPy offers powerful and flexible functions for interpolation through the scipy.interpolate module. This guide walks you through how to use SciPy for interpolation with detailed examples.


What Is Interpolation?

Interpolation allows you to estimate unknown values within the range of a discrete set of known data points.

For example:

  • You have the temperature recorded at 8 AM and 10 AM.

  • You want to estimate what it was at 9 AM.


Getting Started

To use interpolation in Python, you need:

pip install scipy numpy matplotlib

Import the necessary modules:

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

1. 1D Interpolation

Example: Linear Interpolation

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

f = interpolate.interp1d(x, y)

# Estimate value at x=2.5
print(f(2.5))  # Output: 5.0

Interpolation Types

You can specify the type using the kind parameter:

f_linear = interpolate.interp1d(x, y, kind='linear')     # default
f_cubic = interpolate.interp1d(x, y, kind='cubic')       # smooth curve
f_nearest = interpolate.interp1d(x, y, kind='nearest')   # stepwise

Plot Example

xnew = np.linspace(0, 4, 100)
ynew = f_cubic(xnew)

plt.plot(x, y, 'o', label='data points')
plt.plot(xnew, ynew, '-', label='cubic interpolation')
plt.legend()
plt.show()

2. 2D Interpolation

For 2D data grids, use interpolate.interp2d.

x = np.linspace(0, 5, 6)
y = np.linspace(0, 5, 6)
z = np.array([[i * j for i in x] for j in y])

f2d = interpolate.interp2d(x, y, z, kind='linear')

# Estimate value at (2.5, 3.5)
print(f2d(2.5, 3.5))

Note: interp2d is considered legacy — RegularGridInterpolator is recommended for new code.


3. Using interp1d with Missing Values

Use interpolation to fill missing values (e.g., in time series):

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

f = interpolate.interp1d(x, y)
print(f(3))  # Will return interpolated value between 2 and 4

Advanced: Spline Interpolation

Use B-splines for smoother interpolation.

from scipy.interpolate import UnivariateSpline

x = np.linspace(0, 10, 10)
y = np.sin(x)

spline = UnivariateSpline(x, y)
xnew = np.linspace(0, 10, 100)
ynew = spline(xnew)

plt.plot(x, y, 'o', label='data')
plt.plot(xnew, ynew, label='spline')
plt.legend()
plt.show()

✅ Tips for Interpolation

Tip Description
Use kind='cubic' For smooth, natural curves
Use fill_value="extrapolate" To allow extrapolation beyond input
Normalize your data Helps improve accuracy and avoids overflow
Always visualize Plotting helps verify the accuracy of the interpolation

⚠️ Common Pitfalls

Pitfall Solution
Extrapolation error Use fill_value="extrapolate" in interp1d
Irregular data spacing Spline interpolation is more suitable
High-order interpolation overshoot Stick with linear or cubic for stability
Using interp2d for new projects Prefer RegularGridInterpolator or griddata

Bonus: Interpolate Scattered Data with griddata

For scattered (non-grid) 2D data:

from scipy.interpolate import griddata

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

grid_x, grid_y = np.mgrid[0:1:100j, 0:1:100j]
grid_z = griddata(points, values, (grid_x, grid_y), method='cubic')

plt.imshow(grid_z.T, extent=(0,1,0,1), origin='lower')
plt.scatter(points[:, 0], points[:, 1], c=values, s=10, edgecolor='k')
plt.title("Scattered Data Interpolation")
plt.show()

Summary

Feature Function
1D Interpolation interp1d()
2D Interpolation interp2d() (legacy)
Scattered Data griddata()
Smooth Curves UnivariateSpline()
Multidimensional Grids RegularGridInterpolator()

SciPy’s interpolation tools are robust, flexible, and easy to use, making them ideal for a wide range of applications from scientific research to machine learning and engineering.