0% encontró este documento útil (0 votos)
52 vistas14 páginas

Técnicas de Forecast Con Python

Técnicas de Forecast Con Python

Cargado por

Woody Woodpecker
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
52 vistas14 páginas

Técnicas de Forecast Con Python

Técnicas de Forecast Con Python

Cargado por

Woody Woodpecker
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
Está en la página 1/ 14

Técnicas de forecast con

Python, Statsmodels y prophet

Modelos Holt Winters,


Sarima y Prophet
Forecast
Autor: Sergio Diaz Paredes

Contacto:

Linkedin
Github

Caso:
Una tienda de productos tecnológicos necesita generar una proyección de demanda
para cada uno de sus productos con el fin de optimizar el inventario y las estrategias de
venta. Esto implica analizar datos históricos de ventas, incluyendo factores como las
tendencias de compra, estacionalidad, promociones, precios y comportamientos de los
consumidores. Utilizando estos datos, la tienda puede aplicar modelos de predicción
(como regresión, series temporales o machine learning) para prever la demanda futura
de cada producto. Con esta proyección, se pueden ajustar los niveles de inventario y
mejorar la planificación de compras y marketing.

Paquetes
In [ ]: import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from statsmodels.tsa.statespace.sarimax import SARIMAX
from prophet import Prophet

Data
In [ ]: link = "https://raw.githubusercontent.com/SergioDP94/Talleres/refs/heads/main/Fo
data = pd.read_csv(link,parse_dates=['Fecha'])
data.head()

Out[ ]: Fecha Producto Medio_pago Sucursal Factura

0 2024-01-01 Laptops Efectivo Sucursal Sur FAC000001

1 2024-01-01 Laptops Tarjeta de Crédito Sucursal Centro FAC000002

2 2024-01-01 Laptops Efectivo Sucursal Norte FAC000003

3 2024-01-01 Laptops Efectivo Sucursal Centro FAC000004

4 2024-01-01 Laptops Efectivo Sucursal Norte FAC000005


In [ ]: data.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 49164 entries, 0 to 49163
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Fecha 49164 non-null datetime64[ns]
1 Producto 49164 non-null object
2 Medio_pago 49164 non-null object
3 Sucursal 49164 non-null object
4 Factura 49164 non-null object
dtypes: datetime64[ns](1), object(4)
memory usage: 1.9+ MB

In [ ]: data.pivot_table(index='Fecha',values='Factura',aggfunc='count').plot()

Out[ ]: <Axes: xlabel='Fecha'>

