Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

cross_validate

Evaluate one or more metrics across cross-validation folds while recording fit and score times. Optional outputs include training scores, fitted estimators, and the indices used for each split.

How it works

The function repeatedly fits cloned copies of an estimator on training folds and evaluates them on held-out folds. It can use one or several scorers, select the splitting strategy, run folds in parallel, and optionally include training scores, fitted estimators, and train/test indices in its result dictionary.

Syntax

cross_validate(estimator, X, y=None, *, groups=None, scoring=None, cv=None, n_jobs=None, verbose=0, params=None, pre_dispatch='2*n_jobs', return_train_score=False, return_estimator=False, return_indices=False, error_score=nan)

Parameters

NameTypeDescription
estimatorestimator objectEstimator implementing fit; the object fitted on each training split.
Xarray-like or sparse matrixInput samples with shape (n_samples, n_features).
yarray-like or NoneTarget values for supervised learning; defaults to None.
groupsarray-like or NoneGroup labels passed to the splitter when grouping is used.
scoringstr, callable, list, tuple, or dictScorer, collection of scorers, or callable used for evaluation.
cvint, splitter, iterable, or NoneCross-validation strategy; None defaults to five folds.
n_jobsint or NoneNumber of parallel jobs; None means one job unless a backend changes it.
verboseintVerbosity level.
paramsdict or NoneMetadata routed to estimator and splitter methods.
pre_dispatchint or strNumber of jobs dispatched in advance; default is 2*n_jobs.
return_train_scoreboolWhether to include training scores.
return_estimatorboolWhether to include fitted estimators.

Returns

A dictionary of arrays containing test scores, optional train scores, fit and score times, and optionally fitted estimators and train/test indices.

Key points

  • cross_validate combines fold-based metric evaluation with measurements of fitting and scoring duration.
  • The estimator must be an object implementing fit and is independently trained for each fold.
  • The cv argument accepts a default, fold count, splitter, or iterable of train/test index arrays.
  • The output is a dictionary whose arrays contain per-fold scores and timing information.

Common mistakes

  • With the default integer or None splitter, binary and multiclass classifiers use stratified folds rather than ordinary K-fold splits.
  • Choose error_score carefully: failures can either raise immediately or produce a numeric score with a warning.

Example: Evaluate classification metrics with cross-validation

from sklearn.datasets import load_iris
from sklearn.model_selection import cross_validate
from sklearn.linear_model import LogisticRegression

X, y = load_iris(return_X_y=True)
result = cross_validate(
    LogisticRegression(max_iter=1000),
    X,
    y,
    cv=5,
    scoring=["accuracy", "f1_macro"],
    return_train_score=True,
)
print(result["test_accuracy"])
print(result["fit_time"])

Runs five-fold validation with two metrics and returns per-fold test scores, training scores, and fitting times.

When to use it

Estimate generalization performance across folds

Model evaluation

Collect several evaluation metrics in one run

Metric comparison

Measure fitting and scoring time

Performance profiling

Retain fold-specific estimators and split indices

Model inspection

Interview questions

QWhat does cross_validate return?

It evaluates one or more metrics across cross-validation folds and records fit and score times. The returned dictionary contains arrays with one entry per fold, using keys such as test_score, fit_time, and score_time.

Follow-up: How would you compare training and validation behavior? Set return_train_score=True, then compare the train_* arrays with the corresponding test_* arrays.

QHow is the cross-validation scheme selected?

cv controls the splitting strategy. None uses five folds by default; an integer sets the number of folds, while a splitter or iterable can provide custom train/test index splits.

Follow-up: How can fold-specific fitted models be inspected? Set return_estimator=True to include the fitted estimator from each fold.

QHow can multiple evaluation metrics be collected?

scoring can be a single scorer, a list or tuple of scorer names, a dictionary of names to scorers, or a callable. With multiple metrics, result keys are named using each scorer, such as test_accuracy.

Follow-up: How can computation be parallelized? Set n_jobs to the number of parallel jobs, and use pre_dispatch to limit jobs dispatched ahead of execution.

  • cross_val_score
  • cross_val_predict
  • KFold
  • StratifiedKFold
  • GroupKFold
  • check_cv

Source: cross_validate — scikit-learn 1.9.1 documentation. Checked 2026-10-15.