Last active
November 3, 2022 14:28
-
-
Save ngupta23/501b7459f2f73502a766a60814b6fbf8 to your computer and use it in GitHub Desktop.
sktime_prophet_insample.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/ngupta23/501b7459f2f73502a766a60814b6fbf8/sktime_prophet_insample.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "oPZFmtlA8yl9", | |
"outputId": "c7f2810f-4254-472c-dee5-392796f2340c" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\n", | |
"System:\n", | |
" python: 3.7.15 (default, Oct 12 2022, 19:14:55) [GCC 7.5.0]\n", | |
"executable: /usr/bin/python3\n", | |
" machine: Linux-5.10.133+-x86_64-with-Ubuntu-18.04-bionic\n", | |
"\n", | |
"Python dependencies:\n", | |
" pip: 21.1.3\n", | |
" setuptools: 57.4.0\n", | |
" sklearn: 1.0.2\n", | |
" sktime: 0.13.4\n", | |
" statsmodels: 0.12.2\n", | |
" numpy: 1.21.6\n", | |
" scipy: 1.7.3\n", | |
" pandas: 1.3.5\n", | |
" matplotlib: 3.2.2\n", | |
" joblib: 1.2.0\n", | |
" numba: 0.56.3\n", | |
" pmdarima: None\n", | |
" tsfresh: None\n" | |
] | |
} | |
], | |
"source": [ | |
"try:\n", | |
" from sktime import show_versions\n", | |
"except ModuleNotFoundError:\n", | |
" !pip install sktime\n", | |
"\n", | |
"try:\n", | |
" import prophet\n", | |
"except ModuleNotFoundError:\n", | |
" !pip install prophet\n", | |
"\n", | |
"show_versions()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from sktime.datasets import load_longley\n", | |
"\n", | |
"original_data = load_longley()[1].merge(\n", | |
" load_longley()[0].rename(\"TOTEMP\"),\n", | |
" left_index=True,\n", | |
" right_index=True\n", | |
")\n", | |
"# Prophet requires to have data with a pandas.DatetimeIndex\n", | |
"original_data_p = original_data.to_timestamp(freq=\"Y\")" | |
], | |
"metadata": { | |
"id": "AI5eN6qW9Cns" | |
}, | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"y = original_data_p[\"TOTEMP\"]\n", | |
"X = original_data_p.drop(columns=[\"TOTEMP\"])\n", | |
"original_data_p.shape, X.shape, y.shape" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "P3EcbjZk2Gra", | |
"outputId": "e7e78ddd-6300-44e9-ede5-1e3ff8515004" | |
}, | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"((16, 6), (16, 5), (16,))" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 6 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from sktime.forecasting.fbprophet import Prophet\n", | |
"\n", | |
"forecaster = Prophet()\n", | |
"forecaster.fit(y=y, X=X, fh=[1, 2, 3])" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "mWtWCW8K9MlP", | |
"outputId": "043f24ba-9968-4f42-dae2-0463764bff73" | |
}, | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stderr", | |
"text": [ | |
"INFO:prophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.\n", | |
"INFO:prophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.\n", | |
"INFO:prophet:n_changepoints greater than number of observations. Using 11.\n", | |
"DEBUG:cmdstanpy:input tempfile: /tmp/tmpmyzh2i0u/ujkr3zgp.json\n", | |
"DEBUG:cmdstanpy:input tempfile: /tmp/tmpmyzh2i0u/ogt09je1.json\n", | |
"DEBUG:cmdstanpy:idx 0\n", | |
"DEBUG:cmdstanpy:running CmdStan, num_threads: None\n", | |
"DEBUG:cmdstanpy:CmdStan args: ['/usr/local/lib/python3.7/dist-packages/prophet/stan_model/prophet_model.bin', 'random', 'seed=18197', 'data', 'file=/tmp/tmpmyzh2i0u/ujkr3zgp.json', 'init=/tmp/tmpmyzh2i0u/ogt09je1.json', 'output', 'file=/tmp/tmpmyzh2i0u/prophet_model9kgh1iso/prophet_model-20221103141617.csv', 'method=optimize', 'algorithm=newton', 'iter=10000']\n", | |
"14:16:17 - cmdstanpy - INFO - Chain [1] start processing\n", | |
"INFO:cmdstanpy:Chain [1] start processing\n", | |
"14:16:17 - cmdstanpy - INFO - Chain [1] done processing\n", | |
"INFO:cmdstanpy:Chain [1] done processing\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"Prophet()" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 7 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Insample\n", | |
"y_pred = forecaster.predict(X=X)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 328 | |
}, | |
"id": "vsthg9z19R92", | |
"outputId": "4f7735a7-d168-40f8-db8b-f62f9e2eebd9" | |
}, | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "ValueError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-8-b99a87e0cd4c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Insample\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0my_pred\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mforecaster\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/sktime/forecasting/base/_base.py\u001b[0m in \u001b[0;36mpredict\u001b[0;34m(self, fh, X)\u001b[0m\n\u001b[1;32m 356\u001b[0m \u001b[0;31m# we call the ordinary _predict if no looping/vectorization needed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 357\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_is_vectorized\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 358\u001b[0;31m \u001b[0my_pred\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_predict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfh\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfh\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mX\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mX_inner\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 359\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 360\u001b[0m \u001b[0;31m# otherwise we call the vectorized version of predict\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/sktime/forecasting/base/adapters/_fbprophet.py\u001b[0m in \u001b[0;36m_predict\u001b[0;34m(self, fh, X)\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"floor\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgrowth_floor\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 164\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 165\u001b[0;31m \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_forecaster\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 166\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_index\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"ds\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minplace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/prophet/forecaster.py\u001b[0m in \u001b[0;36mpredict\u001b[0;34m(self, df, vectorized)\u001b[0m\n\u001b[1;32m 1209\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1210\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1211\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Dataframe has no rows.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1212\u001b[0m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetup_dataframe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1213\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mValueError\u001b[0m: Dataframe has no rows." | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "W9Ojxhsx9TaO" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment