Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SciPy interp2D for pairs of coordinates

I'm using scipy.interpolate.interp2d to create an interpolation function for a surface. I then have two arrays of real data that I want to calculate interpolated points for. If I pass the two arrays to the interp2d function I get an array of all the points, not just the pairs of points.

My solution to this is to zip the two arrays into a list of coordinate pairs and pass this to the interpolation function in a loop:

f_interp = interpolate.interp2d(X_table, Y_table,Z_table, kind='cubic')

co_ords = zip(X,Y)
out = []
for i in range(len(co_ords)):
    X = co_ords[i][0]
    Y = co_ords[i][1]
    value = f_interp(X,Y)
    out.append(float(value))

My question is, is there a better (more elegant, Pythonic?) way of achieving the same result?

like image 696
BMichell Avatar asked Feb 12 '16 10:02

BMichell


People also ask

What does SciPy interpolate interp2d return?

This class returns a function whose call method uses spline interpolation to find the value of new points.

What is interpolation in SciPy?

Interpolation is a technique of constructing data points between given data points. The scipy. interpolate is a module in Python SciPy consisting of classes, spline functions, and univariate and multivariate interpolation classes.


1 Answers

Passing all of your points at once will probably be quite a lot faster than looping over them in Python. You could use scipy.interpolate.griddata:

Z = interpolate.griddata((X_table, Y_table), Z_table, (X, Y), method='cubic')

or one of the scipy.interpolate.BivariateSpline classes, e.g. SmoothBivariateSpline:

itp = interpolate.SmoothBivariateSpline(X_table, Y_table, Z_table)
# NB: choose grid=False to get an (n,) rather than an (n, n) output
Z = itp(X, Y, grid=False)

CloughTocher2DInterpolator also works in a similar fashion, but without the grid=False parameter (it always returns a 1D output).

like image 107
ali_m Avatar answered Oct 07 '22 07:10

ali_m