Mastering 1D Interpolation in Python with SciPy: Linear, Nearest, Quadratic, Cubic
Learn how to perform one-dimensional interpolation in Python using SciPy's interp1d function, explore various interpolation methods such as linear, nearest, quadratic, and cubic, and visualize the results with Matplotlib to compare their fitting behaviors on sample data.
Interpolation is an important method for approximating discrete functions; it estimates function values at unsampled points using known values at a finite set of points.
Historically, as early as the 6th century Chinese scholar Liu Zhuo applied equally spaced quadratic interpolation to astronomical calculations. In the 17th century, Newton and Lagrange discussed general formulas for both equally and unequally spaced data. Today, interpolation remains a fundamental tool for data processing, function table creation, numerical integration, differentiation, root finding, and solving differential equations.
Given the data:
<code>x = [1, 2, 3, 4, 5, 6]
y = [300, 500, 800, 1300, 3000, 5000]
</code>Plotting the raw data points:
<code>import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(x, y)
</code>To interpolate the relationship between x and y , SciPy provides the one‑dimensional interpolation function interp1d :
<code>interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)
</code>Parameters:
x : independent variable data
y : dependent variable data
kind : type of interpolation; options include 'linear' – linear interpolation 'nearest' – nearest‑point interpolation 'nearest-up' – nearest‑point interpolation (upper) 'zero' – zero‑order interpolation 'quadratic' – quadratic spline interpolation 'cubic' – cubic spline interpolation
Creating interpolation functions for each method:
<code>from scipy.interpolate import interp1d
func_linear = interp1d(x, y, 'linear')
func_nearest = interp1d(x, y, 'nearest')
func_nearest_up = interp1d(x, y, 'nearest-up')
func_zero = interp1d(x, y, 'zero')
func_quadratic = interp1d(x, y, 'quadratic')
func_cubic = interp1d(x, y, 'cubic')
</code>Generating dense points and evaluating each interpolation:
<code>import numpy as np
x1 = np.linspace(1, 6, 100)
y_linear = func_linear(x1)
y_nearest = func_nearest(x1)
y_nearest_up = func_nearest_up(x1)
y_zero = func_zero(x1)
y_quadratic = func_quadratic(x1)
y_cubic = func_cubic(x1)
</code>Plotting the original data together with each interpolated curve for comparison:
<code>plt.figure(figsize=(20, 10))
plt.subplot(2, 3, 1)
plt.scatter(x, y, label='raw data')
plt.plot(x1, y_linear, label='linear', color='red')
plt.legend()
plt.subplot(2, 3, 2)
plt.scatter(x, y, label='raw data')
plt.plot(x1, y_nearest, label='nearest', color='red')
plt.legend()
plt.subplot(2, 3, 3)
plt.scatter(x, y, label='raw data')
plt.plot(x1, y_nearest_up, label='nearest_up', color='red')
plt.legend()
plt.subplot(2, 3, 4)
plt.scatter(x, y, label='raw data')
plt.plot(x1, y_zero, label='zero', color='red')
plt.legend()
plt.subplot(2, 3, 5)
plt.scatter(x, y, label='raw data')
plt.plot(x1, y_quadratic, label='quadratic', color='red')
plt.legend()
plt.subplot(2, 3, 6)
plt.scatter(x, y, label='raw data')
plt.plot(x1, y_cubic, label='cubic', color='red')
plt.legend()
plt.savefig('images/sci0202.png')
</code>Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.