In [ ]: data.pivot_table(index='Fecha',values='Factura',columns='Producto',aggfunc='coun
Out[ ]: Producto Auriculares_inalambricos Camaras_digitales Laptops Reloj_inteligente Tablet

Fecha

2024-01-
84 19 51 30 30
01

2024-01-
83 20 49 29 3
02

2024-01-
83 21 52 32 28
03

2024-01-
88 19 55 36 2
04

2024-01-
87 19 50 31 30
05

... ... ... ... ... .

2024-05-
119 43 81 66 4
20

2024-05-
117 45 85 61 4
21

2024-05-
122 45 75 63 4
22

2024-05-
125 46 82 68 4
23

2024-05-
128 49 82 67 4
24

145 rows × 6 columns

In [ ]: tabla = data.pivot_table(index='Fecha',columns='Producto',values='Factura',aggfu
tabla.plot(ylim=(0),figsize=(12,6))

Out[ ]: <Axes: xlabel='Fecha'>


In [ ]: serie = tabla['Laptops']
serie.tail()

Out[ ]: Laptops

Fecha

2024-05-20 81

2024-05-21 85

2024-05-22 75

2024-05-23 82

2024-05-24 82

dtype: int64

In [ ]: from statsmodels.tsa.seasonal import seasonal_decompose

In [ ]: # Descomponer la serie
decomposition = seasonal_decompose(serie, model='additive', period=7)
plt.figure(figsize=(16, 8))

# Graficar los resultados


decomposition.plot()
plt.show()

<Figure size 1600x800 with 0 Axes>


Modelo Holt-Winters
In [ ]: n_step = 28

In [ ]: # Holt-Winters
holt_winters_model = ExponentialSmoothing(serie, trend='add', seasonal='add', se
holt_winters_fit = holt_winters_model.fit()

# Predicción de 7 periodos con Holt-Winters


holt_winters_forecast = holt_winters_fit.forecast(steps=n_step)

/usr/local/lib/python3.10/dist-packages/statsmodels/tsa/base/tsa_model.py:473: Va
lueWarning: No frequency information was provided, so inferred frequency D will b
e used.
self._init_dates(dates, freq)

In [ ]: holt_winters_fit.forecast(7).plot()

Out[ ]: <Axes: >


In [ ]: # Visualización de los resultados
plt.figure(figsize=(10, 6))
plt.plot(serie, label='Datos Históricos')
plt.plot(holt_winters_forecast, label='Holt-Winters Forecast', color='orange')
plt.legend()
plt.title('Proyección de Ventas')
plt.show()

Modelo Sarimax
In [ ]: # SARIMA Model
sarima_model = SARIMAX(serie, order=(1, 1, 1), seasonal_order=(1, 1, 1, 7))
sarima_fit = sarima_model.fit()

# Predicción con SARIMA


sarima_forecast = sarima_fit.forecast(steps=n_step)

/usr/local/lib/python3.10/dist-packages/statsmodels/tsa/base/tsa_model.py:473: Va
lueWarning: No frequency information was provided, so inferred frequency D will b
e used.
self._init_dates(dates, freq)
/usr/local/lib/python3.10/dist-packages/statsmodels/tsa/base/tsa_model.py:473: Va
lueWarning: No frequency information was provided, so inferred frequency D will b
e used.
self._init_dates(dates, freq)

In [ ]: # Visualización de los resultados


plt.figure(figsize=(10, 6))
plt.plot(serie.index, serie, label='Datos Históricos')
plt.plot(holt_winters_forecast, label='Holt-Winters Forecast', color='orange')
plt.plot(sarima_forecast, label='SARIMA Forecast', color='green')
plt.legend()
plt.title('Proyección de Ventas (Holt-Winters vs SARIMA)')
plt.show()

Prophet
In [ ]: df = serie.reset_index()
df.columns = ['ds', 'y'] # 'ds' es la fecha y 'y' es el valor
df.head()
Out[ ]: ds y

0 2024-01-01 51

1 2024-01-02 49

2 2024-01-03 52

3 2024-01-04 55

4 2024-01-05 50

In [ ]: # Crear y ajustar el modelo


prophet_model = Prophet(
daily_seasonality=False, # Activar estacionalidad diaria si es necesario
seasonality_mode='multiplicative', # Estacionalidad multiplicativa (si se a
yearly_seasonality=False, # Desactivar la estacionalidad anual si no la nec
weekly_seasonality=True # Desactivar la estacionalidad semanal si no es rel
)
prophet_model.fit(df)

DEBUG:cmdstanpy:input tempfile: /tmp/tmpwzbihkkt/9c2r3ozt.json


DEBUG:cmdstanpy:input tempfile: /tmp/tmpwzbihkkt/oyno2nq0.json
DEBUG:cmdstanpy:idx 0
DEBUG:cmdstanpy:running CmdStan, num_threads: None
DEBUG:cmdstanpy:CmdStan args: ['/usr/local/lib/python3.10/dist-packages/prophet/s
tan_model/prophet_model.bin', 'random', 'seed=11822', 'data', 'file=/tmp/tmpwzbih
kkt/9c2r3ozt.json', 'init=/tmp/tmpwzbihkkt/oyno2nq0.json', 'output', 'file=/tmp/t
mpwzbihkkt/prophet_model_vxcatnw/prophet_model-20250110234839.csv', 'method=optim
ize', 'algorithm=lbfgs', 'iter=10000']
23:48:39 - cmdstanpy - INFO - Chain [1] start processing
INFO:cmdstanpy:Chain [1] start processing
23:48:39 - cmdstanpy - INFO - Chain [1] done processing
INFO:cmdstanpy:Chain [1] done processing
Out[ ]: <prophet.forecaster.Prophet at 0x7fba6a257bb0>

In [ ]: # Hacer la predicción para 12 periodos


future = prophet_model.make_future_dataframe(periods=n_step, freq='D')

In [ ]: # Realizar la predicción
forecast = prophet_model.predict(future)

prophet_model.plot(forecast)
plt.legend()
plt.title('Proyección de Vuelos (Prophet)')
plt.show()
In [ ]: prohet_forecast = pd.Series(forecast.yhat[-n_step:].to_list(),index=sarima_forec

In [ ]: # Visualización de los resultados


plt.figure(figsize=(10, 6))
plt.plot(holt_winters_forecast, label='Holt-Winters Forecast', color='orange')
plt.plot(sarima_forecast, label='SARIMA Forecast', color='green')
plt.plot(prohet_forecast, label='Prohet Forecast', color='purple')

plt.legend()
plt.title('Proyección de Ventas (Holt-Winters vs SARIMA vs Prohet)')
plt.show()
Automatizacion
In [ ]: def get_forecast(serie,n_step,type='hw'):
if type == 'hw':
holt_winters_model = ExponentialSmoothing(serie, trend='add', seasonal='add'
holt_winters_fit = holt_winters_model.fit()
holt_winters_forecast = holt_winters_fit.forecast(steps=n_step)
return holt_winters_forecast
elif type == 'sarima':
sarima_model = SARIMAX(serie, order=(1, 1, 1), seasonal_order=(1, 1, 1, 7))
sarima_fit = sarima_model.fit()
sarima_forecast = sarima_fit.forecast(steps=n_step)
return sarima_forecast
elif type == 'prophet':
df = serie.reset_index()
df.columns = ['ds', 'y']
prophet_model = Prophet(
daily_seasonality=False,
seasonality_mode='multiplicative',
yearly_seasonality=False,
weekly_seasonality=True)
prophet_model.fit(df)
future = prophet_model.make_future_dataframe(periods=n_step, freq='D')
forecast = prophet_model.predict(future)
index = pd.date_range(serie.index[-1], periods=(n_step + 1), freq='D')[1:]
prohet_forecast = pd.Series(forecast.yhat[-n_step:].to_list(),index=index)
return prohet_forecast

In [ ]: get_forecast(serie,n_step,type='hw').plot()

/usr/local/lib/python3.10/dist-packages/statsmodels/tsa/base/tsa_model.py:473: Va
lueWarning: No frequency information was provided, so inferred frequency D will b
e used.
self._init_dates(dates, freq)
Out[ ]: <Axes: >
In [ ]: get_forecast(serie,n_step,type='sarima').plot()

/usr/local/lib/python3.10/dist-packages/statsmodels/tsa/base/tsa_model.py:473: Va
lueWarning: No frequency information was provided, so inferred frequency D will b
e used.
self._init_dates(dates, freq)
/usr/local/lib/python3.10/dist-packages/statsmodels/tsa/base/tsa_model.py:473: Va
lueWarning: No frequency information was provided, so inferred frequency D will b
e used.
self._init_dates(dates, freq)
Out[ ]: <Axes: >
In [ ]: get_forecast(serie,n_step,type='prophet').plot()

DEBUG:cmdstanpy:input tempfile: /tmp/tmpwzbihkkt/14cpvo8b.json


DEBUG:cmdstanpy:input tempfile: /tmp/tmpwzbihkkt/oiu4kvwp.json
DEBUG:cmdstanpy:idx 0
DEBUG:cmdstanpy:running CmdStan, num_threads: None
DEBUG:cmdstanpy:CmdStan args: ['/usr/local/lib/python3.10/dist-packages/prophet/s
tan_model/prophet_model.bin', 'random', 'seed=96885', 'data', 'file=/tmp/tmpwzbih
kkt/14cpvo8b.json', 'init=/tmp/tmpwzbihkkt/oiu4kvwp.json', 'output', 'file=/tmp/t
mpwzbihkkt/prophet_model1t5qryp_/prophet_model-20250110235202.csv', 'method=optim
ize', 'algorithm=lbfgs', 'iter=10000']
23:52:02 - cmdstanpy - INFO - Chain [1] start processing
INFO:cmdstanpy:Chain [1] start processing
23:52:02 - cmdstanpy - INFO - Chain [1] done processing
INFO:cmdstanpy:Chain [1] done processing
Out[ ]: <Axes: >
In [ ]: get_forecast(serie,n_step,type='prophet').sum()

DEBUG:cmdstanpy:input tempfile: /tmp/tmpwzbihkkt/j8n8o7ee.json


DEBUG:cmdstanpy:input tempfile: /tmp/tmpwzbihkkt/2j05b4r3.json
DEBUG:cmdstanpy:idx 0
DEBUG:cmdstanpy:running CmdStan, num_threads: None
DEBUG:cmdstanpy:CmdStan args: ['/usr/local/lib/python3.10/dist-packages/prophet/s
tan_model/prophet_model.bin', 'random', 'seed=49594', 'data', 'file=/tmp/tmpwzbih
kkt/j8n8o7ee.json', 'init=/tmp/tmpwzbihkkt/2j05b4r3.json', 'output', 'file=/tmp/t
mpwzbihkkt/prophet_modely0hlf_ef/prophet_model-20250110235210.csv', 'method=optim
ize', 'algorithm=lbfgs', 'iter=10000']
23:52:10 - cmdstanpy - INFO - Chain [1] start processing
INFO:cmdstanpy:Chain [1] start processing
23:52:10 - cmdstanpy - INFO - Chain [1] done processing
INFO:cmdstanpy:Chain [1] done processing
Out[ ]: 2204.562350863345

También podría gustarte