0

I am trying to interpolate missing values of a 2D array in Python. I found this question, however in that case the rows and columns are all equidistant. In my case I have two arrays

x = [275. 290. 310. 330. 350. 410. 450.]
y = [ 8. 12. 16. 20. 30. 35. 40. 45.]

where x and y are the grid coordinates that represent the column and row nodes at which my 2d array

c = [[4 6   9 9   9 8 2]
     [1 6   3 7   1 5 4]
     [8 nan 3 nan 2 9 2]
     [8 2   3 4   3 4 7]
     [2 nan 4 nan 6 1 3]
     [4 nan 8 nan 1 7 6]
     [8 nan 6 nan 5 6 5]
     [1 nan 1 nan 3 1 9]]

is defined.

What is the best way to fill the missing values?

Axel
  • 1,415
  • 1
  • 16
  • 40

1 Answers1

0

scipy includes 2D interpolation for grid data (there are some other interpolation functions as well):

import numpy as np
import pandas as pd
from numpy import nan
from scipy.interpolate import griddata

x = [275, 290, 310, 330, 350, 410, 450]
y = [ 8, 12, 16, 20, 30, 35, 40, 45,]
c = np.array([[ 4.,  6.,  9.,  9.,  9.,  8.,  2.],
       [ 1.,  6.,  3.,  7.,  1.,  5.,  4.],
       [ 8., nan,  3., nan,  2.,  9.,  2.],
       [ 8.,  2.,  3.,  4.,  3.,  4.,  7.],
       [ 2., nan,  4., nan,  6.,  1.,  3.],
       [ 4., nan,  8., nan,  1.,  7.,  6.],
       [ 8., nan,  6., nan,  5.,  6.,  5.],
       [ 1., nan,  1., nan,  3.,  1.,  9.]])

# generate x_coord, y_coord values for each grid point
x_grid, y_grid = np.meshgrid(x,y)

# get known values to set the interpolator
mask = [~np.isnan(c)]
x = x_grid[mask].reshape(-1)
y = y_grid[mask].reshape(-1)
points = np.array([x,y]).T
values = c[mask].reshape(-1) 

# generate interpolated grid data
interp_grid = griddata(points, values, (x_grid, y_grid), method='nearest')

# interp grid:

array([[4., 6., 9., 9., 9., 8., 2.],
       [1., 6., 3., 7., 1., 5., 4.],
       [8., 6., 3., 7., 2., 9., 2.],
       [8., 2., 3., 4., 3., 4., 7.],
       [2., 2., 4., 4., 6., 1., 3.],
       [4., 4., 8., 4., 1., 7., 6.],
       [8., 8., 6., 6., 5., 6., 5.],
       [1., 1., 1., 1., 3., 1., 9.]])
anon01
  • 10,618
  • 8
  • 35
  • 58