utilsforecast


Nameutilsforecast JSON
Version 0.2.9 PyPI version JSON
download
home_pagehttps://github.com/Nixtla/utilsforecast
SummaryForecasting utilities
upload_time2024-11-22 17:28:33
maintainerNone
docs_urlNone
authorNixtla
requires_python>=3.8
licenseApache Software License 2.0
keywords time-series analysis forecasting
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            utilsforecast
================

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Install

### PyPI

``` sh
pip install utilsforecast
```

### Conda

``` sh
conda install -c conda-forge utilsforecast
```

## How to use

### Generate synthetic data

``` python
from utilsforecast.data import generate_series
```

``` python
series = generate_series(3, with_trend=True, static_as_categorical=False)
series
```

<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>unique_id</th>
      <th>ds</th>
      <th>y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0</td>
      <td>2000-01-01</td>
      <td>0.422133</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0</td>
      <td>2000-01-02</td>
      <td>1.501407</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0</td>
      <td>2000-01-03</td>
      <td>2.568495</td>
    </tr>
    <tr>
      <th>3</th>
      <td>0</td>
      <td>2000-01-04</td>
      <td>3.529085</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0</td>
      <td>2000-01-05</td>
      <td>4.481929</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>481</th>
      <td>2</td>
      <td>2000-06-11</td>
      <td>163.914625</td>
    </tr>
    <tr>
      <th>482</th>
      <td>2</td>
      <td>2000-06-12</td>
      <td>166.018479</td>
    </tr>
    <tr>
      <th>483</th>
      <td>2</td>
      <td>2000-06-13</td>
      <td>160.839176</td>
    </tr>
    <tr>
      <th>484</th>
      <td>2</td>
      <td>2000-06-14</td>
      <td>162.679603</td>
    </tr>
    <tr>
      <th>485</th>
      <td>2</td>
      <td>2000-06-15</td>
      <td>165.089288</td>
    </tr>
  </tbody>
</table>
<p>486 rows × 3 columns</p>
</div>

### Plotting

``` python
from utilsforecast.plotting import plot_series
```

``` python
fig = plot_series(series, plot_random=False, max_insample_length=50, engine='matplotlib')
fig.savefig('imgs/index.png', bbox_inches='tight')
```

