pythonnumpyscipylinear-interpolation

What's the function object alternative to 1D linear interpolation with SciPy/NumPy?


I'm looking for a way to create a "functor" for linear interpolation of time,value pairs using SciPy (or NumPy) but according to the SciPy tutorial there is none! (Kind of the opposite of Trying to understand scipy and numpy interpolation)

The natural method would be interp1d but that has a warning:

Legacy This class is considered legacy and will no longer receive updates. […] For a guide to the intended replacements for interp1d see 1-D interpolation.

Following the link takes me to a page that tells me:

If all you need is a linear (a.k.a. broken line) interpolation, you can use the numpy.interp routine.

The problem is, these are not at all equivalent. numpy.interp requires me to know the points beforehand, and does not return a function that can be used to look up the interpolated values.

Meanwhile, SciPy has a number of other interpolation methods that all return a function (or a function object) such as CubicSpline or PchipInterpolator.

What's the easy way to construct a function or object similar to what PchipInterpolator returns, but for simple linear interpolation now that interp1d is deprecated?


Solution

  • You can use scipy.interpolate.make_interp_spline with k=1 for linear interpolation.

    import numpy as np
    from scipy.interpolate import make_interp_spline, interp1d
    import matplotlib.pyplot as plt
    
    plt.close("all")
    
    x = np.linspace(0, 20, 10)
    y = np.cos(x**2)/(x+1)
    
    x_fine = np.linspace(0, 20, 100)
    y_np = np.interp(x_fine, x, y)
    f_sp1 = make_interp_spline(x, y, k=1)
    y_sp1 = f_sp1(x_fine)
    f_sp2 = interp1d(x, y)
    y_sp2 = f_sp2(x_fine)
    
    fig, ax = plt.subplots()
    ax.plot(x, y, ".", label="Original")
    ax.plot(x_fine, y_np, label="Numpy")
    ax.plot(x_fine, y_sp1, label="Scipy (make_interp_spline)")
    ax.plot(x_fine, y_sp2, label="Scipy (interp1d)")
    ax.legend()
    fig.show()