capabilities-forecast-cross_validation.mdx 3.43 KB
Newer Older
bailuo's avatar
readme  
bailuo 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
---
title: "Cross Validation"
description: "Learn how to validate your forecast models using time series cross-validation."
icon: "clipboard-check"
---

# Cross-Validation

<Info>
Cross-validation is a robust method to evaluate and improve your forecasting models. By splitting your time series into multiple windows, you can test how well your model performs on unseen data.
</Info>

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Nixtla/nixtla/blob/main/nbs/docs/capabilities/forecast/09_cross_validation.ipynb)

<Steps>
  <Step title="1. Install and Import Dependencies">
    <Info>
      Make sure you have the `pandas` and `nixtla` libraries installed.
    </Info>

```python Import dependencies
import pandas as pd
from nixtla import NixtlaClient
```
  </Step>

  <Step title="2. Initialize Nixtla Client">
    <Check>
      Replace `my_api_key_provided_by_nixtla` with the API key you received from Nixtla.
    </Check>

    <Tabs>
      <Tab title="Default Nixtla Setup">
```python Default NixtlaClient initialization
nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key='my_api_key_provided_by_nixtla'
)
```
      </Tab>
      <Tab title="Azure AI Setup">
        <Check>
          To use an Azure AI endpoint, add the `base_url` argument.
        </Check>
```python Azure AI NixtlaClient initialization
nixtla_client = NixtlaClient(
    base_url="your azure ai endpoint",
    api_key="my_api_key_provided_by_nixtla"
)
```
      </Tab>
    </Tabs>
  </Step>

  <Step title="3. Read and Prepare Data">
```python Load dataset
# Read the data
df = pd.read_csv(
    "https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv"
)
```
    <Info>
      In this example, we are using the airplane passengers dataset. You can replace the URL with your own dataset.
    </Info>
  </Step>

  <Step title="4. Perform Cross-Validation">
    <Info>
      Specify the number of windows with the `n_windows` argument. Each window will be used to generate forecasts and evaluate performance.
    </Info>
```python Cross-validation example
# Cross-validation using two windows
forecast_cv_df = nixtla_client.cross_validation(
    df=df,
    h=12,
    n_windows=2,
    time_col='timestamp',
    target_col="value",
)
```
    <AccordionGroup>
      <Accordion title="Detailed Logging">
```bash Cross-validation logs
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Inferred freq: MS
INFO:nixtla.nixtla_client:Restricting input...
INFO:nixtla.nixtla_client:Calling Cross Validation Endpoint...
```
      </Accordion>
    </AccordionGroup>
  </Step>
</Steps>

<Info>
If you're using an Azure AI endpoint for forecasting, define the model explicitly by setting `model="azureai"` in the `forecast` method:
</Info>

```python Forecast with Azure AI model
nixtla_client.forecast(
    ...,
    model="azureai"
)
```

<CardGroup>
  <Card title="timegpt-1">
    This is the default model in the public API. It provides reliable short- to medium-range forecasts.
  </Card>
  <Card title="timegpt-1-long-horizon">
    Designed for extended-range forecasts. Refer to the [long-horizon forecasting tutorial](https://docs.nixtla.io/docs/tutorials-long_horizon_forecasting) for more details.
  </Card>
</CardGroup>

<Info>
For more guidance and examples, consult our [cross-validation tutorial](https://docs.nixtla.io/docs/tutorials-cross_validation).
</Info>