Cross-Validation

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.

1

1. Install and Import Dependencies

Make sure you have the pandas and nixtla libraries installed.

Import dependencies
import pandas as pd
from nixtla import NixtlaClient
2

2. Initialize Nixtla Client

Replace my_api_key_provided_by_nixtla with the API key you received from Nixtla.

Default NixtlaClient initialization
nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key='my_api_key_provided_by_nixtla'
)
3

3. Read and Prepare Data

Load dataset
# Read the data
df = pd.read_csv(
    "https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv"
)

In this example, we are using the airplane passengers dataset. You can replace the URL with your own dataset.

4

4. Perform Cross-Validation

Specify the number of windows with the n_windows argument. Each window will be used to generate forecasts and evaluate performance.

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",
)

If you’re using an Azure AI endpoint for forecasting, define the model explicitly by setting model="azureai" in the forecast method:

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

timegpt-1

This is the default model in the public API. It provides reliable short- to medium-range forecasts.

timegpt-1-long-horizon

Designed for extended-range forecasts. Refer to the long-horizon forecasting tutorial for more details.

For more guidance and examples, consult our cross-validation tutorial.