Search code examples
pythonscipyinterpolation

Using scipy.interpolate in python, I want to erase certain values and interpolate them


In my code, I have certain values like

my_list = [725.998474, 0.0, 0.0, 0.0, 0.0, 789.507934, 792.585388, 801.612916, 799.38916, 809.280518, 809.186036, 811.899414, .... , 412.314528]

In my code, I want to interpolate the points where the list is 0.0 because they are outliers. But it is not working as interpolation only works for empty values.

How can I erase those 0.0 values and do interpolation?


Solution

  • Using the data you provided, we can create a "cleaned" list of the x and y data using numpy. You said that the values equal to 0 are the outliers, but checking equality with floating point numbers can lead to issues, so I used np.isclose. With the outliers removed, you can interpolate the cleaned data.

    import numpy as np
    from scipy.interpolate import make_interp_spline
    import matplotlib.pyplot as plt
    
    plt.close("all")
    
    y = np.array([725.998474, 0.0, 0.0, 0.0, 0.0, 789.507934, 792.585388,
                  801.612916, 799.38916, 809.280518, 809.186036, 811.899414, 
                  412.314528])
    x = np.arange(len(y))
    
    outliers = np.isclose(y, 0)
    y_clean = y[~outliers]
    x_clean = x[~outliers]
    
    spline = make_interp_spline(x_clean, y_clean)
    y_interped = spline(x)
    
    fig, ax = plt.subplots()
    ax.plot(x_clean, y_clean, ".", label="cleaned", zorder=3)
    ax.plot(x, y_interped, label="interpolated")
    ax.legend()
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    fig.show()
    

    If, as @Reinderien suggested, your actual condition is that values below 100, for example, are outliers, then you could change it to that condition (i.e. outliers = y < 100).