2

I would like to create a grid-like group of variables in python from a table (e.g., one text file) for an N-dimensional dataset.

Suppose my data have the following format.

column_x = [x0, x1, x2, ..., xN]

column_y = [y0, y1, y2, ..., yN]

column_z = [z0, z1, z2, ..., zN]

column_values = [value0, value1, value2, ..., valueN]

Usually, I am interested in creating an interpolator, e.g., given (x,y,z), compute the corresponding value.

I have ran into this several times and always used some ugly hack. I was wondering if there is a clean, pythonic way of doing this.

Numpy/Scipy solutions are welcome!

Álvaro
  • 1,219
  • 2
  • 12
  • 20
  • Have you seen `scipy`'s [`griddata`](http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.griddata.html#scipy.interpolate.griddata) interpolation function? – jme Dec 23 '14 at 01:10
  • I had seen it before but could not find a way to generalize it to this situation. I just had, thanks a lot! I will post the solution right now. – Álvaro Dec 23 '14 at 01:17

1 Answers1

1

As @jme pointed out,scipy's griddata is the way. This code works

import numpy as np
from scipy.interpolate import griddata

# get x_values, y_values, z_values, func_values
x_values, y_values, z_values, func_values = theta

positions = np.vstack([x_values,y_values,z_values]).T
position_to_evaluate = [x, y, z]
value = griddata(positions, func_values, position_to_evaluate)
Álvaro
  • 1,219
  • 2
  • 12
  • 20