用LinearNDInterpolator推断

用LinearNDInterpolator推断,第1张

用LinearNDInterpolator推断

我提出了一种方法,代码很糟糕,但希望它能对您有所帮助。这个想法是,如果您事先知道必须外推的范围,则可以使用线性外推值在数组的边缘添加额外的列/行,然后在新数组上进行内插。这是一个示例,其中一些数据将外推到x
= +-50和y = +-40:

import numpy as npx,y=np.meshgrid(np.linspace(0,6,7),np.linspace(0,8,9)) # create x,y gridz=x**2*y # and z values# create larger versions with two more columns/rowsxlarge=np.zeros((x.shape[0]+2,x.shape[1]+2))ylarge=np.zeros((x.shape[0]+2,x.shape[1]+2))zlarge=np.zeros((x.shape[0]+2,x.shape[1]+2))xlarge[1:-1,1:-1]=x # copy data on centreylarge[1:-1,1:-1]=yzlarge[1:-1,1:-1]=z# fill extra columns/rowsxmin,xmax=-50,50ymin,ymax=-40,40xlarge[:,0]=xmin;xlarge[:,-1]=xmax # fill first/last columnxlarge[0,:]=xlarge[1,:];xlarge[-1,:]=xlarge[-2,:] # copy first/last rowylarge[0,:]=ymin;ylarge[-1,:]=ymaxylarge[:,0]=ylarge[:,1];ylarge[:,-1]=ylarge[:,-2]# for speed gain: store factor of first/last column/rowfirst_column_factor=(xlarge[:,0]-xlarge[:,1])/(xlarge[:,1]-xlarge[:,2]) last_column_factor=(xlarge[:,-1]-xlarge[:,-2])/(xlarge[:,-2]-xlarge[:,-3])first_row_factor=(ylarge[0,:]-ylarge[1,:])/(ylarge[1,:]-ylarge[2,:])last_row_factor=(ylarge[-1,:]-ylarge[-2,:])/(ylarge[-2,:]-ylarge[-3,:])# extrapolate z; this operation only needs to be repeated when zlarge[1:-1,1:-1] is updatedzlarge[:,0]=zlarge[:,1]+first_column_factor*(zlarge[:,1]-zlarge[:,2]) # extrapolate first columnzlarge[:,-1]=zlarge[:,-2]+last_column_factor*(zlarge[:,-2]-zlarge[:,-3]) # extrapolate last columnzlarge[0,:]=zlarge[1,:]+first_row_factor*(zlarge[1,:]-zlarge[2,:]) # extrapolate first rowzlarge[-1,:]=zlarge[-2,:]+last_row_factor*(zlarge[-2,:]-zlarge[-3,:]) #extrapolate last row

然后您可以插值(xlarge,ylarge,zlarge)。由于所有 *** 作都是numpy
slices *** 作,因此希望对您而言足够快。z数据更新后,将其复制

zlarge[1:-1,1:-1]
并重新执行最后4行。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5650696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存