Moh*_*esr 3 python 3d interpolation numpy scipy
我有一些数据,(x, y, z, V)其中x,y,z是距离,V是水分。我在StackOverflow上阅读了很多有关通过python进行插值的知识,例如这篇文章和这篇有价值的文章,但是它们都是关于的规则网格x, y, z。即的每个价值在的x每个点y和每个点上均等地贡献z。另一方面,我的观点来自3D有限元网格(如下所示),其中网格不是规则的。
上面提到的两个帖子1和2将x,y,z中的每一个定义为一个单独的numpy数组,然后使用了类似于cartcoord = zip(x, y)then scipy.interpolate.LinearNDInterpolator(cartcoord, z)(在3D示例中)的内容。我不能做同样的事情,因为我的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]]
Run Code Online (Sandbox Code Playgroud)
我想获得V该点的插值(25, 20, -30)
我怎么才能得到它?
我找到了答案,并将其发布给了StackOverflow读者。
方法如下:
1-进口:
import numpy as np
from scipy.interpolate import griddata
from scipy.interpolate import LinearNDInterpolator
Run Code Online (Sandbox Code Playgroud)
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]])
Run Code Online (Sandbox Code Playgroud)
3-编写代码的最后一行以获取插值
方法1,使用griddata
print griddata(points, values, request)
# OUTPUT: array([ 0.20448536, 0.20782028])
Run Code Online (Sandbox Code Playgroud)
方法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
Run Code Online (Sandbox Code Playgroud)
希望这对大家有益。
投注问候