Hice un modelo ARMA para predecir pandas.Series de ventas de algunos artículos en diferentes tiendas. Para cada serie de tiempo, si hay datos, prueba y guarda el modelo con el mejor criterio de información de Akaike. Sin embargo, siempre da los mismos resultados, así que supongo que tengo un problema en alguna parte, pero no he sido capaz de encontrar dónde. De hecho, aquí está mi modelo:
import statsmodels.tsa.api as smt
array = []
for i, row in test.iterrows():
print("row['shop_id']: ", row['shop_id'], " row['item_id']: ", row['item_id'])
ts = pd.DataFrame(sales_monthly.loc[pd.IndexSlice[:, [row['shop_id']],[row['item_id']]], :]['item_price'].values*sales_monthly.loc[pd.IndexSlice[:, [row['shop_id']],[row['item_id']]], :]['item_cnt_day'].values).T.iloc[0]
print(ts.values)
if ts.values != []:
best_aic = np.inf
best_order = None
best_model = None
rng = range(5)
for i in rng:
for j in rng:
try:
tmp_model = smt.ARMA(ts.values, order = (i, j)).fit(method='mle', trand='nc')
tmp_aic = tmp_model.aic
if tmp_aic < best_aic:
best_aic = tmp_aic
best_order = (i, j)
best_model = tmp_mdl
except Exception as e:
continue
y_hat = best_model.forecast()[0][0]
if y_hat<0:
y_hat = 0
else:
y_hat = 0
print("predicted:", y_hat)
d = {'id':row['ID'], 'item_cnt_month': y_hat}
array.append(d)
print("-------------------")
df = pd.DataFrame(array)
df
Devuelve:
row['shop_id']: 5 row['item_id']: 5037
[2599. 2599. 3998. 3998. 1299. 1499. 1499. 2997.5 749.5]
predicted: 15001.056988528915
-------------------
row['shop_id']: 5 row['item_id']: 5320
[]
predicted: 0
-------------------
row['shop_id']: 5 row['item_id']: 5233
[2697. 1198. 599. 2997. 1199.]
predicted: 15001.056988528915
-------------------
row['shop_id']: 5 row['item_id']: 5232
[599.]
predicted: 0
-------------------
row['shop_id']: 5 row['item_id']: 5268
[]
predicted: 0
-------------------
row['shop_id']: 5 row['item_id']: 5039
[5198. 6597. 2599. 5197. 749.5 1499. ]
predicted: 15001.056988528915
-------------------
row['shop_id']: 5 row['item_id']: 5041
[11497. 7998.]
predicted: 15001.056988528915
-------------------
row['shop_id']: 5 row['item_id']: 5046
[ 299. 1495. 349. 349.]
predicted: 15001.056988528915
-------------------
...
No lo entiendo, porque cuando intento predecirlos uno a uno funciona bien. Por ejemplo con lo siguiente ts.values
:
array([ 7770. , 15640. , 15540. , 12950. ,
30775. , 15950. , 12760. , 22330. ,
15949.64285714, 0. , 6380. , 3190. ,
9670. , 3490. , 3090. , 3490. ,
3490. , 10470. ])
And:
import statsmodels.tsa.api as smt
# pick best order by Aikake Information Criterion smallest aic wins
best_aic = np.inf
best_order = None
best_mdl = None
rng = range(5)
for i in rng:
for j in rng:
try:
tmp_mdl = smt.ARMA(ts.values, order = (i, j)).fit(method='mle', trand='nc')
tmp_aic = tmp_mdl.aic
if tmp_aic < best_aic:
best_aic = tmp_aic
best_order = (i, j)
best_mdl = tmp_mdl
except:
continue
print(best_aic, best_order)
print('aic: {} | order: {}'.format(best_aic, best_order))
print(best_mdl.forecast()[0][0])
Y devuelve:
204.39695560597815 (0, 0)
aic: 204.39695560597815 | order: (0, 0)
1712.4545454545446