问题描述
我有一个 3d 点列表,我想在 3d 网格上插入这些点.
I have a list of points on in 3d that I would like to interpolate on a 3d grid.
coords = array([[ 8.33399963, 12.94800186, 15.22500038],
[ 8.57299995, 13.90000153, 14.14700031],...)
我有网格 x,y,z 坐标,它们与 numpy.meshgrid
一起用于创建网格:
I have the grid x,y,z coordinates, which together with numpy.meshgrid
are used to create the grid:
xi,yi,zi = np.meshgrid(bbox[:,0],bbox[:,1],bbox[:,2])
然后当我尝试执行插值时:
and then when I try to perform the interpolation:
griddata(coords,np.random.choice([.1,1,2],size=len(coords)),(xi,yi,zi),method='linear')
我得到了一个 nans 向量:
I get a vector of nans:
array([[[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan],
[ nan, nan, nan, nan, nan, nan, nan, nan]],....
我在这里做错了什么?
推荐答案
我不知道 xi,yi,zi
值是什么,但很可能它们在 coords 定义的域之外代码>.如果使用
meshgrid
生成网格,那么注意数组的顺序:
I do not know what xi,yi,zi
values are but most likely they are outside the domain defined by coords
. If you use meshgrid
to generate the grid, then pay attention to the order of the arrays:
在输入长度为 M
、N
和 P
的 3-D 情况下,输出的形状为 (N,M, P)
用于 'xy'
索引和 (M, N, P)
用于 'ij'
索引.>
试试这个:
In [61]: coords = 20 * np.random.random((200, 3)) - 1
In [62]: xi, yi, zi = np.meshgrid(np.arange(coords[:, 0].min()+2, coords[:,0].max()-2), np.arange(coords[:, 1].min()+2, coords[:,1
...: ].max()-2), np.arange(coords[:, 2].min()+2, coords[:,2].max()-2), indexing='ij')
In [63]: griddata(coords,np.random.choice([.1,1,2],size=len(coords)),(xi.astype(np.float), yi.astype(np.float), zi.astype(np.float
...: )),method='linear')
您仍然会得到一些 nan
值,其中点对函数的采样很差,但大多数值都已定义.
You will still get some nan
values where points poorly sample the function but most values are defined.
另一种可能性是您只看到第一个平面",其中可能主要包含 nan
.尝试 np.sum(np.isfinite(g))
以查看所有点中的点如何有效 np.prod(g.shape)
where g
是 griddata()
的输出.
Another possibility is that you are just seeing the first "plane" which may contain mostly nan
. Try np.sum(np.isfinite(g))
to see how may points are valid out of all points np.prod(g.shape)
where g
is thr output from griddata()
.
这篇关于scipy griddata 插值返回一个用 nan 填充的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!