pythonscipyinterpolationdeprecation-warninglinear-interpolation

RegularGridInterpolator excruciatingly slow compared to interp2d


Consider the following code example:

# %%
import numpy
from scipy.interpolate import interp2d, RegularGridInterpolator

x = numpy.arange(9000)
y = numpy.arange(9000)
z = numpy.random.randint(-1000, high=1000, size=(9000, 9000))
f = interp2d(x, y, z, kind='linear', copy=False)

f2 = RegularGridInterpolator((x, y), z, "linear")

mx, my = np.meshgrid(x, y)
M = np.stack([mx, my], axis=-1)

# %%
%timeit f(x, y)

# %%
%timeit f2(M)

It sets up some example interpolators using scipy.interpolate.interp2d and scipy.interpolate.RegularGridInterpolator. The output of the two cells above is

1.09 s ± 4.38 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

and

10 s ± 17.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

respectively.

The RegularGridInterpolator is about 10 times slower than the interp2d. The problem is that interp2d has been marked as deprecated in scipy 1.10.0. And new code should use RegularGridInterpolator. This seems a bit strange to me since it would be such a bad replacement. Is there maybe a problem in my code example above? How can I speed this interpolation process up?


Solution

  • There is no problem with your code, it's probably a bug in scipy. I've reported it on github