如何在网格中对点列表进行二维插值 [英] How to do 2d interpolation in a grid for a list of points
问题描述
似乎这里的一些相关问题没有给我想要的答案.
It seems a few relevant questions here didn't give me the answer I want.
所以我有一个 2D 查找表,它是一个 2D 网格,其值位于网格坐标处.例如,可以通过以下代码生成这样的表:
So I have a 2D look up table which is a 2D grid with values at grid coordiates. For example, such table can be generated by code below:
xx, yy = np.meshgrid(np.arange(100), np.arange(100))
zz = some_function(xx, yy)
现在我有一个网格上散点的坐标列表,例如,我可以通过以下方式生成这样的坐标:
now I have a list of coordinates of scattered points on the grid, for example, I can generate such coordinates by:
xs = np.random.rand(10) * 100
ys = np.random.rand(10) * 100
scattered_points = [(x, y) for x, y in zip(xs, ys)]
如何为这 10 个点插入 z 值?我只发现我可以通过搜索 (x, y) 的相邻网格点并在那里执行插值来做到这一点.有几点很好,但我只是想知道是否有更好的/pythonic 方法使用 numpy
/scipy
来做到这一点.
How can I interpolate out the z value for these 10 points?
I only find out that I can do this by searching the neighboring grid points of (x, y) and perform interpolation there. It is fine for a few points but I just wonder if there is a better/pythonic way to do that using numpy
/scipy
.
推荐答案
这应该适用于您所描述的情况.(更新)
This should work for what you describe. (Updated)
from scipy.interpolate import interp2d
import numpy as np
xx, yy = np.meshgrid(np.arange(100), np.arange(100))
zz = some_function(xx,yy)
f = interp2d(xx, yy, zz, kind='cubic')
xs = np.random.rand(10) * 100
ys = np.random.rand(10) * 100
zs = np.zeros_like(xs)
for i in range(xs.shape[0]):
zs[i]=f(xs[i],ys[i])
这篇关于如何在网格中对点列表进行二维插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!