简体   繁体   中英

Python with dataframe sales values - forecast next value with numpy.polyfit?

I have this dataframe:

       sales
0  22.000000
1  25.000000
2  22.000000
3  18.000000
4  23.000000

and want to 'forecast' the next value as a simple numeric to pass it on easily to another code. I thought numpy.polyfit could do that, but not sure how. I am just after something simple.

If numpy.polyfit can do a curve fit, the curve can continue to the next x value. This would give a forecast like in excel. That's good for me.

Unfortunately, numpy.org doesn't say how forecasting is possible, hence asking here:)

Forecasting can be done by using np.poly1d(z) as described in the documentation .

n = 10 # number of values you would like to forecast
deg = 1 # degree of polynomial

df = pd.DataFrame([22, 25, 22, 18, 23], columns=['Sales'])

z = np.polyfit(df.index, df.Sales, deg)
p = np.poly1d(z)

for row in range(len(df), len(df) + n):
    df.loc[row, 'Sales'] = p(row)

You can change n and deg as you like. But remember, you only have 5 observations so deg must be 4 or lower.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM