Python 在scipy中插值np.nan值

Python 在scipy中插值np.nan值,python,numpy,scipy,Python,Numpy,Scipy,我想通过插入粗体显示的元素来填充np.nan数据值。 它们是与np.nan在其他维度中的相同位置相对应的元素 import numpy as np from scipy.interpolate import interp1d data = np.array([[[3, 2, 1, 3, 2], [**np.nan**, 1, 1, 4, 4], [4, 2, 3, 3, 4], [1, 1

我想通过插入粗体显示的元素来填充np.nan数据值。 它们是与np.nan在其他维度中的相同位置相对应的元素

import numpy as np
from scipy.interpolate import interp1d
data = np.array([[[3, 2, 1, 3, 2],
                  [**np.nan**, 1, 1, 4, 4],
                  [4, 2, 3, 3, 4],
                  [1, 1, 4, 1, 5],
                  [2, 4, 5, 2, 1]],

                 [[6, 7, 10, 6, 6],
                  [**5**, 9, 8, 6, 9],
                  [6, 10, 9, 8, 10],
                  [6, 8, 7, 10, 8],
                  [10, 9, 9, 10, 8]],

                 [[12, 14, 12, 15, 15],
                  [**21**, 11, 14, 14, 11],
                  [13, 13, 16, 15, 11],
                  [14, 15, 14, 16, 14],
                  [13, 15, 11, 11, 14]]])

result = interp1d(data, kind='cubic')
print result
结果

TypeError: __init__() takes at least 3 arguments (3 given)

最好的方法是什么?因为我必须处理非常大的数组,所以我正在寻找有效的方法。谢谢。

你的问题太复杂了,插值不需要来自5*5矩阵的信息,只需要其他维度相同单元格中的值?如果是这样的话,那么,它仍然是太板,因为有太多的插值工具,以适应不同的需要。我想说的是,最近邻法可能会给您一个良好的开端,尽管
scipy.interpolate
的文档对某些人来说有点弱:

In [1]:

data = np.array([[[3, 2, 1, 3, 2],
                  [np.nan, 1, 1, 4, 4],
                  [4, 2, 3, 3, 4],
                  [1, 1, 4, 1, 5],
                  [2, 4, 5, 2, 1]],

                 [[6, 7, 10, 6, 6],
                  [5, 9, 8, 6, 9],
                  [6, 10, 9, 8, 10],
                  [6, 8, 7, 10, 8],
                  [10, 9, 9, 10, 8]],

                 [[12, 14, 12, 15, 15],
                  [21, 11, 14, 14, 11],
                  [13, 13, 16, 15, 11],
                  [14, 15, 14, 16, 14],
                  [13, 15, 11, 11, 14]]])
In [2]:

data1=data.reshape((3,-1))
In [3]:
#the one you want to interpolate
data1[:,(np.isnan(data.reshape((3,-1))).any(0))]
Out[3]:
array([[ nan],
       [  5.],
       [ 21.]])
In [4]:
#the other 'good' data points
data1[:,~(np.isnan(data.reshape((3,-1))).any(0))]
Out[4]:
array([[  3.,   2.,   1.,   3.,   2.,   1.,   1.,   4.,   4.,   4.,   2.,
          3.,   3.,   4.,   1.,   1.,   4.,   1.,   5.,   2.,   4.,   5.,
          2.,   1.],
       [  6.,   7.,  10.,   6.,   6.,   9.,   8.,   6.,   9.,   6.,  10.,
          9.,   8.,  10.,   6.,   8.,   7.,  10.,   8.,  10.,   9.,   9.,
         10.,   8.],
       [ 12.,  14.,  12.,  15.,  15.,  11.,  14.,  14.,  11.,  13.,  13.,
         16.,  15.,  11.,  14.,  15.,  14.,  16.,  14.,  13.,  15.,  11.,
         11.,  14.]])
In [5]:

import scipy.interpolate as si
In [6]:

Q=si.NearestNDInterpolator(data1[:,~(np.isnan(data.reshape((3,-1))).any(0))][[1,2]].T, 
                           data1[:,~(np.isnan(data.reshape((3,-1))).any(0))][0])
In [8]:
#the first value is the answer, the 2nd is the index of the nearest neighbor.
Q.tree.query([5,21])
Out[8]:
(6.082762530298219, 3) 

谢谢你的努力。实际上,5*5数据属于一个光栅图像,因此有3个光栅图像放在一个numpy数组中。现在我想通过插值其他两个光栅图像中的可用值来填充一个光栅图像的np.nan值。当然,在插值每个光栅图像中相同位置的值时应该考虑。我想使用scipy.interpolate函数和内置的三次或样条方法。当我将数据重塑为数据时。重塑(3,25),你能教我如何应用三次插值吗?