【问题标题】:4D interpolation for irregular (x,y,z) grids by pythonpython对不规则(x,y,z)网格的4D插值
【发布时间】:2018-06-02 17:09:10
【问题描述】:

我有一些以(x, y, z, V) 形式出现的数据,其中 x,y,z 是距离,V 是湿度。我在 StackOverflow 上阅读了很多关于 Python 插值的内容,例如 thisthis 有价值的帖子,但所有这些都是关于 x, y, z 的常规网格。即x 的每个值与y 的每个点和z 的每个点的贡献相同。另一方面,我的观点来自 3D 有限元网格(如下所示),其中网格不规则。

提到的两个帖子12 将x、y、z 中的每一个定义为一个单独的numpy 数组,然后他们使用cartcoord = zip(x, y)scipy.interpolate.LinearNDInterpolator(cartcoord, z) 之类的东西(在3D 示例中)。我不能这样做,因为我的 3D 网格不规则,因此不是每个点都对其他点有贡献,所以如果我重复这些方法时,我会发现很多空值,并且出现很多错误。

这里是[x, y, z, V]形式的10个样本点

data = [[27.827, 18.530, -30.417, 0.205] , [24.002, 17.759, -24.782, 0.197] , 
[22.145, 13.687, -33.282, 0.204] , [17.627, 18.224, -25.197, 0.197] , 
[29.018, 18.841, -38.761, 0.212] , [24.834, 20.538, -33.012, 0.208] , 
[26.232, 22.327, -27.735, 0.204] , [23.017, 23.037, -29.230, 0.205] , 
[28.761, 21.565, -31.586, 0.211] , [26.263, 23.686, -32.766, 0.215]]

我想得到点(25, 20, -30)的插值V

我怎样才能得到它?

【问题讨论】:

    标签: python numpy 3d scipy interpolation


    【解决方案1】:

    我找到了答案,并将其发布以供 StackOverflow 读者阅读。

    方法如下:

    1- 进口:

    import numpy as np
    from scipy.interpolate import griddata
    from scipy.interpolate import LinearNDInterpolator
    

    2- 准备数据如下:

    # put the available x,y,z data as a numpy array
    points = np.array([
            [ 27.827,  18.53 , -30.417], [ 24.002,  17.759, -24.782],
            [ 22.145,  13.687, -33.282], [ 17.627,  18.224, -25.197],
            [ 29.018,  18.841, -38.761], [ 24.834,  20.538, -33.012],
            [ 26.232,  22.327, -27.735], [ 23.017,  23.037, -29.23 ],
            [ 28.761,  21.565, -31.586], [ 26.263,  23.686, -32.766]])
    # and put the moisture corresponding data values in a separate array:
    values = np.array([0.205,  0.197,  0.204,  0.197,  0.212,  
                       0.208,  0.204,  0.205, 0.211,  0.215])
    # Finally, put the desired point/points you want to interpolate over
    request = np.array([[25, 20, -30], [27, 20, -32]])
    

    3- 编写最后一行代码以获取插值

    方法一,使用griddata

    print griddata(points, values, request)
    # OUTPUT: array([ 0.20448536, 0.20782028])
    

    方法二,使用LinearNDInterpolator

    # First, define an interpolator function
    linInter= LinearNDInterpolator(points, values)
    
    # Then, apply the function to one or more points
    print linInter(np.array([[25, 20, -30]]))
    print linInter(request)
    # OUTPUT: [0.20448536  0.20782028]
    # I think you may use it with python map or pandas.apply as well
    

    希望这对每个人都有好处。

    打赌

    【讨论】:

    • 谢谢!请注意,带有参数method='linear'griddata 使用的是LinearNDInterpolator(“立方”仅适用于一维或二维网格)。所以你做了两次同样的事情。另请注意,您应该将最后一行中的xi 替换为request
    猜你喜欢
    • 2020-09-04
    • 2019-09-14
    • 1970-01-01
    • 2013-03-13
    • 2014-03-08
    • 2015-03-16
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多