---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://catboost.ai/docs/en/features/cross-validation.md
  - href: en/features/cross-validation.md
    type: text/markdown
    title: Markdown version
  - href: ../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://catboost.ai/docs/en/llms.txt

# Cross-validation

CatBoost allows to perform cross-validation on the given dataset.

<!-- source: en/_includes/work_src/reusage-common-phrases/choose-the-required-implementation-for-more-details.md -->
Choose the implementation for more details.
<!-- endsource: en/_includes/work_src/reusage-common-phrases/choose-the-required-implementation-for-more-details.md -->


## Python package

### Class

[cv](https://catboost.ai/docs/en/concepts/python-reference_cv.md)

**Class purpose**

<!-- source: en/_includes/work_src/reusage-python/cv__purpose.md -->
Perform cross-validation on the dataset.
<!-- endsource: en/_includes/work_src/reusage-python/cv__purpose.md -->

## Command-line version

For the [catboost fit](https://catboost.ai/docs/en/concepts/cli-reference_cross-validation.md) command:

**Purpose**

<!-- source: en/_includes/work_src/reusage/cli__cross-validation__purpose__div.md -->
Training can be launched in cross-validation mode. In this case, only the training dataset is required. This dataset is split, and the resulting folds are used as the learning and evaluation datasets. If the input dataset contains the GroupId column, all objects from one group are added to the same fold.

Each cross-validation run from the command-line interface launches one training out of N trainings in N-fold cross-validation.

Use one of the following methods to get aggregated N-fold cross-validation results:
- Run the training in cross-validation mode from the command-line interface N times with different validation folds and aggregate results by hand.
- Use the [cv](https://catboost.ai/docs/en/concepts/python-reference_cv.md) function of the [Python package](https://catboost.ai/docs/en/concepts/python-quickstart.md) instead of the command-line version. It returns aggregated results out-of-the-box.
<!-- endsource: en/_includes/work_src/reusage/cli__cross-validation__purpose__div.md -->

### Command keys

#### --cv

**Key description**

Enable the cross-validation mode and specify the launching parameters.

Format:
```no-highlight
<cv_type>:<fold_index>;<fold_count>
```

The following cross-validation types (`cv_type`) are supported:

##### Classical

Format: `Classical<fold_index>;<fold_count>`

- `fold_index` is the index of the fold to exclude from the learning data and use for evaluation (indexing starts from zero).

- <!-- source: en/_includes/work_src/reusage/cv__k-param__desc.md -->
  `fold_count` is the number of folds to split the input data into.
  <!-- endsource: en/_includes/work_src/reusage/cv__k-param__desc.md -->


All folds, except the one indexed `n`, are used as the learning dataset. The fold indexed `n` is used as the validation dataset.

<!-- source: en/_includes/work_src/reusage/cv__n_less_than_K.md -->
The inequality $fold\_index < fold\_count$ must be true.
<!-- endsource: en/_includes/work_src/reusage/cv__n_less_than_K.md -->


<!-- source: en/_includes/work_src/reusage/cv__random_shuffling.md -->
The data is randomly shuffled before splitting.
<!-- endsource: en/_includes/work_src/reusage/cv__random_shuffling.md -->


##### Inverted

Format: `Inverted<fold_index>;<fold_count>`
- `fold_index` is the index of the fold to use for learning (indexing starts from zero).
- <!-- source: en/_includes/work_src/reusage/cv__k-param__desc.md -->
  `fold_count` is the number of folds to split the input data into.
  <!-- endsource: en/_includes/work_src/reusage/cv__k-param__desc.md -->

The fold indexed `fold_index` is used as the learning dataset. All other folds are used as the validation dataset.

<!-- source: en/_includes/work_src/reusage/cv__n_less_than_K.md -->
The inequality $fold\_index < fold\_count$ must be true.
<!-- endsource: en/_includes/work_src/reusage/cv__n_less_than_K.md -->


<!-- source: en/_includes/work_src/reusage/cv__random_shuffling.md -->
The data is randomly shuffled before splitting.
<!-- endsource: en/_includes/work_src/reusage/cv__random_shuffling.md -->

{% cut "Example" %}

Split the input dataset into 5 folds, use the one indexed 0 for validation and all others for training:

```no-highlight
--cv Classical:0;5
```

{% endcut %}

#### --cv-rand

**Purpose**

Use this as the seed value for random permutation of the data.

The permutation is performed before splitting the data for cross-validation.

Each seed generates unique data splits.

It must be used with the `--cv` parameter type set to Classical or Inverted.

