0% found this document useful (0 votes)
58 views13 pages

Pronosticos - Ipynb - Colaboratory

The document discusses using Python libraries like Pandas and Matplotlib to analyze time series demand data and generate forecasts. It loads demand data, plots the time series, calculates a 3-month moving average to smooth the data, and plots the actual demand versus the forecast. It then generates an initial forecast for the next time period by carrying forward the last moving average value. The goal is to demonstrate time series forecasting using a simple moving average method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views13 pages

Pronosticos - Ipynb - Colaboratory

The document discusses using Python libraries like Pandas and Matplotlib to analyze time series demand data and generate forecasts. It loads demand data, plots the time series, calculates a 3-month moving average to smooth the data, and plots the actual demand versus the forecast. It then generates an initial forecast for the next time period by carrying forward the last moving average value. The goal is to demonstrate time series forecasting using a simple moving average method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

22/9/21 19:08 pronosticos.

ipynb - Colaboratory

#Paso 1. Importación de librerias

!pip install numpy #importar vectores y  matrices. trabajo con base de datos
!pip install pandas # manejo y análisis de estructura de datos
!pip install matplotlib #gráficos en python
!pip install statsmodels #libreria que estima modelos estadísticos y realiza pruebas estadístic

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels

Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (1.19.5)

Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (1.1.5)

Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas) (2.8.2)

Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas) (2018.9)

Requirement already satisfied: numpy>=1.15.4 in /usr/local/lib/python3.7/dist-packages (from pandas) (1.19.5)


Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas
Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (3.2.2)

Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib) (2.8.2


Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib) (0.10.0)

Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (fro


Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib) (1.3.2)

Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python3.7/dist-packages (from matplotlib) (1.19.5)

Requirement
Guardando… already satisfied: six in /usr/local/lib/python3.7/dist-packages (from cycler>=0.10->matplotlib) (1.15.0)

Requirement already satisfied: statsmodels in /usr/local/lib/python3.7/dist-packages (0.10.2)

Requirement already satisfied: pandas>=0.19 in /usr/local/lib/python3.7/dist-packages (from statsmodels) (1.1.5)

Requirement already satisfied: patsy>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from statsmodels) (0.5.1)

Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python3.7/dist-packages (from statsmodels) (1.19.5)

Requirement already satisfied: scipy>=0.18 in /usr/local/lib/python3.7/dist-packages (from statsmodels) (1.4.1)

Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.19->statsmodels)


Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.19->st
Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from patsy>=0.4.0->statsmodels) (1.15.0)

#Paso 2. Importación de datos

df1 = pd.read_excel('ejemplopython.xlsx',parse_dates=["fecha"])
https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 1/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory
df1

fecha demanda

0 2018-01-01 189

1 2018-01-02 477

2 2018-01-03 255

3 2018-01-04 367

4 2018-01-05 374

... ... ...

1334 2021-08-27 164

1335 2021-08-28 386

1336 2021-08-29 444

1337 2021-08-30 194

1338 2021-08-31 170

1339 rows × 2 columns

#Paso 3. Gráfico de datos
Guardando…
plt.figure(figsize=(12,6))
plt.plot(df1.index,df1['demanda'])

plt.xlabel("Periodos",fontsize=14)
plt.ylabel("Ventas",fontsize=14)
plt.title("series de tiempo",fontsize=18)
plt.show()

https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 2/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory

#Paso 4. Manipulación de datos

df1['ano_mes']=df1['fecha'].apply(lambda x:pd.Timestamp(x).strftime('%Y-%m'))
df1

ventasmes=df1.groupby(by=['ano_mes']).sum().reset_index()
Guardando…
ventasmes

plt.figure(figsize=(12,6))
plt.plot(ventasmes.index,ventasmes['demanda'],'-o')

plt.xlabel("Periodos",fontsize=14)
plt.ylabel("Ventas",fontsize=14)
plt.title("series de tiempo",fontsize=18)
plt.show()

https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 3/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory

#Paso 5. Promedio móvil

ventasmes['prom_movil']=ventasmes['demanda'].rolling(window=3).mean().shift(1)
ventasmes

Guardando…

https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 4/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory

5 2018-06 8628 9085.333333

6 2018-07 9755 8809.333333

