Search code examples
pythonscipyinterpolationcubic-spline

Scipy interp1d cubic interpolation results different between scipy 0.18.1 and 1.2.1?


The results obtained by a kind='cubic' interpolation using interp1d seem to change between scipy 0.18.1 and 1.2.1. I use python 2.7.12/numpy 1.12.0 with the former and python 3.6.8/numpy 1.16.2 with the latter. Which change in the source code is responsible for the different results?

Example code:

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

np.random.seed(1000)
n_p = 6
n_p_interpolated = 11

a = np.linspace(0,5,n_p)
a_interpolated = np.linspace(0,5,n_p_interpolated)
b = np.random.rand(n_p)

b_interpolated = interp1d(a,b,kind='cubic')(a_interpolated)

print('b:', b)
print('b_interpolated:', b_interpolated)

plt.ion()
plt.figure()
plt.plot(a,b,label='original data')
plt.plot(a_interpolated,b_interpolated,label='interpolated data')
plt.legend()
plt.xlim(-1,6)
plt.ylim(-0.2,1)

With scipy 0.18.1:

('b:', array([ 0.65358959,  0.11500694,  0.95028286,  0.4821914 ,  0.87247454,
        0.21233268]))
('b_interpolated:', array([ 0.65358959, -0.1292027 ,  0.11500694,  0.64514961,  0.95028286,
        0.75328508,  0.4821914 ,  0.58350879,  0.87247454,  0.95267129,
        0.21233268]))

With scipy 1.2.1:

b: [0.65358959 0.11500694 0.95028286 0.4821914  0.87247454 0.21233268]
b_interpolated: [ 0.65358959 -0.05237073  0.11500694  0.62584926  0.95028286  0.75365451
  0.4821914   0.60133139  0.87247454  0.88101143  0.21233268]

All interpolated values (that are not in the original grid) are different, some quite drastically (such as the second one).


Solution

  • Yes, the algorithm for constructing the spline was changed. In scipy 0.18 and below, it used the find_smoothest algorithm (see scipy/interpolate/interpolate.py in the scipy source repository, pre scipy 0.19.0). From version 0.19.0 onwards, the default algorithm for cubics is not-a-knot.