我有一个常规的2d x,y和z数组,我有一个x0和y0,我想在网格上知道点(x0,y0)中的z0值。
我发现Scipy具有插值模块,但据我了解,它插入了1D/2D数组并返回1D/2D数组,但是没有任何方法仅返回一个值。
。例如:
#My grid data
X = [ [X11, X12, X13, ..., X1N],
[X21, X22, X23, ..., X2N],
....
[XN1, XN2, XN3, ..., XNN]
Y = [ [Y11, Y12, Y13, ..., Y1N],
[Y21, Y22, Y23, ..., Y2N],
....
[YN1, YN2, YN3, ..., YNN] ]
Z = [ [Z11, Z12, Z13, ..., Z1N],
[Z21, Z22, Z23, ..., Z2N],
....
[ZN1, ZN2, ZN3, ..., ZNN] ]
#Point at which I want to know the value of the Z
X0, Y0 = ..., ...
#Now I want to call any function that'll return the value at point (X0, Y0), Z0 is float value, not array
Z0 = interpolation(X, Y, Z, X0, Y0)
我了解相似的函数是scipy.interpaly.interpn,但它仅适用于1D数组,并在我想使用2D数据
您也可以使用Griddata:
points = np.array( (X.flatten(), Y.flatten()) ).T values = Z.flatten() from scipy.interpolate import griddata Z0 = griddata( points, values, (X0,Y0) )
x0和y0可以是数组,甚至是网格。
- 您也可以选择方法= 的插值
- 也许您可以找到一种方法来乘坐Flatten(),但应该起作用。
(https://docs.scipy.org/doc/scipy/reference/tutorial/tutorial/interpalle.html)
scipy.Interpaly griddata在单点上工作与数组一样好。将点传递到功能,瞧。您有一个值。
import numpy as np
from scipy.interpolate import griddata
points = np.array([[1,1],[1,2],[2,2],[2,1]])
values = np.array([1,4,5,2])
xi=([1.2,1.5])
result=griddata(points, values, xi, method='linear')
print("Value of interpolated function at",xi,"=",*result)
我认为您要寻找的是:https://docs.scipy.org/doc/doc/scipy-0.14.0/reference/generated/generated/scipy.interpal-.interp2.html