You are making a wrong assumption wrt the structure of the grid requested by interpn
(您对interpn
请求的网格结构做出了错误的假设)
ValueError: The points in dimension 0 must be 1-dimensional
This message, a little bit obscure, references the elements pf the grid and say that they must be 1-dimensional, you must call interpn
not with (gx, gy)
as first argument but with (x, y)
.
(此消息有点晦涩,它引用了网格中的元素并说它们必须是一维的,您必须不使用(gx, gy)
作为第一个参数而是使用(x, y)
来调用interpn
。)
scipy.interpolate.interpn((x, y), v, sample_at)
But there is another wrong assumption in your code, because if you use the call above you'll get a different error
(但是您的代码中还有另一个错误的假设,因为如果您使用上面的调用,则会得到不同的错误)
ValueError: The requested sample points xi have dimension 10, but this RegularGridInterpolator has dimension 2
that hopefully has a clearer meaning: if your grid has two dimensions ( x and y , that is) all your points on which interpn
has to interpolate must be 2-dimensional as well... In other words, in your example the LAST dimension of the grid must be 2
(希望它具有更清晰的含义:如果网格具有二维(即x和y ),则interpn
必须在其上进行插值的所有点也都必须为2维...换句话说,在您的示例中LAST维的网格必须为2
)
Putting it all together (I'll use a location matrix sample_at
of 5 rows by 3 columns 0f 2-D points in this example) we have
(放在一起(在此示例中,我将使用5行乘3列0f 2维点的位置矩阵sample_at
),)
In [69]: import numpy as np
...: import scipy.interpolate
...: import matplotlib.pyplot as plt
...:
...: x = np.arange(10)
...: y = np.arange(10)
...: v = np.ones((10,10))
...: sample_at = 8*np.random.random((30)).reshape(5, 3, 2)
...:
...: scipy.interpolate.interpn((x, y), v, sample_at)
Out[69]:
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
PS: read the source code 1 , the comments in it are much better than the error messages...
(PS:读取源代码1 ,其中的注释比错误消息要好得多...)
(1) for me the source code is located at
((1)对我来说,源代码位于)
~/lib/miniconda3/lib/python3.7/site-packages/scipy/interpolate/interpolate.py