Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add level argument to forecast_fitted_values #287

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ dependencies:
- polars
- ray<2.8
- triad==0.9.1
- utilsforecast>=0.0.21
- utilsforecast>=0.0.22
- xgboost_ray
2 changes: 1 addition & 1 deletion local_environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ dependencies:
- datasetsforecast
- nbdev
- polars
- utilsforecast>=0.0.21
- utilsforecast>=0.0.22
28 changes: 25 additions & 3 deletions mlforecast/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,33 @@ def fit(
self.fcst_fitted_values_ = fitted_values
return self

def forecast_fitted_values(self):
"""Access in-sample predictions."""
def forecast_fitted_values(
self, level: Optional[List[Union[int, float]]] = None
) -> DataFrame:
"""Access in-sample predictions.

Parameters
----------
level : list of ints or floats, optional (default=None)
Confidence levels between 0 and 100 for prediction intervals.

Returns
-------
pandas or polars DataFrame
Dataframe with predictions for the training set
"""
if not hasattr(self, "fcst_fitted_values_"):
raise Exception("Please run the `fit` method using `fitted=True`")
return self.fcst_fitted_values_
res = self.fcst_fitted_values_
if level is not None:
res = ufp.add_insample_levels(
res,
models=self.models_.keys(),
level=level,
id_col=self.ts.id_col,
target_col=self.ts.target_col,
)
return res

def make_future_dataframe(self, h: int) -> DataFrame:
"""Create a dataframe with all ids and future times in the forecasting horizon.
Expand Down
239 changes: 230 additions & 9 deletions nbs/forecast.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,31 @@
" self.fcst_fitted_values_ = fitted_values\n",
" return self\n",
"\n",
" def forecast_fitted_values(self):\n",
" \"\"\"Access in-sample predictions.\"\"\"\n",
" def forecast_fitted_values(self, level: Optional[List[Union[int, float]]] = None) -> DataFrame:\n",
" \"\"\"Access in-sample predictions.\n",
" \n",
" Parameters\n",
" ----------\n",
" level : list of ints or floats, optional (default=None)\n",
" Confidence levels between 0 and 100 for prediction intervals.\n",
"\n",
" Returns\n",
" -------\n",
" pandas or polars DataFrame\n",
" Dataframe with predictions for the training set\n",
" \"\"\"\n",
" if not hasattr(self, 'fcst_fitted_values_'):\n",
" raise Exception('Please run the `fit` method using `fitted=True`')\n",
" return self.fcst_fitted_values_\n",
" res = self.fcst_fitted_values_\n",
" if level is not None:\n",
" res = ufp.add_insample_levels(\n",
" res,\n",
" models=self.models_.keys(),\n",
" level=level,\n",
" id_col=self.ts.id_col,\n",
" target_col=self.ts.target_col,\n",
" )\n",
" return res\n",
"\n",
" def make_future_dataframe(self, h: int) -> DataFrame:\n",
" \"\"\"Create a dataframe with all ids and future times in the forecasting horizon.\n",
Expand Down Expand Up @@ -1456,6 +1476,8 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/Nixtla/mlforecast/blob/main/mlforecast/forecast.py#L542){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### MLForecast.make_future_dataframe\n",
"\n",
"> MLForecast.make_future_dataframe (h:int)\n",
Expand All @@ -1470,6 +1492,8 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/Nixtla/mlforecast/blob/main/mlforecast/forecast.py#L542){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### MLForecast.make_future_dataframe\n",
"\n",
"> MLForecast.make_future_dataframe (h:int)\n",
Expand Down Expand Up @@ -1576,6 +1600,8 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/Nixtla/mlforecast/blob/main/mlforecast/forecast.py#L566){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### MLForecast.get_missing_future\n",
"\n",
"> MLForecast.get_missing_future (h:int,\n",
Expand All @@ -1593,6 +1619,8 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/Nixtla/mlforecast/blob/main/mlforecast/forecast.py#L566){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### MLForecast.get_missing_future\n",
"\n",
"> MLForecast.get_missing_future (h:int,\n",
Expand Down Expand Up @@ -1642,24 +1670,38 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/Nixtla/mlforecast/blob/main/mlforecast/forecast.py#L548){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/Nixtla/mlforecast/blob/main/mlforecast/forecast.py#L536){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### MLForecast.forecast_fitted_values\n",
"\n",
"> MLForecast.forecast_fitted_values ()\n",
"> MLForecast.forecast_fitted_values\n",
"> (level:Optional[List[Union[int,float]]\n",
"> ]=None)\n",
"\n",
"Access in-sample predictions."
"Access in-sample predictions.\n",
"\n",
"| | **Type** | **Default** | **Details** |\n",
"| -- | -------- | ----------- | ----------- |\n",
"| level | Optional | None | Confidence levels between 0 and 100 for prediction intervals. |\n",
"| **Returns** | **Union** | | **Dataframe with predictions for the training set** |"
],
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/Nixtla/mlforecast/blob/main/mlforecast/forecast.py#L548){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/Nixtla/mlforecast/blob/main/mlforecast/forecast.py#L536){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### MLForecast.forecast_fitted_values\n",
"\n",
"> MLForecast.forecast_fitted_values ()\n",
"> MLForecast.forecast_fitted_values\n",
"> (level:Optional[List[Union[int,float]]\n",
"> ]=None)\n",
"\n",
"Access in-sample predictions.\n",
"\n",
"Access in-sample predictions."
"| | **Type** | **Default** | **Details** |\n",
"| -- | -------- | ----------- | ----------- |\n",
"| level | Optional | None | Confidence levels between 0 and 100 for prediction intervals. |\n",
"| **Returns** | **Union** | | **Dataframe with predictions for the training set** |"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1813,6 +1855,185 @@
"fcst.forecast_fitted_values()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9e84d1e-5c92-4d3c-99e8-e9d897ac0f6f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>unique_id</th>\n",
" <th>ds</th>\n",
" <th>y</th>\n",
" <th>LGBMRegressor</th>\n",
" <th>LGBMRegressor-lo-90</th>\n",
" <th>LGBMRegressor-hi-90</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>H196</td>\n",
" <td>193</td>\n",
" <td>12.7</td>\n",
" <td>12.671271</td>\n",
" <td>12.540634</td>\n",
" <td>12.801909</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>H196</td>\n",
" <td>194</td>\n",
" <td>12.3</td>\n",
" <td>12.271271</td>\n",
" <td>12.140634</td>\n",
" <td>12.401909</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>H196</td>\n",
" <td>195</td>\n",
" <td>11.9</td>\n",
" <td>11.871271</td>\n",
" <td>11.740634</td>\n",
" <td>12.001909</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>H196</td>\n",
" <td>196</td>\n",
" <td>11.7</td>\n",
" <td>11.671271</td>\n",
" <td>11.540634</td>\n",
" <td>11.801909</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>H196</td>\n",
" <td>197</td>\n",
" <td>11.4</td>\n",
" <td>11.471271</td>\n",
" <td>11.340634</td>\n",
" <td>11.601909</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3067</th>\n",
" <td>H413</td>\n",
" <td>956</td>\n",
" <td>59.0</td>\n",
" <td>68.280574</td>\n",
" <td>58.846640</td>\n",
" <td>77.714509</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3068</th>\n",
" <td>H413</td>\n",
" <td>957</td>\n",
" <td>58.0</td>\n",
" <td>70.427570</td>\n",
" <td>60.993636</td>\n",
" <td>79.861504</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3069</th>\n",
" <td>H413</td>\n",
" <td>958</td>\n",
" <td>53.0</td>\n",
" <td>44.767965</td>\n",
" <td>35.334031</td>\n",
" <td>54.201899</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3070</th>\n",
" <td>H413</td>\n",
" <td>959</td>\n",
" <td>38.0</td>\n",
" <td>48.691257</td>\n",
" <td>39.257323</td>\n",
" <td>58.125191</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3071</th>\n",
" <td>H413</td>\n",
" <td>960</td>\n",
" <td>46.0</td>\n",
" <td>46.652238</td>\n",
" <td>37.218304</td>\n",
" <td>56.086172</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>3072 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" unique_id ds y LGBMRegressor LGBMRegressor-lo-90 \\\n",
"0 H196 193 12.7 12.671271 12.540634 \n",
"1 H196 194 12.3 12.271271 12.140634 \n",
"2 H196 195 11.9 11.871271 11.740634 \n",
"3 H196 196 11.7 11.671271 11.540634 \n",
"4 H196 197 11.4 11.471271 11.340634 \n",
"... ... ... ... ... ... \n",
"3067 H413 956 59.0 68.280574 58.846640 \n",
"3068 H413 957 58.0 70.427570 60.993636 \n",
"3069 H413 958 53.0 44.767965 35.334031 \n",
"3070 H413 959 38.0 48.691257 39.257323 \n",
"3071 H413 960 46.0 46.652238 37.218304 \n",
"\n",
" LGBMRegressor-hi-90 \n",
"0 12.801909 \n",
"1 12.401909 \n",
"2 12.001909 \n",
"3 11.801909 \n",
"4 11.601909 \n",
"... ... \n",
"3067 77.714509 \n",
"3068 79.861504 \n",
"3069 54.201899 \n",
"3070 58.125191 \n",
"3071 56.086172 \n",
"\n",
"[3072 rows x 6 columns]"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fcst.forecast_fitted_values(level=[90])"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ language = English
custom_sidebar = True
license = apache2
status = 3
requirements = numba packaging pandas scikit-learn utilsforecast>=0.0.21 window-ops
requirements = numba packaging pandas scikit-learn utilsforecast>=0.0.22 window-ops
dask_requirements = fugue dask[complete] lightgbm xgboost
ray_requirements = fugue[ray] lightgbm_ray xgboost_ray
spark_requirements = fugue pyspark lightgbm xgboost
Expand Down
Loading