在scipy中插入np.nan值

时间:2014-05-03 01:56:44

标签: 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, 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)

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

1 个答案:

答案 0 :(得分:1)

你的问题只是太过分了,插值不需要来自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) 
相关问题