Python三维插值/多维插值 interpn/griddata
scipy.interpolate包含了python中常用的插值函数,包含一维到多维,本文介绍两种三维插值函数interpn和griddata两个函数的区别在于原始数据的类型,或者说完整程度。如果原始数据在每个维度的每个水平上均有函数值,即网格类数据,可采用interpn函数。若原始数据是一系列散点,并无严格的规律可言,则只能采用griddata函数。interpn函数首先给出该函数的帮助文档:H
·
scipy.interpolate包含了python中常用的插值函数,包含一维到多维,本文介绍两种三维插值函数interpn和griddata
两个函数的区别在于原始数据的类型,或者说完整程度。如果原始数据在每个维度的每个水平上均有函数值,即网格类数据,可采用interpn函数。若原始数据是一系列散点,并无严格的规律可言,则只能采用griddata函数。
interpn函数
首先给出该函数的帮助文档:
Help on function interpn in module scipy.interpolate.interpolate:
interpn(points, values, xi, method='linear', bounds_error=True, fill_value=nan)
Multidimensional interpolation on regular grids.
Parameters
----------
points : tuple of ndarray of float, with shapes (m1, ), ..., (mn, )
The points defining the regular grid in n dimensions.
n个行向量组成的数据,n代表维度,行向量里面是该维度下的不同水平(变量取值)
values : array_like, shape (m1, ..., mn, ...)
The data on the regular grid in n dimensions.
以上网格对应的函数值,n维矩阵
xi : ndarray of shape (..., ndim)
The coordinates to sample the gridded data at
插值点处的各维度值
method : str, optional
The method of interpolation to perform. Supported are "linear" and
"nearest", and "splinef2d". "splinef2d" is only supported for
2-dimensional data.
bounds_error : bool, optional
If True, when interpolated values are requested outside of the
domain of the input data, a ValueError is raised.
If False, then `fill_value` is used.
fill_value : number, optional
If provided, the value to use for points outside of the
interpolation domain. If None, values outside
the domain are extrapolated. Extrapolation is not supported by method
"splinef2d".
Returns
-------
values_x : ndarray, shape xi.shape[:-1] + values.shape[ndim:]
Interpolated values at input coordinates.
插值点处的函数值
示例
import numpy as np
from scipy.interpolate import interpn
arr = np.random.random((3,3,3))
x1 = np.array([0, 1, 2])
x2 = np.array([0, 10, 20])
x3 = np.array([0, 10, 20])
points = (x1, x2, x3)
xi = (0.1, 9, 19)
result = interpn(points, arr, xi)
griddata函数
同样给出该函数的帮助文档
Help on function griddata in module scipy.interpolate.ndgriddata:
griddata(points, values, xi, method='linear', fill_value=nan, rescale=False)
Interpolate unstructured D-dimensional data.
Parameters
----------
points : ndarray of floats, shape (n, D)
Data point coordinates. Can either be an array of
shape (n, D), or a tuple of `ndim` arrays.
散点图自变量值(矩阵)
values : ndarray of float or complex, shape (n,)
Data values.
散点图对应函数值(可以有多个函数值)
xi : 2-D ndarray of float or tuple of 1-D array, shape (M, D)
Points at which to interpolate data.
插值点自变量取值
method : {'linear', 'nearest', 'cubic'}, optional
Method of interpolation. One of
``nearest``
return the value at the data point closest to
the point of interpolation. See `NearestNDInterpolator` for
more details.
``linear``
tesselate the input point set to n-dimensional
simplices, and interpolate linearly on each simplex. See
`LinearNDInterpolator` for more details.
``cubic`` (1-D)
return the value determined from a cubic
spline.
``cubic`` (2-D)
return the value determined from a
piecewise cubic, continuously differentiable (C1), and
approximately curvature-minimizing polynomial surface. See
`CloughTocher2DInterpolator` for more details.
fill_value : float, optional
Value used to fill in for requested points outside of the
convex hull of the input points. If not provided, then the
default is ``nan``. This option has no effect for the
'nearest' method.
rescale : bool, optional
Rescale points to unit cube before performing interpolation.
This is useful if some of the input dimensions have
incommensurable units and differ by many orders of magnitude.
示例
import numpy as np
from scipy.interpolate import griddata
def func(x, y, z):
return x*(1-x)*np.cos(4*np.pi*x) * (np.sin(4*np.pi*y**2)**2)*z
points = np.random.rand(10, 3)#实际点坐标
values = func(points[:,0], points[:,1],points[:,2])#实际点的值
xi = np.array([2, 3, 4])
grid_z0 = griddata(points, values, xi, method='nearest')#插值计算,计算出插值点的值
参考链接
更多推荐
已为社区贡献1条内容
所有评论(0)