7 2018-08 10208 9157.333333

8 2018-09 8743 9530.333333

9 2018-10 9039 9568.666667

10 2018-11 9359 9330.000000

11 2018-12 8765 9047.000000

12 2019-01 9335 9054.333333

13 2019-02 8511 9153.000000

14 2019-03 9018 8870.333333

15 2019-04 9061 8954.666667

16 2019-05 9636 8863.333333

17 2019-06 8425 9238.333333

18 2019-07 8629 9040.666667

19 2019-08 8892 8896.666667

20 2019-09 8345 8648.666667


Guardando…
21 2019-10 8602 8622.000000

22 2019-11 9019 8613.000000

23 2019-12 9065 8655.333333

24 2020-01 9722 8895.333333

25 2020-02 9022 9268.666667

26 2020-03 10853 9269.666667

27 2020-04 11036 9865.666667

28 2020-05 10608 10303.666667


https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 5/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory

29 2020-06 9456 10832.333333

30 2020-07 9536 10366.666667

31 2020-08 10219 9866.666667

32 2020-09 8269 9737.000000

33 2020-10 9045 9341.333333

34 2020-11 8626 9177.666667

35 2020-12 8573 8646.666667

36 2021-01 8754 8748.000000

37 2021-02 9674 8651.000000

38 2021-03 9750 9000.333333

39 2021-04 10117 9392.666667

40 2021-05 8551 9847.000000

41 2021-06 8405 9472.666667

42 2021-07 8042 9024.333333


#Paso 6. Gráfico de demanda real vs pronóstico
43 2021-08 9148 8332.666667
ventasmes['prom_movil']=ventasmes['demanda'].rolling(window=3).mean().shift(1)
44 2021-09 0 8531.666667
Guardando…
ventasmes

