scipy.interpolate.

interpn#

scipy.interpolate.interpn(points, values, xi, method='linear', bounds_error=True, fill_value=nan)[source]#

在規則或等距網格上的多維插值。

嚴格來說,並非所有規則網格都受支援 - 此函數適用於等距網格,即具有均勻或不均勻間距的矩形網格。

參數:
pointstuple of ndarray of float, shape 為 (m1, ), …, (mn, )

定義 n 維度規則網格的點。每個維度中的點(即 points tuple 的每個元素)必須嚴格遞增或遞減。

valuesarray_like,shape 為 (m1, …, mn, …)

n 維度規則網格上的資料。接受複數資料。

Deprecated since version 1.13.0: 複數資料在使用 method="pchip" 時已被棄用,並將在 SciPy 1.15.0 中引發錯誤。這是因為 PchipInterpolator 僅適用於實數值。如果您嘗試使用傳遞陣列的實部,請在 values 上使用 np.real

xindarray,shape 為 (…, ndim)

要在網格化資料上採樣的座標

methodstr,optional

要執行的插值方法。支援 “linear”、“nearest”、“slinear”、“cubic”、“quintic”、“pchip” 和 “splinef2d”。“splinef2d” 僅支援二維資料。

bounds_errorbool,optional

如果為 True,當請求的插值值超出輸入資料的域時,將引發 ValueError。如果為 False,則使用 fill_value

fill_valuenumber,optional

如果提供,則用於網格域外點的值。如果為 None,則網格域外的點會被外插。方法 “splinef2d” 不支援外插。

返回:
values_xndarray,shape 為 xi.shape[:-1] + values.shape[ndim:]

xi 處的插值。 有關 xi.ndim == 1 時的行為,請參閱註釋。

另請參閱

NearestNDInterpolator

N 維度非結構化資料上的最近鄰插值

LinearNDInterpolator

N 維度非結構化資料上的分段線性插值

RegularGridInterpolator

任意維度規則或等距網格上的插值(interpn 包裝了這個類別)。

RectBivariateSpline

矩形網格上的雙變數 spline 近似

scipy.ndimage.map_coordinates

在間距相等的網格上進行插值(適用於例如 N 維影像重新取樣)

註釋

在 0.14 版本中新增。

如果 xi.ndim == 1,則會在傳回的陣列 values_x 的位置 0 插入一個新軸,因此其 shape 會改為 (1,) + values.shape[ndim:]

如果輸入資料的輸入維度具有不可比較的單位,並且相差多個數量級,則插值器可能會出現數值瑕疵。 考慮在插值之前重新調整資料的尺度。

範例

在規則 3-D 網格的點上評估一個簡單的範例函數

>>> import numpy as np
>>> from scipy.interpolate import interpn
>>> def value_func_3d(x, y, z):
...     return 2 * x + 3 * y - z
>>> x = np.linspace(0, 4, 5)
>>> y = np.linspace(0, 5, 6)
>>> z = np.linspace(0, 6, 7)
>>> points = (x, y, z)
>>> values = value_func_3d(*np.meshgrid(*points, indexing='ij'))

在某個點評估插值函數

>>> point = np.array([2.21, 3.12, 1.15])
>>> print(interpn(points, values, point))
[12.63]