【问题标题】:Python interpolate point value on 2D gridPython在2D网格上插入点值
【发布时间】:2017-02-28 09:33:39
【问题描述】:

我有一个常规的 2D X、Y 和 Z 数组,我有一个点 X0 和 Y0,我想知道网格上点 (X0, Y0) 中的 Z0 值。

我发现 scipy 有 interpolate 模块,但据我了解,它会插入 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.interpolate.interpn,但它仅适用于 1D 数组,当我想使用 2D 数据时会出错

【问题讨论】:

    标签: python arrays numpy scipy interpolation


    【解决方案1】:
    • 你也可以使用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 可以是数组,甚至可以是网格。

    • 您也可以使用 method= 选择插值
    • 也许您可以找到一种方法来使用 flatten(),但它应该可以工作。

    (https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html)

    【讨论】:

    • 非常感谢您的回答。我也遇到了同样的问题,现在解决了。再次非常感谢
    【解决方案2】:
    【解决方案3】:

    scipy.interpolate 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2018-08-14
      • 2019-12-13
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多