plt.figure(figsize=(12,6))
plt.plot (ventasmes['ano_mes'][:-1],ventasmes['demanda'][:-1],color='black',label='demanda_real
plt.plot(ventasmes.index,ventasmes['prom_movil'],'-o',color='red',label='pronostico')
plt.legend(loc='best')
plt.xlabel("Mes_Año",fontsize=14)
plt.xticks(rotation=90)
plt.ylabel("Ventas",fontsize=14)
plt.title("series de tiempo",fontsize=18)
plt.show()

https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 6/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory

#Paso 7. Pronósticos próximos periodos
Guardando…
ventasmes.loc[44]=['2021-09',0,0]
ventasmes

https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 7/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory

5 2018-06 8628 9085.333333

6 2018-07 9755 8809.333333

7 2018-08 10208 9157.333333

8 2018-09 8743 9530.333333

9 2018-10 9039 9568.666667

10 2018-11 9359 9330.000000

11 2018-12 8765 9047.000000

12 2019-01 9335 9054.333333

13 2019-02 8511 9153.000000

14 2019-03 9018 8870.333333

15 2019-04 9061 8954.666667

16 2019-05 9636 8863.333333

17 2019-06 8425 9238.333333

18 2019-07 8629 9040.666667

19 2019-08 8892 8896.666667

20 2019-09 8345 8648.666667


Guardando…
21 2019-10 8602 8622.000000

22 2019-11 9019 8613.000000

23 2019-12 9065 8655.333333

24 2020-01 9722 8895.333333

25 2020-02 9022 9268.666667

26 2020-03 10853 9269.666667

27 2020-04 11036 9865.666667

28 2020-05 10608 10303.666667


https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 8/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory

29 2020-06 9456 10832.333333

30 2020-07 9536 10366.666667

31 2020-08 10219 9866.666667

32 2020-09 8269 9737.000000

33 2020-10 9045 9341.333333

34 2020-11 8626 9177.666667

35 2020-12 8573 8646.666667

36 2021-01 8754 8748.000000

37 2021-02 9674 8651.000000

38 2021-03 9750 9000.333333

39 2021-04 10117 9392.666667

40 2021-05 8551 9847.000000

41 2021-06 8405 9472.666667

42 2021-07 8042 9024.333333

43 2021-08 9148 8332.666667

44 2021-09
Guardando… 0 0.000000

#Paso 8. Construimos de nuevo los gráficos

plt.figure(figsize=(12,6))
plt.plot(ventasmes['ano_mes'][:-1],ventasmes['demanda'][:-1],'-o',color='black',label='demanda 
plt.plot(ventasmes['ano_mes'],ventasmes['prom_movil'],'-o',color='red',label='pronostico')
plt.legend(loc='best')
plt.xlabel("Mes_Año",fontsize=14)
plt.xticks(rotation=90)
plt.ylabel("Ventas",fontsize=14)
plt.title("series de tiempo",fontsize=18)
plt.show()
https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 9/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory
plt.show()

Guardando…
#Paso 9. Suavización exponencial Simple

ventasmes=df1.groupby(by=['ano_mes']).sum().reset_index()
ventasmes['prom_movil']=ventasmes['demanda'].rolling(window=3).mean().shift(1)

from statsmodels.tsa.api import SimpleExpSmoothing

mod = SimpleExpSmoothing(ventasmes['demanda']).fit(optimized =True)
#mod = SimpleExpSmoothing(ventasmes['demanda']).fit(smoothing_level = 0.3, optimized =False)

ventasmes['SES']=mod.fittedvalues
ventasmes

https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 10/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory

Guardando…

https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 11/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory

ano_mes demanda prom_movil SES

0 2018-01 7439 NaN 8261.383520

1 2018-02 8851 NaN 7866.035445

2 2018-03 9456 NaN 8339.541822

3 2018-04 8711 8582.000000 8876.261711

4 2018-05 9089 9006.000000 8796.814716

5 2018-06 8628 9085.333333 8937.278243

6 2018-07 9755 8809.333333 8788.597542

7 2018-08 10208 9157.333333 9253.180480

8 2018-09 8743 9530.333333 9712.195100

9 2018-10 9039 9568.666667 9246.269643

10 2018-11 9359 9330.000000 9146.627989

11 2018-12 8765 9047.000000 9248.722527

12 2019-01 9335 9054.333333 9016.180452

13 2019-02 8511 9153.000000 9169.447987

14 2019-03
Guardando… 9018 8870.333333 8852.909367

15 2019-04 9061 8954.666667 8932.274119

16 2019-05 9636 8863.333333 8994.157082

17 2019-06 8425 9238.333333 9302.713073

18 2019-07 8629 9040.666667 8880.766177

19 2019-08 8892 8896.666667 8759.733507

20 2019-09 8345 8648.666667 8823.318564

21 2019-10 8602 8622.000000 8593.374361


#Paso 10 Actualizamos gráficos
22 2019-11 9019 8613.000000 8597.521002
https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 12/13
22/9/21 19:08 pronosticos.ipynb - Colaboratory
22 2019 11 9019 8613.000000 8597.521002

23 2019-12 9065
plt.figure(figsize=(12,6)) 8655.333333 8800.140469
plt.plot(ventasmes['ano_mes'][:-1],ventasmes['demanda'][:-1],'-o',color='black',label='demanda 
24 2020-01 9722 8895.333333 8927.467566
plt.plot(ventasmes['ano_mes'],ventasmes['prom_movil'],'-o',color='red',label='promedio_movil')
25 2020-02 9022 9268.666667 9309.426665
plt.plot(ventasmes['ano_mes'],ventasmes['SES'],'-o',color='blue',label='SES')
plt.legend(loc='best')
26 2020-03 10853 9269.666667 9171.250770
plt.xlabel("Año",fontsize=14)
plt.xticks(rotation=90)
27 2020-04 11036 9865.666667 9979.725532
plt.ylabel("Ventas",fontsize=14)
28 2020-05 10608 10303.666667 10487.513040
plt.title("series de tiempo",fontsize=18)
plt.show()
29 2020-06 9456 10832.333333 10545.435270

30 2020-07 9536 10366.666667 10021.706223

31 2020-08 10219 9866.666667 9788.210518

32 2020-09 8269 9737.000000 9995.305855

33 2020-10 9045 9341.333333 9165.411189

34 2020-11 8626 9177.666667 9107.525384

35 2020-12 8573 8646.666667 8876.039552

36 2021-01 8754 8748.000000 8730.358005

37 2021-02 9674 8651.000000 8741.723526


Guardando…
38 2021-03 9750 9000.333333 9189.900928

https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 13/13

You might also like