假设您不需要“是”矩阵中指定的精确边值(请参阅我的注释),下面是我要做的:import numpy as np
from scipy.interpolate import griddata
nan = np.NaN
dat = np.array([[ 1, nan, nan, nan, 5,],
[ nan, nan, nan, nan, nan,],
[ nan, 6, nan, nan, 8, ],
[ nan, nan, 9, nan, nan,],
[ nan, nan, nan, nan, nan,],
[ 0, nan, nan, nan, 2, ]])
def fill_nans(indata, method='linear'):
"""
Fill NaN values in the input array `indata`.
"""
# Find the non-NaN indices
inds = np.nonzero(~np.isnan(indata))
# Create an `out_inds` array that contains all of the indices of indata.
out_inds = np.mgrid[[slice(s) for s in indata.shape]].reshape(indata.ndim, -1).T
# Perform the interpolation of the non-NaN values to all the indices in the array:
return griddata(inds, indata[inds], out_