杰瑞科技汇

Python插值方法有哪些?

Of course! Interpolation is a fundamental technique in data analysis, scientific computing, and computer graphics. In Python, there are several excellent libraries to perform interpolation, each with its own strengths.

Python插值方法有哪些?-图1
(图片来源网络,侵删)

Let's break down what interpolation is, the main types, and how to implement them in Python using popular libraries.

What is Interpolation?

Interpolation is the process of estimating unknown values between two known data points. Imagine you have a set of discrete measurements (like temperature taken every hour). Interpolation allows you to estimate the temperature at, say, 25 minutes past the hour.

Analogy: If you know the altitude of a mountain at its base (0m) and its summit (1000m), interpolation can help you estimate the altitude at the 500m mark on the path.


Key Python Libraries for Interpolation

  1. SciPy: The gold standard for scientific computing in Python. Its scipy.interpolate module is powerful and comprehensive.
  2. NumPy: While not primarily an interpolation library, NumPy has simple, built-in functions like np.interp for 1D linear interpolation, which is very fast and easy to use.
  3. Pandas: The go-to library for data manipulation. It has convenient methods that leverage SciPy's functionality, making it easy to work with time-series data in DataFrames.

Univariate Interpolation (1D)

This involves estimating values for a single variable. For example, estimating y for a given x.

Python插值方法有哪些?-图2
(图片来源网络,侵删)

Example Data

Let's start with some sample data points that we want to interpolate.

import numpy as np
import matplotlib.pyplot as plt
# Known data points
x_known = np.array([0, 1, 2, 3, 4, 5])
y_known = np.array([0, 0.8, 0.9, 0.1, -0.8, -1.0])
# New points where we want to estimate the value
x_new = np.linspace(-1, 6, 200) # Create 200 points from -1 to 6

a) Linear Interpolation (np.interp)

This is the simplest method. It connects the known data points with straight lines.

  • When to use: For quick, simple estimates where you don't need smooth curves. It's guaranteed to pass through all your data points.
  • Pros: Fast, simple, no overfitting.
  • Cons: Not smooth; the "slope" changes abruptly at each data point.
# Using NumPy's built-in interp function
y_linear = np.interp(x_new, x_known, y_known)
# Plotting
plt.figure(figsize=(10, 6))
plt.plot(x_known, y_known, 'o', label='Known Data Points')
plt.plot(x_new, y_linear, '-', label='Linear Interpolation')'Linear Interpolation with NumPy')
plt.legend()
plt.grid(True)
plt.show()

b) Polynomial Interpolation (scipy.interpolate.interp1d)

This method fits a single polynomial of a specified degree through all the data points.

  • When to use: When you need a smooth curve that passes exactly through your points.
  • Caution: High-degree polynomials can lead to Runge's phenomenon, where they oscillate wildly between points, especially near the edges of the data range. A polynomial of degree n-1 will pass through n points, but it's often not what you want.
from scipy.interpolate import interp1d
# Create an interpolation function
# 'kind' can be 'linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic'
f_poly = interp1d(x_known, y_known, kind='quadratic')
# Use the function to get interpolated values
y_poly = f_poly(x_new)
# Plotting
plt.figure(figsize=(10, 6))
plt.plot(x_known, y_known, 'o', label='Known Data Points')
plt.plot(x_new, y_poly, '-', label='Quadratic Polynomial Interpolation')'Polynomial Interpolation with SciPy')
plt.legend()
plt.grid(True)
plt.show()

c) Spline Interpolation (scipy.interpolate.CubicSpline)

This is often the best choice for smooth, accurate interpolation. Instead of one single high-degree polynomial, it fits a low-degree polynomial (a "spline") to each segment between data points, ensuring they connect smoothly.

Python插值方法有哪些?-图3
(图片来源网络,侵删)
  • When to use: When you need a smooth curve that passes through your data points and you want to avoid the wild oscillations of high-degree polynomials. Cubic splines (degree 3) are the most common.
  • Pros: Very smooth, more stable than high-degree polynomials.
  • Cons: More complex than linear interpolation.
from scipy.interpolate import CubicSpline
# Create a cubic spline interpolation object
cs = CubicSpline(x_known, y_known)
# Use the object to get interpolated values
y_spline = cs(x_new)
# Plotting
plt.figure(figsize=(10, 6))
plt.plot(x_known, y_known, 'o', label='Known Data Points')
plt.plot(x_new, y_spline, '-', label='Cubic Spline Interpolation')'Spline Interpolation with SciPy')
plt.legend()
plt.grid(True)
plt.show()

Multivariate Interpolation (2D and higher)

This involves estimating values for a function of two or more variables (e.g., estimating altitude z for given coordinates x and y).

Example Data

We'll create a 2D grid of points.

# Create a 2D grid of known points
x = np.arange(-5.0, 5.0, 0.25)
y = np.arange(-5.0, 5.0, 0.25)
x_grid, y_grid = np.meshgrid(x, y)
# The known values (e.g., a surface z = f(x, y))
# Let's use a function like z = sin(sqrt(x^2 + y^2))
z_known = np.sin(np.sqrt(x_grid**2 + y_grid**2))
# New points for evaluation (we'll create a finer grid)
x_new = np.arange(-4.8, 4.8, 0.1)
y_new = np.arange(-4.8, 4.8, 0.1)
x_new_grid, y_new_grid = np.meshgrid(x_new, y_new)

a) Linear Interpolation (scipy.interpolate.RectBivariateSpline)

For 2D data, RectBivariateSpline is a great choice for a grid. It works like interp1d but in two dimensions.

from scipy.interpolate import RectBivariateSpline
# Create the interpolation object
# Note: RectBivariateSpline expects x and y to be 1D arrays
rbs = RectBivariateSpline(x, y, z_known)
# Evaluate on the new grid
z_linear_2d = rbs(x_new, y_new)
# Plotting
fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_subplot(121, projection='3d')
ax2 = fig.add_subplot(122, projection='3d')
# Original data
ax1.plot_surface(x_grid, y_grid, z_known, cmap='viridis')
ax1.set_title('Original Data')
# Interpolated data
ax2.plot_surface(x_new_grid, y_new_grid, z_linear_2d, cmap='viridis')
ax2.set_title('2D Linear Interpolation')
plt.show()

b) Griddata (scipy.interpolate.griddata)

This is a very flexible function for interpolating on an irregularly spaced 2D (or N-D) grid.

  • How it works: You provide scattered (x, y) points and their corresponding z values. griddata then estimates z on a regular grid that you define.
  • method parameter:
    • 'linear': (Default) TIN-based linear interpolation.
    • 'nearest': Nearest-neighbor interpolation.
    • 'cubic': 2D cubic interpolation.
from scipy.interpolate import griddata
# Flatten the original grid to create scattered data points
points = np.column_stack((x_grid.ravel(), y_grid.ravel()))
values = z_known.ravel()
# Interpolate onto the new grid
z_griddata = griddata(points, values, (x_new_grid, y_new_grid), method='cubic')
# Plotting
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x_new_grid, y_new_grid, z_griddata, cmap='viridis')
ax.set_title('2D Interpolation with griddata (Cubic)')
plt.show()

Summary: Which Method Should I Use?

Method Library Use Case Pros Cons
Linear NumPy Quick, simple 1D estimates. Very fast. Simple, fast, guaranteed to pass through points.
分享:
扫描分享到社交APP
上一篇
下一篇