import pandas as pd
import numpy as np
import warnings
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA
%matplotlib inline
warnings.filterwarnings("ignore")
df = pd.read_excel("Dados.xlsx")
def mape(actual, pred):
return np.mean(np.abs((actual - pred) / actual)) * 100
df = df[["Data", "Vendas"]]
df.columns = ["Data", "Vendas"]
df["date"] = pd.to_datetime(df.Data)
df.set_index(df.Data, inplace = True)
df.sort_index(ascending = True, inplace = True)
df.drop("Data", axis = 1, inplace = True)
df.head()
Vendas | date | |
---|---|---|
Data | ||
2022-12-06 | 870 | 2022-12-06 |
2022-12-07 | 868 | 2022-12-07 |
2022-12-08 | 1189 | 2022-12-08 |
2022-12-09 | 742 | 2022-12-09 |
2022-12-10 | 317 | 2022-12-10 |
plt.figure(figsize = (15, 10))
ax = plt.subplot(111)
ax.plot(df.index, df.Vendas)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.tick_params(labelsize = 16)
plt.show()
gr_dt = df.groupby(df.index.day).sum()
plt.figure(figsize = (15, 10))
ax = plt.subplot(111)
ax.plot(gr_dt.index, gr_dt.Vendas, color = "black", marker = "o")
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.tick_params(labelsize = 16)
plt.show()
"""params = []
for x in range(0, 11):
for y in range(0, 11):
for z in range(0, 11):
params.append((x, y, z))
best_param = None
best_acc = float("inf")
for param in params:
try:
model = ARIMA(df.Vendas, order = param).fit()
acc = mape(df.Vendas, model.predict(typ = "levels"))
if acc < best_acc:
best_acc = acc
best_param = param
print("Order: ", param, "MAPE:", acc)
except:
pass
print("Melhor order: ", best_param, " MAPE:", best_acc)"""
model = ARIMA(df.Vendas, order = (7,1,6)).fit()
acc = mape(df.Vendas, model.predict(typ = "levels"))
fig, ax = plt.subplots(figsize = (15, 10))
actual = ax.plot(df.Vendas, color = "deepskyblue")
pred = ax.plot(model.predict(typ = "levels"), color = "orangered")
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.tick_params(labelsize = 16)
plt.show()
pred_steps = 5
pred_data = model.forecast(steps = pred_steps)
plt.figure(figsize = (15, 10))
ax = plt.subplot(111)
ax.plot(pred_data, color = "black", marker = "o")
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.tick_params(labelsize = 16)
plt.show()