Pronosticos - Ipynb - Colaboratory
Pronosticos - Ipynb - Colaboratory
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
Guardando… already satisfied: six in /usr/local/lib/python3.7/dist-packages (from cycler>=0.10->matplotlib) (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
#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
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
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
14 2019-03
Guardando… 9018 8870.333333 8852.909367
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
https://colab.research.google.com/drive/1e6ZZbg3iBg0Qc3bV1thiRIe083VhmBMz#scrollTo=tTCwjk6AAoTq&printMode=true 13/13