![](https://raw.githubusercontent.com/Nixtla/utilsforecast/main/nbs/imgs/index.png)

### Preprocessing

``` python
from utilsforecast.preprocessing import fill_gaps
```

``` python
serie = series[series['unique_id'].eq(0)].tail(10)
# drop some points
with_gaps = serie.sample(frac=0.5, random_state=0).sort_values('ds')
with_gaps
```

<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>unique_id</th>
      <th>ds</th>
      <th>y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>213</th>
      <td>0</td>
      <td>2000-08-01</td>
      <td>18.543147</td>
    </tr>
    <tr>
      <th>214</th>
      <td>0</td>
      <td>2000-08-02</td>
      <td>19.941764</td>
    </tr>
    <tr>
      <th>216</th>
      <td>0</td>
      <td>2000-08-04</td>
      <td>21.968733</td>
    </tr>
    <tr>
      <th>220</th>
      <td>0</td>
      <td>2000-08-08</td>
      <td>19.091509</td>
    </tr>
    <tr>
      <th>221</th>
      <td>0</td>
      <td>2000-08-09</td>
      <td>20.220739</td>
    </tr>
  </tbody>
</table>
</div>

``` python
fill_gaps(with_gaps, freq='D')
```

<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>unique_id</th>
      <th>ds</th>
      <th>y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0</td>
      <td>2000-08-01</td>
      <td>18.543147</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0</td>
      <td>2000-08-02</td>
      <td>19.941764</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0</td>
      <td>2000-08-03</td>
      <td>NaN</td>
    </tr>
    <tr>
      <th>3</th>
      <td>0</td>
      <td>2000-08-04</td>
      <td>21.968733</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0</td>
      <td>2000-08-05</td>
      <td>NaN</td>
    </tr>
    <tr>
      <th>5</th>
      <td>0</td>
      <td>2000-08-06</td>
      <td>NaN</td>
    </tr>
    <tr>
      <th>6</th>
      <td>0</td>
      <td>2000-08-07</td>
      <td>NaN</td>
    </tr>
    <tr>
      <th>7</th>
      <td>0</td>
      <td>2000-08-08</td>
      <td>19.091509</td>
    </tr>
    <tr>
      <th>8</th>
      <td>0</td>
      <td>2000-08-09</td>
      <td>20.220739</td>
    </tr>
  </tbody>
</table>
</div>

### Evaluating

``` python
from functools import partial

import numpy as np

from utilsforecast.evaluation import evaluate
from utilsforecast.losses import mape, mase
```

``` python
valid = series.groupby('unique_id').tail(7).copy()
train = series.drop(valid.index)
rng = np.random.RandomState(0)
valid['seas_naive'] = train.groupby('unique_id')['y'].tail(7).values
valid['rand_model'] = valid['y'] * rng.rand(valid['y'].shape[0])
daily_mase = partial(mase, seasonality=7)
evaluate(valid, metrics=[mape, daily_mase], train_df=train)
```

<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>unique_id</th>
      <th>metric</th>
      <th>seas_naive</th>
      <th>rand_model</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0</td>
      <td>mape</td>
      <td>0.024139</td>
      <td>0.440173</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1</td>
      <td>mape</td>
      <td>0.054259</td>
      <td>0.278123</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2</td>
      <td>mape</td>
      <td>0.042642</td>
      <td>0.480316</td>
    </tr>
    <tr>
      <th>3</th>
      <td>0</td>
      <td>mase</td>
      <td>0.907149</td>
      <td>16.418014</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1</td>
      <td>mase</td>
      <td>0.991635</td>
      <td>6.404254</td>
    </tr>
    <tr>
      <th>5</th>
      <td>2</td>
      <td>mase</td>
      <td>1.013596</td>
      <td>11.365040</td>
    </tr>
  </tbody>
</table>
</div>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Nixtla/utilsforecast",
    "name": "utilsforecast",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "time-series analysis forecasting",
    "author": "Nixtla",
    "author_email": "business@nixtla.io",
    "download_url": "https://files.pythonhosted.org/packages/18/b0/c5a3ef13b537515e57c865549f1c4d19837c6d5531109efec05becd1890a/utilsforecast-0.2.9.tar.gz",
    "platform": null,
    "description": "utilsforecast\n================\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n## Install\n\n### PyPI\n\n``` sh\npip install utilsforecast\n```\n\n### Conda\n\n``` sh\nconda install -c conda-forge utilsforecast\n```\n\n## How to use\n\n### Generate synthetic data\n\n``` python\nfrom utilsforecast.data import generate_series\n```\n\n``` python\nseries = generate_series(3, with_trend=True, static_as_categorical=False)\nseries\n```\n\n<div>\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    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>2000-01-01</td>\n      <td>0.422133</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0</td>\n      <td>2000-01-02</td>\n      <td>1.501407</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>2000-01-03</td>\n      <td>2.568495</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0</td>\n      <td>2000-01-04</td>\n      <td>3.529085</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>2000-01-05</td>\n      <td>4.481929</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>481</th>\n      <td>2</td>\n      <td>2000-06-11</td>\n      <td>163.914625</td>\n    </tr>\n    <tr>\n      <th>482</th>\n      <td>2</td>\n      <td>2000-06-12</td>\n      <td>166.018479</td>\n    </tr>\n    <tr>\n      <th>483</th>\n      <td>2</td>\n      <td>2000-06-13</td>\n      <td>160.839176</td>\n    </tr>\n    <tr>\n      <th>484</th>\n      <td>2</td>\n      <td>2000-06-14</td>\n      <td>162.679603</td>\n    </tr>\n    <tr>\n      <th>485</th>\n      <td>2</td>\n      <td>2000-06-15</td>\n      <td>165.089288</td>\n    </tr>\n  </tbody>\n</table>\n<p>486 rows \u00d7 3 columns</p>\n</div>\n\n### Plotting\n\n``` python\nfrom utilsforecast.plotting import plot_series\n```\n\n``` python\nfig = plot_series(series, plot_random=False, max_insample_length=50, engine='matplotlib')\nfig.savefig('imgs/index.png', bbox_inches='tight')\n```\n\n![](https://raw.githubusercontent.com/Nixtla/utilsforecast/main/nbs/imgs/index.png)\n\n### Preprocessing\n\n``` python\nfrom utilsforecast.preprocessing import fill_gaps\n```\n\n``` python\nserie = series[series['unique_id'].eq(0)].tail(10)\n# drop some points\nwith_gaps = serie.sample(frac=0.5, random_state=0).sort_values('ds')\nwith_gaps\n```\n\n<div>\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    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>213</th>\n      <td>0</td>\n      <td>2000-08-01</td>\n      <td>18.543147</td>\n    </tr>\n    <tr>\n      <th>214</th>\n      <td>0</td>\n      <td>2000-08-02</td>\n      <td>19.941764</td>\n    </tr>\n    <tr>\n      <th>216</th>\n      <td>0</td>\n      <td>2000-08-04</td>\n      <td>21.968733</td>\n    </tr>\n    <tr>\n      <th>220</th>\n      <td>0</td>\n      <td>2000-08-08</td>\n      <td>19.091509</td>\n    </tr>\n    <tr>\n      <th>221</th>\n      <td>0</td>\n      <td>2000-08-09</td>\n      <td>20.220739</td>\n    </tr>\n  </tbody>\n</table>\n</div>\n\n``` python\nfill_gaps(with_gaps, freq='D')\n```\n\n<div>\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    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>2000-08-01</td>\n      <td>18.543147</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0</td>\n      <td>2000-08-02</td>\n      <td>19.941764</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>2000-08-03</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0</td>\n      <td>2000-08-04</td>\n      <td>21.968733</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>2000-08-05</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0</td>\n      <td>2000-08-06</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0</td>\n      <td>2000-08-07</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>0</td>\n      <td>2000-08-08</td>\n      <td>19.091509</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>0</td>\n      <td>2000-08-09</td>\n      <td>20.220739</td>\n    </tr>\n  </tbody>\n</table>\n</div>\n\n### Evaluating\n\n``` python\nfrom functools import partial\n\nimport numpy as np\n\nfrom utilsforecast.evaluation import evaluate\nfrom utilsforecast.losses import mape, mase\n```\n\n``` python\nvalid = series.groupby('unique_id').tail(7).copy()\ntrain = series.drop(valid.index)\nrng = np.random.RandomState(0)\nvalid['seas_naive'] = train.groupby('unique_id')['y'].tail(7).values\nvalid['rand_model'] = valid['y'] * rng.rand(valid['y'].shape[0])\ndaily_mase = partial(mase, seasonality=7)\nevaluate(valid, metrics=[mape, daily_mase], train_df=train)\n```\n\n<div>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>unique_id</th>\n      <th>metric</th>\n      <th>seas_naive</th>\n      <th>rand_model</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>mape</td>\n      <td>0.024139</td>\n      <td>0.440173</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>mape</td>\n      <td>0.054259</td>\n      <td>0.278123</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>mape</td>\n      <td>0.042642</td>\n      <td>0.480316</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0</td>\n      <td>mase</td>\n      <td>0.907149</td>\n      <td>16.418014</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>mase</td>\n      <td>0.991635</td>\n      <td>6.404254</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>2</td>\n      <td>mase</td>\n      <td>1.013596</td>\n      <td>11.365040</td>\n    </tr>\n  </tbody>\n</table>\n</div>\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "Forecasting utilities",
    "version": "0.2.9",
    "project_urls": {
        "Homepage": "https://github.com/Nixtla/utilsforecast"
    },
    "split_keywords": [
        "time-series",
        "analysis",
        "forecasting"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c0dc75080416c546291c378b2651633a14900e950267183a05d9522f94ae9aec",
                "md5": "a2049a31e322df41ea62e5956680f655",
                "sha256": "684398bb8d6e460d53aa2a7c8b9f4256573d61fcd387fc2ecc193f8b5328cab9"
            },
            "downloads": -1,
            "filename": "utilsforecast-0.2.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a2049a31e322df41ea62e5956680f655",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 41682,
            "upload_time": "2024-11-22T17:28:31",
            "upload_time_iso_8601": "2024-11-22T17:28:31.479042Z",
            "url": "https://files.pythonhosted.org/packages/c0/dc/75080416c546291c378b2651633a14900e950267183a05d9522f94ae9aec/utilsforecast-0.2.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "18b0c5a3ef13b537515e57c865549f1c4d19837c6d5531109efec05becd1890a",
                "md5": "f8148029517533efc68b5fd7654d925a",
                "sha256": "f6e0515637d5f6556327e3b1784a3dc190c7a193b9419aca029e1395d099215c"
            },
            "downloads": -1,
            "filename": "utilsforecast-0.2.9.tar.gz",
            "has_sig": false,
            "md5_digest": "f8148029517533efc68b5fd7654d925a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 40973,
            "upload_time": "2024-11-22T17:28:33",
            "upload_time_iso_8601": "2024-11-22T17:28:33.557352Z",
            "url": "https://files.pythonhosted.org/packages/18/b0/c5a3ef13b537515e57c865549f1c4d19837c6d5531109efec05becd1890a/utilsforecast-0.2.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-22 17:28:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Nixtla",
    "github_project": "utilsforecast",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "utilsforecast"
}
        
Elapsed time: 0.66440s