Search code examples
pythonpython-2.7numpyscipy

extrapolating data with numpy/python


Let's say I have a simple data set. Perhaps in dictionary form, it would look like this:

{1:5, 2:10, 3:15, 4:20, 5:25}

(the order is always ascending). What I want to do is logically figure out what the next point of data is most likely to be. In the case, for example, it would be {6: 30}

what would be the best way to do this?


Solution

  • After discussing with you in the Python chat - you're fitting your data to an exponential. This should give a relatively good indicator since you're not looking for long term extrapolation.

    import numpy as np
    from scipy.optimize import curve_fit
    import matplotlib.pyplot as plt
    
    def exponential_fit(x, a, b, c):
        return a*np.exp(-b*x) + c
    
    if __name__ == "__main__":
        x = np.array([0, 1, 2, 3, 4, 5])
        y = np.array([30, 50, 80, 160, 300, 580])
        fitting_parameters, covariance = curve_fit(exponential_fit, x, y)
        a, b, c = fitting_parameters
        
        next_x = 6
        next_y = exponential_fit(next_x, a, b, c)
        
        plt.plot(y)
        plt.plot(np.append(y, next_y), 'ro')
        plt.show()
    

    The red dot in the on far right axis shows the next "predicted" point.