Two-dimensional interpolation in Python

This article shows how to do interpolation in Python and examines different 2d implementations. We will discuss useful functions for bivariate interpolation, such as scipy.interpolate.interp2d, numpy.meshgrid, and radial basis functions for smoothing/interpolation (RBF) used in Python.

We'll implement interpolation using the SciPy and Numpy libraries to make it easy.


Create 2D interpolations in Python using scipy.interpolate.interp2d

First, let's learn about interpolation, a technique for constructing data points between given data points. Suppose there are two points, say 1 and 2.

In this example, we can interpolate and find the points 1.22 and 1.44, and so on.

Interpolation is often used in machine learning to fill in missing data in a dataset, known as imputation. Interpolation is often used to make the points of a dataset more uniform.

Let's see scipy.interpolatean example of handling interpolation in Python using the module.

interp2d is a direct generalization of the interp1d function. This function takes the x and y coordinates of the available data points as separate 1D arrays and the value of each pair of x and y coordinates as a 2D array.

Data points are assumed to lie on a regular and uniform grid of x and y coordinates. The general function form is as follows.

class scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=None)

where x, y and z are arrays and kind can be {'linear', 'cubic', 'quintic'}or can be left optional.

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

# x,y arrays
x = np.arange(-5.01, 5.01, 0.25)
y = np.arange(-5.01, 5.01, 0.25)
xx, yy = np.meshgrid(x, y)  #see details below for 'np.meshgrid'

#approximate function which is z:= f(x,y)
z = np.sin(xx**2+yy**2)
fun = interpolate.interp2d(x, y, z, kind='linear') # kind could be {'linear', 'cubic', 'quintic'}
xnew = np.arange(-5.01, 5.01, 1e-2)
ynew = np.arange(-5.01, 5.01, 1e-2)
znew = fun(xnew, ynew)

plt.plot(x, z[0, :], 'go-', xnew, znew[0, :], 'b-')
plt.show()

output:

scipy interpolation interp2d

%> 请注意, we used numpy.meshgrid to make the grid; you can make a rectangular grid with two 1D arrays representing Cartesian or matrix indices.

The general function form is as follows.

numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')

xi represents a one-dimensional coordinate array x1,x2,...,xn.


Create radial basis functions for interpolation in Python using scipy.interpolate.Rbf

This type of interpolation is used in the case of n-dimensional scattered data; for this we use scipy.interpolate.Rbf.

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate.rbf import Rbf  # radial basis functions

# x y arrays
x = [1, 1, 2 ,3, 4, 4, 2, 6, 7]
y = [0, 2, 5, 6, 2, 4, 1, 5, 2]
z = [1]*len(x)

#RBF Func
rbf_fun = Rbf(x, y, z, function='gaussian')

x_new = np.linspace(0, 8, 81)
y_new = np.linspace(0, 8, 82)

x_grid, y_grid = np.meshgrid(x_new, y_new)
z_new = rbf_fun(x_grid.ravel(), y_grid.ravel()).reshape(x_grid.shape)

plt.pcolor(x_new, y_new, z_new);
plt.plot(x, y, 'o');
plt.xlabel('x'); plt.ylabel('y');
plt.title('RBF Gaussian interpolation');

output:

radial basis function

These interpolation functions convert ND scatter data to MD using radial basis functions (RBF). Scattered n-dimensional data can be smoothed and interpolated using RBF interpolation.

However, this should be done with caution due to the possibility of extrapolation, such as obtaining values ​​outside the range of the data.

Guess you like

Origin blog.csdn.net/fengqianlang/article/details/131505745