Why Would Scipy's Interp1d Take Over A Minute To Build An Interpolator?
I'd like to quad or cube interpolate a long series of floats (or vectors) in 1d, where long could be 1E+05 or 1E+06 (or more). For some reason SciPi's handy interp1d()'s time overh
Solution 1:
Short answer: update your scipy installation.
Longer answer: pre-0.19, interp1d
was based on splmake
which is using linear algebra with full matrices. In scipy 0.19, it was refactored to use banded linear algebra. As a result, (below is with scipy 0.19.1)
In [14]: rndm = np.random.RandomState(1234)
In [15]: for n in [100, 1000, 10000, int(1e6)]:
...: x = np.sort(rndm.uniform(size=n))
...: y = np.sin(x)
...: %timeit interp1d(x, y, kind='quadratic', assume_sorted=True)
...:
...:
244 µs ± 4.6 µs per loop (mean ± std. dev. of7 runs, 1000 loops each)
422 µs ± 4.21 µs per loop (mean ± std. dev. of7 runs, 1000 loops each)
2.17 ms ± 50.2 µs per loop (mean ± std. dev. of7 runs, 100 loops each)
227 ms ± 4.45 ms per loop (mean ± std. dev. of7 runs, 1 loop each)
In [16]:
In [16]: for n in [100, 1000, 10000, int(1e6)]:
...: x = np.sort(rndm.uniform(size=n))
...: y = np.sin(x)
...: %timeit interp1d(x, y, kind='cubic', assume_sorted=True)
...:
...:
241 µs ± 4.67 µs per loop (mean ± std. dev. of7 runs, 1000 loops each)
462 µs ± 4.92 µs per loop (mean ± std. dev. of7 runs, 1000 loops each)
2.64 ms ± 37.6 µs per loop (mean ± std. dev. of7 runs, 100 loops each)
276 ms ± 1.91 ms per loop (mean ± std. dev. of7 runs, 1 loop each)
Your other options are CubicSpline
if you want cubic splines specifically (this is new in scipy 0.18) or make_interp_spline
if you want also quadratic splines (new in scipy 0.19; this is what interp1d
is using under the hood).
Post a Comment for "Why Would Scipy's Interp1d Take Over A Minute To Build An Interpolator?"