2

I have n equal length arrays whose transpose corresponds to the coordinates in an n dimensional parameter space:

x = np.array([800,800,800,800,900,900,900,900,900,1000,1000,1000,1000,1000])
y = np.array([4.5,5.0,4.5,5.0,4.5,5.0,5.5,5.0,5.5,4.5,5.0,5.5,5.0,5.5])
z = np.array([2,2,4,4,2,2,4,4,4,2,2,4,4,4])

Each coordinate in parameter space also has a value:

v = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14])

I want to interpolate between the grid points to get the v value at given arbitrary xyz coordinate, e.g. [934,5.1,3.3].

I've been trying to use scipy.RegularGridInterpolator, which takes (x,y,z) as the first argument, but I can't figure out how to construct the second argument of the values at each point.

Any suggestions would be greatly appreciated! Thanks!

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Joe Flip
  • 1,076
  • 4
  • 21
  • 37
  • 1
    Perhaps [this answer](http://stackoverflow.com/a/30057858/832621) has what you want. – Saullo G. P. Castro Aug 13 '15 at 01:45
  • 1
    @SaulloCastro Sort of, but not quite. `RegularGridInterpolator` does the interpolation for you, is much faster, and you can choose different interpolation methods. All I need is to convert my array of values into a grid of the same shape as the `xyz` parameter space. – Joe Flip Aug 13 '15 at 02:15

1 Answers1

3

Your input would fit better with LinearNDInterpolator or NearestNDInterpolator:

from scipy.interpolate import LinearNDInterpolator

ex = LinearNDInterpolator((x, y, z), v)
ex((800, 4.5, 2))
#array(1.0)

ex([[800, 4.5, 2], [800, 4.5, 3]])
#array([ 1.,  2.])

To use RegularGridInterpolator you need to define v as a regular array. For example, assume that:

x = np.array([800., 900., 1000.])
y = np.array([4.5, 5.0, 5.5, 6.0])
z = np.array([2., 4.])

The array v could be something like:

v = np.array([[[ 1.,  2.],
               [ 1.,  2.],
               [ 1.,  2.],
               [ 1.,  2.]],

              [[10., 20.],
               [10., 20.],
               [10., 20.],
               [10., 20.]],

              [[100., 200.],
               [100., 200.],
               [100., 200.],
               [100., 200.]]])

And then you would be able to interpolate:

form scipy.interpolate import RegularGridInterpolator

rgi = RegularGridInterpolator((x, y, z), v)

rgi((850., 4.5, 3.))
#array(8.25)

rgi([[850., 4.5, 3.], [800, 4.5, 3]])
#array([ 8.25,  1.5 ])
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234