Pronóstico: Leer Datos

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

1/8/2021 Untitled1.

ipynb - Colaboratory

Pronóstico

import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
from fbprophet import Prophet

Leer Datos

df=pd.read_csv("./Datos4.csv")
df.head()
 

Fecha Datos

0 01-01-2021 6

1 02-01-2021 31

2 03-01-2021 4

3 04-01-2021 143

4 05-01-2021 137

Crear Datos de Test y de Entrenamiento

p_train=0.8
 
train = df[:int((len(df))*p_train)] 
test = df[int((len(df))*p_train):]

df_comp=train.copy()

df_comp.head()

https://colab.research.google.com/drive/1RH1BvQGwhJY1qzwwKWqhaVDfSpMsxkhb#scrollTo=ns2nesm5n9mS&printMode=true 1/5
1/8/2021 Untitled1.ipynb - Colaboratory

Fecha Datos

0 01-01-2021 6

1 02-01-2021 31

2 03-01-2021 4

3 04-01-2021 143

4 05-01-2021 137
df_comp.Fecha=pd.to_datetime(df_comp.Fecha, dayfirst= True)
df_comp.set_index("Fecha",inplace=True)

df_comp.head()

Datos

Fecha

2021-01-01 6

2021-01-02 31

2021-01-03 4

2021-01-04 143

2021-01-05 137

df.Datos.plot(figsize=(10,5))
plt.title("Datos", size=24)
plt.show()

  !pip install pmdarima
https://colab.research.google.com/drive/1RH1BvQGwhJY1qzwwKWqhaVDfSpMsxkhb#scrollTo=ns2nesm5n9mS&printMode=true 2/5
1/8/2021 Untitled1.ipynb - Colaboratory
p p p
  from pmdarima.arima import auto_arima

Collecting pmdarima

Downloading pmdarima-1.8.2-cp37-cp37m-manylinux1_x86_64.whl (1.5 MB)

|████████████████████████████████| 1.5 MB 4.8 MB/s

Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (


Requirement already satisfied: numpy~=1.19.0 in /usr/local/lib/python3.7/dist-packages
Requirement already satisfied: setuptools!=50.0.0,>=38.6.0 in /usr/local/lib/python3.7/
Requirement already satisfied: scipy>=1.3.2 in /usr/local/lib/python3.7/dist-packages (
Requirement already satisfied: urllib3 in /usr/local/lib/python3.7/dist-packages (from
Requirement already satisfied: Cython!=0.29.18,>=0.29 in /usr/local/lib/python3.7/dist-
Requirement already satisfied: scikit-learn>=0.22 in /usr/local/lib/python3.7/dist-pack
Requirement already satisfied: pandas>=0.19 in /usr/local/lib/python3.7/dist-packages (
Collecting statsmodels!=0.12.0,>=0.11

Downloading statsmodels-0.12.2-cp37-cp37m-manylinux1_x86_64.whl (9.5 MB)

|████████████████████████████████| 9.5 MB 19.9 MB/s

Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-


Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from
Requirement already satisfied: patsy>=0.5 in /usr/local/lib/python3.7/dist-packages (fr
Installing collected packages: statsmodels, pmdarima

Attempting uninstall: statsmodels

Found existing installation: statsmodels 0.10.2

Uninstalling statsmodels-0.10.2:

Successfully uninstalled statsmodels-0.10.2

Successfully installed pmdarima-1.8.2 statsmodels-0.12.2

WARNING: The following packages were previously imported in this runtime:

[statsmodels]

You must restart the runtime in order to use newly installed versions.

RESTART RUNTIME

model_auto=auto_arima(df_comp.Datos, m=5,
                      max_order= None, max_p=7, max_q=7, max_d=2, max_P=4, max_Q=4, max_D=2, 
                      maxiter=50, alpha=0.05, information_criterion='aic')

model_auto

ARIMA(maxiter=50, method='lbfgs', order=(0, 0, 1), out_of_sample_size=0,

scoring='mse', scoring_args={}, seasonal_order=(0, 0, 1, 5),

start_params=None, suppress_warnings=True, trend=None,

with_intercept=True)

model_auto.summary()

https://colab.research.google.com/drive/1RH1BvQGwhJY1qzwwKWqhaVDfSpMsxkhb#scrollTo=ns2nesm5n9mS&printMode=true 3/5
1/8/2021 Untitled1.ipynb - Colaboratory

SARIMAX Results
Dep. Variable: y No. Observations: 167
Model: SARIMAX(0, 0, 1)x(0, 0, 1, 5) Log Likelihood -841.427
Date: Sun, 01 Aug 2021 AIC 1690.855
Time: 16:25:43 BIC 1703.327
Sample: 0 HQIC 1695.917
- 167
Covariance Type: opg
coef std err z P>|z| [0.025 0.975]
intercept 74.5716 1.797 41.499 0.000 71.050 78.094
ma.L1 0.2953 0.094 3.126 0.002 0.110 0.480
ma.S.L5 -0.5747 0.074 -7.756 0.000 -0.720 -0.429
sigma2 1375.4178 175.577 7.834 0.000 1031.293 1719.543
Ljung-Box (L1) (Q): 0.02 Jarque-Bera (JB): 5.70
Prob(Q): 0.89 Prob(JB): 0.06
Heteroskedasticity (H): 1.02
date=pd.to_datetime("06-17-2021") Skew: 0.44
date Prob(H) (two-sided): 0.93 Kurtosis: 3.21

Timestamp('2021-06-17 00:00:00')
Warnings:

[1] Covariance matrix calculated using the outer product of gradients (complex-step).
i_date=date+pd.to_timedelta(np.arange(10),'D')
i_date

DatetimeIndex(['2021-06-17', '2021-06-18', '2021-06-19', '2021-06-20',

'2021-06-21', '2021-06-22', '2021-06-23', '2021-06-24',

'2021-06-25', '2021-06-26'],

dtype='datetime64[ns]', freq=None)

pronostico=model_auto.predict(len(i_date))

df_test=pd.Series(pronostico,index=i_date)
df_test.head()

2021-06-17 93.366008

2021-06-18 114.026628

2021-06-19 47.533056

2021-06-20 55.214624

2021-06-21 66.332862

dtype: float64

df_auto_pred=pd.DataFrame({'Pronosticos': pronostico},index=i_date)

df_auto_pred.plot(figsize=(10,5),color="red")
df_comp.Datos.plot(color="blue")
plt.title("Pronostico vs Real", size=24)
plt.show()

https://colab.research.google.com/drive/1RH1BvQGwhJY1qzwwKWqhaVDfSpMsxkhb#scrollTo=ns2nesm5n9mS&printMode=true 4/5
1/8/2021 Untitled1.ipynb - Colaboratory

https://colab.research.google.com/drive/1RH1BvQGwhJY1qzwwKWqhaVDfSpMsxkhb#scrollTo=ns2nesm5n9mS&printMode=true 5/5

You might also like