getting-started-timegpt_quickstart_polars_.mdx 6.69 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
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
202
203
204
205
206
207
208
209
210
211
212
---
title: "TimeGPT Quickstart (Polars)"
description: "Get started with TimeGPT using Polars for efficient data processing."
icon: "bolt-lightning"
---

<Info>
TimeGPT is a production-ready, generative pretrained transformer for time series. It can make accurate predictions in just a few lines of code across domains like retail, electricity, finance, and IoT.
</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/getting-started/21_polars_quickstart.ipynb)

<Steps>
  <Step title="Create a TimeGPT Account and Generate an API Key">
    1. Visit [dashboard.nixtla.io](https://dashboard.nixtla.io/)
    2. Sign in with Google, GitHub, or email
    3. Select **API Keys** in the menu, then click **Create New API Key**
    4. Copy your generated API key using the provided button

    <Frame caption="TimeGPT dashboard with API key management">
      ![TimeGPT dashboard with API key management](https://github.com/Nixtla/nixtla/blob/main/nbs/img/dashboard.png?raw=true)
    </Frame>
  </Step>

  <Step title="Install Nixtla">
  ```bash install-nixtla
  pip install nixtla
  ```
  </Step>

  <Step title="Import and Validate Your Nixtla Client">
  ```python client-setup
  from nixtla import NixtlaClient

  # Instantiate the NixtlaClient
  nixtla_client = NixtlaClient(
      api_key='my_api_key_provided_by_nixtla'
  )

  # Validate the API key
  nixtla_client.validate_api_key()
  ```

  <Warning>
    For enhanced security, check [Setting Up your API Key](https://docs.nixtla.io/docs/getting-started-setting_up_your_api_key).
  </Warning>
  </Step>

  <Step title="Make Forecasts with Polars">
    <AccordionGroup>
      <Accordion title="1. Load and Preview the Dataset">
        <Info>
          We use the **AirPassengers** dataset, containing monthly airline passenger totals from 1949 to 1960. This dataset is a classic example for time series forecasting.
        </Info>

      ```python load-airpassengers-data
      import polars as pl

      df = pl.read_csv(
          'https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv',
          try_parse_dates=True,
      )

      df.head()
      ```

      | timestamp    | value   |
| ------------ | ------- |
| 1949-01-01   | 112     |
| 1949-02-01   | 118     |
| 1949-03-01   | 132     |
| 1949-04-01   | 129     |
| 1949-05-01   | 121     |



      <Info>
        **Plot the dataset** for a quick visual inspection:
      </Info>

      ```python plot-airpassengers-data
      nixtla_client.plot(df, time_col='timestamp', target_col='value')
      ```

      <Frame caption="Monthly airline passengers from 1949–1960">
        ![Monthly airline passengers from 1949–1960](https://raw.githubusercontent.com/Nixtla/nixtla/readme_docs/nbs/_docs/docs/getting-started/21_polars_quickstart_files/figure-markdown_strict/cell-13-output-1.png)
      </Frame>
      </Accordion>

      <Accordion title="2. Data Requirements">
        <Info>

            - The target variable column should not contain missing or non-numeric values.

            - Ensure there are no gaps in the timestamps.

            - The time column must be of type [Date](https://docs.pola.rs/api/python/stable/reference/api/polars.datatypes.Date.html) or [Datetime](https://docs.pola.rs/api/python/stable/reference/api/polars.datatypes.Datetime.html).


          For comprehensive details, visit [Data Requirements](https://docs.nixtla.io/docs/getting-started-data_requirements).
        </Info>
      </Accordion>

      <Accordion title="3. Generate a 12-Month Forecast">
      ```python forecast-timegpt-12-months
      timegpt_fcst_df = nixtla_client.forecast(
          df=df,
          h=12,
          freq='1mo',
          time_col='timestamp',
          target_col='value'
      )

      timegpt_fcst_df.head()
      ```

      <Info>
        Forecast values for the next 12 months:
      </Info>

      | timestamp    | TimeGPT      |
| ------------ | ------------ |
| 1961-01-01   | 437.837921   |
| 1961-02-01   | 426.062714   |
| 1961-03-01   | 463.116547   |
| 1961-04-01   | 478.244507   |
| 1961-05-01   | 505.646484   |



      <Info>
        Plot the 12-month forecast alongside the actual data:
      </Info>

      ```python plot-timegpt-12-months
      nixtla_client.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')
      ```

      <Frame caption="Comparison of forecast and actual data (12 months)">
        ![Comparison of forecast and actual data (12 months)](https://raw.githubusercontent.com/Nixtla/nixtla/readme_docs/nbs/_docs/docs/getting-started/21_polars_quickstart_files/figure-markdown_strict/cell-15-output-1.png)
      </Frame>
      </Accordion>

      <Accordion title="4. Forecast Longer Horizons (36 Months)">
        <Warning>
          When requesting `h` (horizon) values larger than the models maximum, you may see a warning.
        </Warning>

      ```python forecast-timegpt-36-months
      timegpt_fcst_df = nixtla_client.forecast(
          df=df,
          h=36,
          time_col='timestamp',
          target_col='value',
          freq='1mo',
          model='timegpt-1-long-horizon'
      )

      timegpt_fcst_df.head()
      ```

      <Info>
        Plot the 36-month forecast results:
      </Info>

      ```python plot-timegpt-36-months
      nixtla_client.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')
      ```

      <Frame caption="36-month forecast">
        ![36-month forecast](https://raw.githubusercontent.com/Nixtla/nixtla/readme_docs/nbs/_docs/docs/getting-started/21_polars_quickstart_files/figure-markdown_strict/cell-17-output-1.png)
      </Frame>
      </Accordion>

      <Accordion title="5. Generate a Shorter Forecast (6 Months)">
      ```python forecast-timegpt-6-months
      timegpt_fcst_df = nixtla_client.forecast(
          df=df,
          h=6,
          time_col='timestamp',
          target_col='value',
          freq='1mo'
      )

      nixtla_client.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')
      ```

      <Frame caption="6-month forecast">
        ![6-month forecast](https://raw.githubusercontent.com/Nixtla/nixtla/readme_docs/nbs/_docs/docs/getting-started/21_polars_quickstart_files/figure-markdown_strict/cell-18-output-2.png)
      </Frame>
      </Accordion>
    </AccordionGroup>
  </Step>
</Steps>

<CardGroup cols={1}>
  <Card title="Key Takeaways">

      - TimeGPT can forecast short to long horizons easily.

      - Minimal setup is required—just an API key and your dataset!

      - Data validation helps ensure accurate forecasts.


  </Card>
</CardGroup>

<Check>
You are now ready to harness TimeGPT for quick and reliable time series forecasting using Polars!
</Check>