Exploring 1D Interpolation with SciPy: Linear, Nearest, Cubic & More
This article introduces the concept of interpolation for discrete data, demonstrates how to use SciPy's interp1d function with various methods (linear, nearest, nearest‑up, zero, quadratic, cubic), visualizes the resulting curves alongside the original points, and provides complete Python code for reproducing the plots.
Interpolation is an important method for approximating discrete functions, allowing estimation of function values at unsampled points based on known values.
Historically, Liu Zhuo in the 6th century used equally spaced quadratic interpolation for astronomical calculations; later Newton and Lagrange formulated general interpolation formulas for both equally and unequally spaced data. Today interpolation remains essential for data processing, function table creation, numerical integration, differentiation, root finding, and solving differential equations.
Given data:
<code>x = [1,2,3,4,5,6]
y = [300,500,800,1300,3000,5000]
</code>Plot the data points:
<code>import matplotlib.pyplot as plt # import pyplot
%matplotlib inline
x = [1,2,3,4,5,6]
y = [300,500,800,1300,3000,5000]
plt.scatter(x,y) # scatter plot
</code>To interpolate the x‑y relationship, SciPy's one‑dimensional interpolation function is called as:
<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', 'nearest', 'nearest-up', 'zero', 'quadratic', 'cubic', etc.
Define 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>Generate dense x values and compute interpolated y values:
<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>Plot and compare the interpolation results:
<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.