index.ipynb 6.61 KB
Newer Older
chenzk's avatar
v1.0  
chenzk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30ebf8f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "43832dcf-0452-4503-aace-8f38e5e23723",
   "metadata": {},
   "source": [
    "# 🧠 NeuralForecast\n",
    "\n",
    "> **NeuralForecast** offers a large collection of neural forecasting models focused on their usability, and robustness. The models range from classic networks like `MLP`, `RNN`s to novel proven contributions like `NBEATS`, `NHITS`, `TFT` and other architectures."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9bdd2aa5",
   "metadata": {},
   "source": [
    "## 🎊 Features\n",
    "\n",
    "* **Exogenous Variables**: Static, historic and future exogenous support.\n",
    "* **Forecast Interpretability**: Plot trend, seasonality and exogenous `NBEATS`, `NHITS`, `TFT`, `ESRNN` prediction components.\n",
    "* **Probabilistic Forecasting**: Simple model adapters for quantile losses and parametric distributions.\n",
    "* **Train and Evaluation Losses** Scale-dependent, percentage and scale independent errors, and parametric likelihoods.\n",
    "* **Automatic Model Selection** Parallelized automatic hyperparameter tuning, that efficiently searches best validation configuration.\n",
    "* **Simple Interface** Unified SKLearn Interface for `StatsForecast` and `MLForecast` compatibility.\n",
    "* **Model Collection**: Out of the box implementation of `MLP`, `LSTM`, `RNN`, `TCN`, `DilatedRNN`, `NBEATS`, `NHITS`, `ESRNN`, `Informer`, `TFT`, `PatchTST`, `VanillaTransformer`, `StemGNN` and `HINT`. See the entire [collection here](https://nixtla.github.io/neuralforecast/models.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cebf377",
   "metadata": {},
   "source": [
    "## Why?\n",
    "\n",
    "There is a shared belief in Neural forecasting methods' capacity to improve our pipeline's accuracy and efficiency.\n",
    "\n",
    "Unfortunately, available implementations and published research are yet to realize neural networks' potential. They are hard to use and continuously fail to improve over statistical methods while being computationally prohibitive. For this reason, we created `NeuralForecast`, a library favoring proven accurate and efficient models focusing on their usability."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50d28044",
   "metadata": {},
   "source": [
    "## 💻 Installation\n",
    "\n",
    "\n",
    "### PyPI\n",
    "\n",
    "You can install `NeuralForecast`'s *released version* from the Python package index [pip](https://pypi.org/project/neuralforecast/) with:\n",
    "\n",
    "```python\n",
    "pip install neuralforecast\n",
    "```\n",
    "\n",
    "(Installing inside a python virtualenvironment or a conda environment is recommended.)\n",
    "\n",
    "\n",
    "### Conda\n",
    "\n",
    "Also you can install `NeuralForecast`'s *released version* from [conda](https://anaconda.org/conda-forge/neuralforecast) with:\n",
    "\n",
    "```python\n",
    "conda install -c conda-forge neuralforecast\n",
    "```\n",
    "\n",
    "(Installing inside a python virtualenvironment or a conda environment is recommended.)\n",
    "\n",
    "### Dev Mode\n",
    "If you want to make some modifications to the code and see the effects in real time (without reinstalling), follow the steps below:\n",
    "\n",
    "```bash\n",
    "git clone https://github.com/Nixtla/neuralforecast.git\n",
    "cd neuralforecast\n",
    "pip install -e .\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3aeb537",
   "metadata": {},
   "source": [
    "## How to Use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "401a785d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| eval: false\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from IPython.display import display, Markdown\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from neuralforecast import NeuralForecast\n",
    "from neuralforecast.models import NBEATS, NHITS\n",
    "from neuralforecast.utils import AirPassengersDF\n",
    "\n",
    "# Split data and declare panel dataset\n",
    "Y_df = AirPassengersDF\n",
    "Y_train_df = Y_df[Y_df.ds<='1959-12-31'] # 132 train\n",
    "Y_test_df = Y_df[Y_df.ds>'1959-12-31'] # 12 test\n",
    "\n",
    "# Fit and predict with NBEATS and NHITS models\n",
    "horizon = len(Y_test_df)\n",
    "models = [NBEATS(input_size=2 * horizon, h=horizon, max_steps=50),\n",
    "          NHITS(input_size=2 * horizon, h=horizon, max_steps=50)]\n",
    "nf = NeuralForecast(models=models, freq='M')\n",
    "nf.fit(df=Y_train_df)\n",
    "Y_hat_df = nf.predict().reset_index()\n",
    "\n",
    "# Plot predictions\n",
    "fig, ax = plt.subplots(1, 1, figsize = (20, 7))\n",
    "Y_hat_df = Y_test_df.merge(Y_hat_df, how='left', on=['unique_id', 'ds'])\n",
    "plot_df = pd.concat([Y_train_df, Y_hat_df]).set_index('ds')\n",
    "\n",
    "plot_df[['y', 'NBEATS', 'NHITS']].plot(ax=ax, linewidth=2)\n",
    "\n",
    "ax.set_title('AirPassengers Forecast', fontsize=22)\n",
    "ax.set_ylabel('Monthly Passengers', fontsize=20)\n",
    "ax.set_xlabel('Timestamp [t]', fontsize=20)\n",
    "ax.legend(prop={'size': 15})\n",
    "ax.grid()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "7cb3919a",
   "metadata": {},
   "source": [
    "## 🙏 How to Cite"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2c669ec7",
   "metadata": {},
   "source": [
    "If you enjoy or benefit from using these Python implementations, a citation to the repository will be greatly appreciated."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0fed17d7",
   "metadata": {},
   "source": [
    "```\n",
    "@misc{olivares2022library_neuralforecast,\n",
    "    author={Kin G. Olivares and\n",
    "            Cristian Challú and\n",
    "            Federico Garza and\n",
    "            Max Mergenthaler Canseco and\n",
    "            Artur Dubrawski},\n",
    "    title = {{NeuralForecast}: User friendly state-of-the-art neural forecasting models.},\n",
    "    year={2022},\n",
    "    howpublished={{PyCon} Salt Lake City, Utah, US 2022},\n",
    "    url={https://github.com/Nixtla/neuralforecast}\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "617b9dca",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}