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

I have a table of measured values ​​for a quantity that depends on two parameters. So to speak, I have a function fuelConsumption(speed, temperature)for which grid data is known.

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

I am currently using SciPy interpolate.interp2dfor interpolation, but when passing parameters as two vectors [s1,s2]and [t1,t2](only for two ordered values ​​for simplicity) it will build a grid and return:

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

The result that I hope to get:

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

How can I interpolate to get the result I want?

+4
source share
1 answer

With scipy v0.14 onwards you can use scipy.interpolate.RectBivariateSplinewith 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

+7
source

All Articles