加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 正确使用scipy.interpolate.RegularGridInterpolator

发布时间:2020-12-20 12:32:14 所属栏目:Python 来源:网络整理
导读:我对 documentation for scipy.interpolate.RegularGridInterpolator有点困惑. 比方说我有一个函数f:R ^ 3 = R在单位立方体的顶点上采样.我想插值以便在立方体内找到值. import numpy as np# Grid points / sample locationsX = np.array([[0,0],[0,1],1,[1
我对 documentation for scipy.interpolate.RegularGridInterpolator有点困惑.

比方说我有一个函数f:R ^ 3 => R在单位立方体的顶点上采样.我想插值以便在立方体内找到值.

import numpy as np

# Grid points / sample locations
X = np.array([[0,0],[0,1],1,[1,1.]])

# Function values at the grid points
F = np.random.rand(8)

现在,RegularGridInterpolator接受一个点参数和一个values参数.

points : tuple of ndarray of float,with shapes (m1,),…,(mn,)
The points defining the regular grid in n dimensions.

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)

但是,当我这样做时,我收到以下错误:

ValueError: There are 8 point arrays,but values has 1 dimensions

我在文档中误解了什么?

解决方法

你的答案更好,你接受它是完全可以的.我只是将其添加为脚本的“替代”方式.

import numpy as np
import scipy.interpolate as spint

RGI = spint.RegularGridInterpolator

x = np.linspace(0,3) #  or  0.5*np.arange(3.) works too

# populate the 3D array of values (re-using x because lazy)
X,Y,Z = np.meshgrid(x,x,indexing='ij')
vals = np.sin(X) + np.cos(Y) + np.tan(Z)

# make the interpolator,(list of 1D axes,values at all points)
rgi = RGI(points=[x,x],values=vals)  # can also be [x]*3 or (x,)*3

tst = (0.47,0.49,0.53)

print rgi(tst)
print np.sin(tst[0]) + np.cos(tst[1]) + np.tan(tst[2])

收益:

1.93765972087
1.92113615659

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读