Now that we have build our model, let's make future predictions. As a reminder, our plan of action was as follows:
# Only enable critical logging (Optional)
import os
os.environ["PYCARET_CUSTOM_LOGGING_LEVEL"] = "CRITICAL"
def what_is_installed():
from pycaret import show_versions
show_versions()
try:
what_is_installed()
except ModuleNotFoundError:
!pip install pycaret
what_is_installed()
System: python: 3.8.13 (default, Mar 28 2022, 06:59:08) [MSC v.1916 64 bit (AMD64)] executable: C:\Users\Nikhil\.conda\envs\pycaret_dev_sktime_0p11_2\python.exe machine: Windows-10-10.0.19044-SP0 PyCaret required dependencies:
C:\Users\Nikhil\.conda\envs\pycaret_dev_sktime_0p11_2\lib\site-packages\_distutils_hack\__init__.py:30: UserWarning: Setuptools is replacing distutils. warnings.warn("Setuptools is replacing distutils.")
pip: 21.2.2 setuptools: 61.2.0 pycaret: 3.0.0 ipython: Not installed ipywidgets: 7.7.0 numpy: 1.21.6 pandas: 1.4.2 jinja2: 3.1.2 scipy: 1.8.0 joblib: 1.1.0 sklearn: 1.0.2 pyod: Installed but version unavailable imblearn: 0.9.0 category_encoders: 2.4.1 lightgbm: 3.3.2 numba: 0.55.1 requests: 2.27.1 matplotlib: 3.5.2 scikitplot: 0.3.7 yellowbrick: 1.4 plotly: 5.8.0 kaleido: 0.2.1 statsmodels: 0.13.2 sktime: 0.11.4 tbats: Installed but version unavailable pmdarima: 1.8.5 PyCaret optional dependencies: shap: Not installed interpret: Not installed umap: Not installed pandas_profiling: Not installed explainerdashboard: Not installed autoviz: Not installed fairlearn: Not installed xgboost: Not installed catboost: Not installed kmodes: Not installed mlxtend: Not installed statsforecast: 0.5.5 tune_sklearn: Not installed ray: Not installed hyperopt: Not installed optuna: Not installed skopt: Not installed mlflow: 1.25.1 gradio: Not installed fastapi: Not installed uvicorn: Not installed m2cgen: Not installed evidently: Not installed nltk: Not installed pyLDAvis: Not installed gensim: Not installed spacy: Not installed wordcloud: Not installed textblob: Not installed psutil: 5.9.0 fugue: Not installed streamlit: Not installed prophet: Not installed
import numpy as np
import pandas as pd
from pycaret.datasets import get_data
from pycaret.time_series import TSForecastingExperiment
# Global Figure Settings for notebook ----
global_fig_settings = {"renderer": "notebook", "width": 1000, "height": 600}
data = get_data("airquality", verbose=False)
data["index"] = pd.to_datetime(data["Date"] + " " + data["Time"])
data.drop(columns=["Date", "Time"], inplace=True)
data.replace(-200, np.nan, inplace=True)
data.set_index("index", inplace=True)
exog_vars = ['NOx(GT)', 'PT08.S3(NOx)', 'RH']
data = data[exog_vars]
data.head()
NOx(GT) | PT08.S3(NOx) | RH | |
---|---|---|---|
index | |||
2004-03-10 18:00:00 | 166.0 | 1056.0 | 48.9 |
2004-03-10 19:00:00 | 103.0 | 1174.0 | 47.7 |
2004-03-10 20:00:00 | 131.0 | 1140.0 | 54.0 |
2004-03-10 21:00:00 | 172.0 | 1092.0 | 60.0 |
2004-03-10 22:00:00 | 131.0 | 1205.0 | 59.6 |
exog_exps = []
exog_models = []
for exog_var in exog_vars:
exog_exp = TSForecastingExperiment()
exog_exp.setup(
data=data[exog_var], fh=48,
numeric_imputation_target="ffill", numeric_imputation_exogenous="ffill",
fig_kwargs=global_fig_settings, session_id=42
)
# Users can customize how to model future exogenous variables i.e. add
# more steps and models to potentially get better models at the expense
# of higher modeling time.
best = exog_exp.compare_models(
sort="mase", include=["arima", "ets", "exp_smooth", "theta", "lightgbm_cds_dt",]
)
final_exog_model = exog_exp.finalize_model(best)
exog_exps.append(exog_exp)
exog_models.append(final_exog_model)
# Step 2: Get future predictions for exog variables ----
future_exog = [
exog_exp.predict_model(exog_model)
for exog_exp, exog_model in zip(exog_exps, exog_models)
]
future_exog = pd.concat(future_exog, axis=1)
future_exog.columns = exog_vars
Model | MASE | RMSSE | MAE | RMSE | MAPE | SMAPE | R2 | TT (Sec) | |
---|---|---|---|---|---|---|---|---|---|
lightgbm_cds_dt | Light Gradient Boosting w/ Cond. Deseasonalize & Detrending | 1.1990 | 1.0486 | 11.0836 | 13.5879 | 0.2862 | 0.2304 | -0.9502 | 5.1700 |
exp_smooth | Exponential Smoothing | 1.4890 | 1.1904 | 13.7510 | 15.4140 | 0.3037 | 0.2478 | -0.4248 | 3.2700 |
arima | ARIMA | 1.5657 | 1.3693 | 14.4696 | 17.7412 | 0.3539 | 0.2739 | -2.1191 | 3.6833 |
theta | Theta Forecaster | 1.9102 | 1.4902 | 17.6420 | 19.2958 | 0.3713 | 0.2986 | -1.2629 | 3.1567 |
ets | ETS | 3.4504 | 2.6557 | 31.8652 | 34.3839 | 0.6295 | 0.4585 | -6.1733 | 9.8033 |
future_exog
NOx(GT) | PT08.S3(NOx) | RH | |
---|---|---|---|
2005-04-04 15:00 | 262.1091 | 656.1044 | 14.5065 |
2005-04-04 16:00 | 297.1918 | 638.6659 | 15.6333 |
2005-04-04 17:00 | 325.5872 | 596.7227 | 17.5673 |
2005-04-04 18:00 | 259.7444 | 556.0672 | 22.7364 |
2005-04-04 19:00 | 425.2039 | 534.2108 | 28.3385 |
2005-04-04 20:00 | 403.5829 | 553.1982 | 33.7580 |
2005-04-04 21:00 | 275.5622 | 611.5993 | 37.0870 |
2005-04-04 22:00 | 266.8754 | 652.8857 | 39.7600 |
2005-04-04 23:00 | 198.3008 | 662.6246 | 41.3155 |
2005-04-05 00:00 | 140.6534 | 680.4221 | 42.4415 |
2005-04-05 01:00 | 89.7790 | 730.9550 | 44.4348 |
2005-04-05 02:00 | 85.5490 | 799.7945 | 46.8401 |
2005-04-05 03:00 | 75.8562 | 861.3735 | 48.6809 |
2005-04-05 04:00 | 63.6113 | 890.8526 | 49.7074 |
2005-04-05 05:00 | 86.7398 | 878.5921 | 50.1808 |
2005-04-05 06:00 | 190.1795 | 796.5090 | 50.4606 |
2005-04-05 07:00 | 600.8788 | 663.6406 | 48.9828 |
2005-04-05 08:00 | 591.7944 | 561.3295 | 46.7280 |
2005-04-05 09:00 | 527.8903 | 565.2039 | 41.8545 |
2005-04-05 10:00 | 476.1367 | 599.8656 | 37.9259 |
2005-04-05 11:00 | 356.5084 | 622.0729 | 35.3302 |
2005-04-05 12:00 | 295.9845 | 630.0078 | 34.0590 |
2005-04-05 13:00 | 237.5478 | 632.9998 | 35.7544 |
2005-04-05 14:00 | 267.1838 | 653.6938 | 36.6735 |
2005-04-05 15:00 | 263.9894 | 655.8095 | 35.8249 |
2005-04-05 16:00 | 298.8190 | 638.3788 | 35.1843 |
2005-04-05 17:00 | 327.0035 | 596.4545 | 35.7987 |
2005-04-05 18:00 | 260.9848 | 555.8173 | 38.7220 |
2005-04-05 19:00 | 426.2978 | 533.9706 | 41.7648 |
2005-04-05 20:00 | 404.5546 | 552.9496 | 44.0184 |
2005-04-05 21:00 | 276.4319 | 611.3244 | 47.1953 |
2005-04-05 22:00 | 267.6602 | 652.5922 | 49.4721 |
2005-04-05 23:00 | 199.0148 | 662.3268 | 51.0524 |
2005-04-06 00:00 | 141.3084 | 680.1162 | 53.1152 |
2005-04-06 01:00 | 90.3847 | 730.6264 | 54.2431 |
2005-04-06 02:00 | 86.1137 | 799.4349 | 55.4766 |
2005-04-06 03:00 | 76.3867 | 860.9863 | 56.8362 |
2005-04-06 04:00 | 64.1132 | 890.4521 | 58.0996 |
2005-04-06 05:00 | 87.2179 | 878.1971 | 59.2240 |
2005-04-06 06:00 | 190.6379 | 796.1510 | 59.8861 |
2005-04-06 07:00 | 601.3206 | 663.3422 | 59.1575 |
2005-04-06 08:00 | 592.2224 | 561.0771 | 57.2327 |
2005-04-06 09:00 | 528.3069 | 564.9498 | 52.8706 |
2005-04-06 10:00 | 476.5436 | 599.5958 | 46.1781 |
2005-04-06 11:00 | 356.9073 | 621.7932 | 41.6050 |
2005-04-06 12:00 | 296.3769 | 629.7245 | 38.9799 |
2005-04-06 13:00 | 237.9346 | 632.7152 | 37.8304 |
2005-04-06 14:00 | 267.5660 | 653.3999 | 37.2366 |
exp_future = TSForecastingExperiment()
final_slim_model = exp_future.load_model("final_slim_model")
Transformation Pipeline and Model Successfully Loaded
future_preds = exp_future.predict_model(final_slim_model, X=future_exog)
future_preds.plot()
<AxesSubplot:>