基于python的不规则(x,y,z)网格的4D插值

2024-05-20 01:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些数据的形式是(x, y, z, V),其中x,y,z是距离,V是水分。我在StackOverflow上读了很多关于python插值的文章,比如thisthis有价值的文章,但它们都是关于x, y, z的规则网格。i、 每一个x的值与y的每一个点以及{}的每一个点的贡献相等。另一方面,我的观点来自三维有限元网格(如下所示),其中网格不是规则的。在

enter image description here

上面提到的两篇文章12,将x、y、z定义为一个单独的numpy数组,然后使用类似于cartcoord = zip(x, y)和{}(在一个3D示例中)。我不能这样做,因为我的三维网格是不规则的,因此不是每个点都对其他点有贡献,所以当我重复这些方法时,我发现了很多空值,我得到了很多错误。在

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

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

我怎样才能得到它?在


Tags: 数据网格距离定义规则文章贡献this
1条回答
网友
1楼 · 发布于 2024-05-20 01:33:02

我找到了答案,并发布给StackOverflow的读者。在

方法如下:

1-进口:

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

2-准备数据如下:

^{pr2}$

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

方法1,使用griddata

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

方法2,使用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(xi)
# OUTPUT: [0.20448536  0.20782028]
# I think you may use it with python map or pandas.apply as well

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

向您致意

相关问题 更多 >