Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use scipy.interpolate.interp2d for a vector of data?

I have a table of measured values for a quantity that depends on two parameters. So say I have a function fuelConsumption(speed, temperature), for which data on a mesh are known.

Now I want to interpolate the expected fuelConsumption for a lot of measured data points (speed, temperature) from a pandas.DataFrame (and return a vector with the values for each data point).

I am currently using SciPy's interpolate.interp2d for interpolation, but when passing the parameters as two vectors [s1,s2] and [t1,t2] (only two ordered values for simplicity) it will construct a mesh and return:

[[f(s1,t1), f(s2,t1)], [f(s1,t2), f(s2,t2)]]

The result I am hoping to get is:

[f(s1,t1), f(s2, t2)]

How can I interpolate to get the output I want?

like image 597
Tim Avatar asked Feb 09 '23 14:02

Tim


1 Answers

From scipy v0.14 onwards you can use scipy.interpolate.RectBivariateSpline with grid=False:

import numpy as np
from scipy.interpolate import RectBivariateSpline
from matplotlib import pyplot as plt


x, y = np.ogrid[-1:1:10j,-1:1:10j]
z = (x + y)*np.exp(-6.0 * (x * x + y * y))

spl = RectBivariateSpline(x, y, z)

xi = np.linspace(-1, 1, 50)
yi = np.linspace(-1, 1, 50)
zi = spl(xi, yi, grid=False)

fig, ax = plt.subplots(1, 1)
ax.hold(True)
ax.imshow(z, cmap=plt.cm.coolwarm, origin='lower', extent=(-1, 1, -1, 1))
ax.scatter(xi, yi, s=60, c=zi, cmap=plt.cm.coolwarm)

enter image description here

like image 145
ali_m Avatar answered Feb 12 '23 03:02

ali_m