import numpy as np
from scipy.interpolate import griddata # not quite the same as `matplotlib.mlab.griddata`
# a grid of data
grid = np.random.random((10, 10))
# a mask defining where the data is valid
mask = np.random.random((10, 10)) < 0.2
# locations and values of the valid data points
points = mask.nonzero()
values = grid[points]
gridx, gridy = np.mgrid[:grid.shape[0], :grid.shape[1]]
outgrid = griddata(points, values, (gridx, gridy), method='nearest') # or method='linear', method='cubic'
import numpy as np
from matplotlib.mlab import griddata
##Generate a random sparse grid
grid = np.random.random((6,6))*10
grid[grid>5] = np.nan
## Create Boolean array of missing values
mask = np.isfinite(grid)
## Get all of the finite values from the grid
values = grid[mask].flatten()
## Find indecies of finite values
index = np.where(mask==True)
x,y = index[0],index[1]
##Create regular grid of points
xi = np.arange(0,len(grid[0,:]),1)
yi = np.arange(0,len(grid[:,0]),1)
## Grid irregular points to regular grid using delaunay triangulation
ivals = griddata(x,y,values,xi,yi,interp='nn')
from scipy import ndimage as nd
indices = nd.distance_transform_edt(invalid_cell_mask, return_distances=False, return_indices=True)
data = data[tuple(ind)]
其中invalid_cell_mask是未定义的数组单元的布尔掩码,data是要填充的数组。 我在Filling gaps in a numpy array上发布了一个完整的例子。
3条答案
按热度按时间5ktev3wc1#
使用Scipy函数:
2mbi3lxu2#
这是我的尝试。
这就是我如何将不均匀分布的点插值到规则网格中。我没有尝试过任何其他类型的插值方法(即。线性)。
flmtquvp3#
您可以使用以下行非常简单地获得最近邻插值:
其中
invalid_cell_mask
是未定义的数组单元的布尔掩码,data
是要填充的数组。我在Filling gaps in a numpy array上发布了一个完整的例子。