MRE(有工作输出和不工作的输出,尽管我希望它工作,因为这将是直观的事情):
import numpy as np
from scipy.interpolate import RegularGridInterpolator, griddata
def f(x1, x2, x3):
return x1 + 2*x2 + 3*x3, x1**2, x2
# Define the input points
xi = [np.linspace(0, 1, 5), np.linspace(0, 1, 5), np.linspace(0, 1, 5)]
# Mesh grid
x1, x2, x3 = np.meshgrid(*xi, indexing='ij')
# Outputs
y = f(x1, x2, x3)
assert (y[0][1][1][3] == (0.25 + 2*0.25 + 3*0.75))
assert (y[1][1][1][3] == (0.25**2))
assert (y[2][1][1][3] == 0.25)
#### THIS WORKS BUT I CAN ONLY GET THE nth (with n integer in [1, d]) VALUE RETURNED BY f
# Interpolate at point 0.3, 0.3, 0.4
interp = RegularGridInterpolator(xi, y[0])
print(interp([0.3, 0.3, 0.4])) # outputs 2.1 as expected
#### THIS DOESN'T WORK (I WOULD'VE EXPECTED A LIST OF TUPLES FOR EXAMPLE)
# Interpolate at point 0.3, 0.3, 0.4
interp = RegularGridInterpolator(xi, y)
print(interp([0.3, 0.3, 0.4])) # doesn't output array([2.1, 0.1, 0.3])
有趣的是griddata确实支持以R^d
为单位输出值的函数
# Same with griddata
grid_for_griddata = np.array([x1.flatten(), x2.flatten(), x3.flatten()]).T
assert (grid_for_griddata.shape == (125, 3))
y_for_griddata = np.array([y[0].flatten(), y[1].flatten(), y[2].flatten()]).T
assert (y_for_griddata.shape == (125, 3))
griddata(grid_for_griddata, y_for_griddata, [0.3, 0.3, 0.4], method='linear')[0] # outputs array([2.1, 0.1, 0.3]) as expected
我使用RegularGridInterpolator
的方式是否错误?
我知道有些人可能会说“只要使用griddata
“,但是因为我的数据是在直线网格中,所以我应该使用RegularGridInterpolator
,这样会更快,对吗?
证明它更快:
1条答案
按热度按时间zte4gxcn1#
如果我定义一个
y
,最后一个维度是3:设置工作正常(没有关于3与5不匹配的投诉):
和插值:
对于多个点:
虽然以上只是一个猜测,但我看到这些文档可以这样解释:
最后的
...
表示数组可以有0个或更多的尾部维度(超过与点维度匹配的n
)。但是这种灵活性可能更多地应用于linear
和nearest
方法。其他方法似乎有问题。这在
__call__
的文档页面上更清楚:https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RegularGridInterpolator.call.html#scipy.interpolate.RegularGridInterpolator.call
interpn
也记录了这一点。