# [SciPy-User] interpolate.interp1d - constructing a cubic interpolator is Slllooooooow. Classic List Threaded 4 messages Open this post in threaded view
|

## [SciPy-User] interpolate.interp1d - constructing a cubic interpolator is Slllooooooow.

 I'm intending to use interpolation in a curvefitting function.  So have been investigating the use of interpolate.interp1d.I'd prefer to use cubic interpolation but it seems to take ages: import numpy as npfrom scipy.interpolate import interp1d a = np.linspace(-2 * np.pi, 2 * np.pi, 1000) b = np.cos(a) %timeit interp1d(a, b) 10000 loops, best of 3: 71.6 µs per loop %timeit interp1d(a, b, kind='cubic') 1 loops, best of 3: 5.15 s per loopI'm wondering why it takes 5 orders of magnitude (x72000) longer to calculate a cubic interpolator than a linear interpolator?cheers,Andrew. -- _____________________________________Dr. Andrew Nelson_____________________________________ _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user
Open this post in threaded view
|

## Re: interpolate.interp1d - constructing a cubic interpolator is Slllooooooow.

 Dear Andrew, On Thu, Jan 15, 2015 at 9:46 AM, Andrew Nelson wrote:I'm intending to use interpolation in a curvefitting function.  So have been investigating the use of interpolate.interp1d.I'd prefer to use cubic interpolation but it seems to take ages: import numpy as npfrom scipy.interpolate import interp1d a = np.linspace(-2 * np.pi, 2 * np.pi, 1000) b = np.cos(a) %timeit interp1d(a, b) 10000 loops, best of 3: 71.6 µs per loop %timeit interp1d(a, b, kind='cubic') 1 loops, best of 3: 5.15 s per loopI'm wondering why it takes 5 orders of magnitude (x72000) longer to calculate a cubic interpolator than a linear interpolator?I can reproduce your results but I cannot comment on the reason why there exist a so big difference between the two cases. On the other hand, following the documentation, I would go for the use of the more recent UnivariateSpline (http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.UnivariateSpline.html#scipy.interpolate.UnivariateSpline) class that doesn't have this drawback. On my system, following your example, I get: %timeit spl = UnivariateSpline(a,b,k=3)`1000 loops, best of 3: 271 µs per loop``Cheers,`Eraldo   _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user
 Great, thanks.I found that interpolate.InterpolatedUnivariateSpline is going to do the job for me.On 15 January 2015 at 20:49, Eraldo Pomponi wrote:Dear Andrew, On Thu, Jan 15, 2015 at 9:46 AM, Andrew Nelson wrote:I'm intending to use interpolation in a curvefitting function.  So have been investigating the use of interpolate.interp1d.I'd prefer to use cubic interpolation but it seems to take ages: import numpy as npfrom scipy.interpolate import interp1d a = np.linspace(-2 * np.pi, 2 * np.pi, 1000) b = np.cos(a) %timeit interp1d(a, b) 10000 loops, best of 3: 71.6 µs per loop %timeit interp1d(a, b, kind='cubic') 1 loops, best of 3: 5.15 s per loopI'm wondering why it takes 5 orders of magnitude (x72000) longer to calculate a cubic interpolator than a linear interpolator?I can reproduce your results but I cannot comment on the reason why there exist a so big difference between the two cases. On the other hand, following the documentation, I would go for the use of the more recent UnivariateSpline (http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.UnivariateSpline.html#scipy.interpolate.UnivariateSpline) class that doesn't have this drawback. On my system, following your example, I get: %timeit spl = UnivariateSpline(a,b,k=3)`1000 loops, best of 3: 271 µs per loop``Cheers,`Eraldo   _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user -- _____________________________________Dr. Andrew Nelson_____________________________________ _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user