Interpolate 2d missing values ​​python

I have a 2d array (or matrix, if you want) with some missing values ​​represented as NaN. Missing values ​​are usually in a strip along one axis, for example:

1   2   3 NaN   5
2   3   4 Nan   6
3   4 Nan Nan   7
4   5 Nan Nan   8
5   6   7   8   9

where I would like to replace NaNwith a few reasonable numbers.

I looked at delaunay triangulation, but found very little documentation.

I tried using astropyconvolve as it supports the use of 2d arrays and is pretty simple. The problem is that convolution is not interpolation, it moves all the values ​​to the average (which can be mitigated using a narrow kernel).

This question should be a natural two-dimensional extension of this post . Is there any way to interpolate over NaN/ missing values ​​in a 2d array?

+4
source share
2 answers

Yes, you can use a scipy.interpolate.griddatamasked array as well, and you can choose the type of interpolation that you prefer, using an argument methodusually 'cubic'do an excellent job:

import numpy as np
from scipy import interpolate


#Let create some random  data
array = np.random.random_integers(0,10,(10,10)).astype(float)
#values grater then 7 goes to np.nan
array[array>7] = np.nan

It looks something like this: plt.imshow(array,interpolation='nearest')

enter image description here

x = np.arange(0, array.shape[1])
y = np.arange(0, array.shape[0])
#mask invalid values
array = np.ma.masked_invalid(array)
xx, yy = np.meshgrid(x, y)
#get only the valid values
x1 = xx[~array.mask]
y1 = yy[~array.mask]
newarr = array[~array.mask]

GD1 = interpolate.griddata((x1, y1), newarr.ravel(),
                          (xx, yy),
                             method='cubic')

This is the end result:

enter image description here

See that if nan values ​​are at the edges and surrounded by nan values, they cannot be interpolated and stored nan. You can change it using the argument fill_value.

, NaN 3x3, ?

, . , , , , . , .. , , , .

- :

reference = array[3:6,3:6].copy()
array[3:6,3:6] = np.nan
method = ['linear', 'nearest', 'cubic']

for i in method:
    GD1 = interpolate.griddata((x1, y1), newarr.ravel(),
                              (xx, yy),
                                 method=i)
    meandifference = np.mean(np.abs(reference - GD1[3:6,3:6]))
    print ' %s interpolation difference: %s' %(i,meandifference )

- :

   linear interpolation difference: 4.88888888889
   nearest interpolation difference: 4.11111111111
   cubic interpolation difference: 5.99400137377

, , , . , , " " , .

+6

, , Nans, Nans , Nans, , . , Nans .

0

All Articles