正确使用scipy.interpolate.RegularGridInterpolator [英] Correct usage of scipy.interpolate.RegularGridInterpolator
问题描述
有关scipy的文档让我有些困惑.interpolate.RegularGridInterpolator .
例如,我有一个函数f:R ^ 3 => R,它在单位立方体的顶点上采样.我想进行插值以便在多维数据集中找到值.
Say for instance I have a function f: R^3 => R which is sampled on the vertices of the unit cube. I would like to interpolate so as to find values inside the cube.
import numpy as np
# Grid points / sample locations
X = np.array([[0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,0], [1,1,1.]])
# Function values at the grid points
F = np.random.rand(8)
现在,RegularGridInterpolator
使用一个points
参数和一个values
参数.
Now, RegularGridInterpolator
takes a points
argument, and a values
argument.
点:浮点数ndarray的元组,形状为(m1,),...,(mn,) 在n个维度上定义规则网格的点.
points : tuple of ndarray of float, with shapes (m1, ), ..., (mn, ) The points defining the regular grid in n dimensions.
值:类数组,形状(m1,...,mn,...) 规则网格中n个维度上的数据.
values : array_like, shape (m1, ..., mn, ...) The data on the regular grid in n dimensions.
我认为这可以打电话:
import scipy.interpolate as irp
rgi = irp.RegularGridInterpolator(X, F)
但是,当我这样做时,会出现以下错误:
However, when I do so, I get the following error:
ValueError:有8个点数组,但值具有1个维度
ValueError: There are 8 point arrays, but values has 1 dimensions
我在文档中误解了什么?
What am I misinterpreting in the docs?
推荐答案
好的,当我回答自己的问题时,我感到很傻,但是在原始regulargrid
lib的文档的帮助下,我发现了自己的错误:
Ok I feel silly when I answer my own question, but I found my mistake with help from the documentation of the original regulargrid
lib:
https://github.com/JohannesBuchner/regulargrid
points
应该是一个数组列表,用于指定沿每个轴的点间距.
points
should be a list of arrays that specifies how the points are spaced along each axis.
例如,要采用上述单位立方体,我应该设置:
For example, to take the unit cube as above, I should set:
pts = ( np.array([0,1.]), )*3
或者如果我有沿最后一个轴以更高的分辨率采样的数据,则可以设置:
or if I had data which was sampled at higher resolution along the last axis, I might set:
pts = ( np.array([0,1.]), np.array([0,1.]), np.array([0,0.5,1.]) )
最后,values
的形状必须与points
隐式布局的网格相对应.例如,
Finally, values
has to be of shape corresponding to the grid laid out implicitly by points
. For example,
val_size = map(lambda q: q.shape[0], pts)
vals = np.zeros( val_size )
# make an arbitrary function to test:
func = lambda pt: (pt**2).sum()
# collect func's values at grid pts
for i in range(pts[0].shape[0]):
for j in range(pts[1].shape[0]):
for k in range(pts[2].shape[0]):
vals[i,j,k] = func(np.array([pts[0][i], pts[1][j], pts[2][k]]))
最后,
rgi = irp.RegularGridInterpolator(points=pts, values=vals)
根据需要运行并执行.
这篇关于正确使用scipy.interpolate.RegularGridInterpolator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!