--- title: "Cross Validation" description: "Learn how to validate your forecast models using time series cross-validation." icon: "clipboard-check" --- # 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. [![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) Make sure you have the `pandas` and `nixtla` libraries installed. ```python Import dependencies import pandas as pd from nixtla import NixtlaClient ``` Replace `my_api_key_provided_by_nixtla` with the API key you received from Nixtla. ```python Default NixtlaClient initialization nixtla_client = NixtlaClient( # defaults to os.environ.get("NIXTLA_API_KEY") api_key='my_api_key_provided_by_nixtla' ) ``` To use an Azure AI endpoint, add the `base_url` argument. ```python Azure AI NixtlaClient initialization nixtla_client = NixtlaClient( base_url="your azure ai endpoint", api_key="my_api_key_provided_by_nixtla" ) ``` ```python 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. Specify the number of windows with the `n_windows` argument. Each window will be used to generate forecasts and evaluate performance. ```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", ) ``` ```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... ``` If you're using an Azure AI endpoint for forecasting, define the model explicitly by setting `model="azureai"` in the `forecast` method: ```python Forecast with Azure AI model nixtla_client.forecast( ..., model="azureai" ) ``` This is the default model in the public API. It provides reliable short- to medium-range forecasts. Designed for extended-range forecasts. Refer to the [long-horizon forecasting tutorial](https://docs.nixtla.io/docs/tutorials-long_horizon_forecasting) for more details. For more guidance and examples, consult our [cross-validation tutorial](https://docs.nixtla.io/docs/tutorials-cross_validation).