Search code examples
pythonpandasnumpyregressionnon-linear-regression

Shelf life prediction using polynomial fit of several results


I have results of several batches of the same product over time, e.g.:

x1 = np.array([0, 3, 6]) # x coordinates of batch1
y1 = np.array([0.51, 0.69, 0.78]) # y coordinates of batch1
batch1 = '29880G001'
x2 = np.array([0, 3, 6]) # x coordinates of batch2
y2 = np.array([0.29, 0.77, 0.61]) # y coordinates of batch2
batch2 = '30565G002'
x3 = np.array([0, 3, 6]) # x coordinates of batch3
y3 = np.array([0.29, 0.61, 0.6]) # y coordinates of batch3
batch3 = '30657G003'

Y values are time in months and X values are a lab result for the specific batch. Presenting them on a chart: An example chart

I am looking for a way to make ONE polynominal fit to these results which I can carry forward, i.e. extrapolate, to make predictions on the product behaviour in the future.

Thanks!


Solution

  • Concat your arrays and do polyfit:

    np.polynomial.polynomial.polyfit(np.r_[x1,x2,x3], np.r_[y1,y2,y3], 3)
    

    But I very much doubt that it makes much sense to use a polynomial to extrapolate.

    Example:

    z = np.polynomial.polynomial.polyfit(np.r_[x1,x2,x3], np.r_[y1,y2,y3], 3)
    
    plt.plot(x1, y1, 'bo', x2, y2, 'ro', x3, y3, 'go')
    x = np.linspace(0, 10, 100)
    plt.plot(x, np.polynomial.polynomial.polyval(x, z))
    

    enter image description here