osc*_*ury 3 python interpolation numpy scipy extrapolation
我有一个 3D 数据集,我想对其进行插值和线性外推。使用 可以轻松完成插值scipy.interpolate.LinearNDInterpolator。该模块只能为参数范围之外的值填充常量/nan,但我不明白为什么它不提供打开外推法的选项。
查看代码,我发现该模块是用 cython 编写的。由于没有 cython 经验,很难使用代码来实现外推。我可以用纯Python代码编写它,但也许这里的其他人有更好的主意?我的特殊情况涉及恒定的 xy 网格,但 z 值不断变化(-100,000),因此插值必须很快,因为每次 z 值变化时都会运行插值。
根据要求给出一个基本示例,假设我有一个像
xyPairs = [[-1.0, 0.0], [-1.0, 4.0],
[-0.5, 0.0], [-0.5, 4.0],
[-0.3, 0.0], [-0.3, 4.0],
[+0.0, 0.0], [+0.0, 4.0],
[+0.2, 0.0], [+0.2, 4.0]]
Run Code Online (Sandbox Code Playgroud)
假设我想计算x = -1.5, -0.8, +0.5和 处的值y = -0.2, +0.2, +0.5。目前,我正在对每个 y 值沿 x 轴执行一维插值/外推,然后对每个 x 值沿 y 轴执行一维插值/外推。外推由 中的第二个函数完成ryggyr's answer。
小智 6
使用最近插值和线性插值的组合。LinearNDInterpolator 如果插值失败则返回 np.nan,否则返回数组大小(1) NearestNDInterpolator 返回浮点数
import scipy.interpolate
import numpy
class LinearNDInterpolatorExt(object):
def __init__(self, points,values):
self.funcinterp=scipy.interpolate.LinearNDInterpolator(points,values)
self.funcnearest=scipy.interpolate.NearestNDInterpolator(points,values)
def __call__(self,*args):
t=self.funcinterp(*args)
if not numpy.isnan(t):
return t.item(0)
else:
return self.funcnearest(*args)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5054 次 |
| 最近记录: |