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

Scikit-Learn · Function

cross_val_score

Evaluate an estimator by cross-validation and return one score for each split. The function can use a named metric, a custom scorer, or the estimator's default scorer.

How it works

The function repeatedly fits cloned versions of an estimator on the training portion of each cross-validation split, evaluates the configured scorer on the held-out portion, and returns one score for every split. If scoring is omitted, the estimator's default scorer is used; parallel execution can be enabled across splits.

Syntax

cross_val_score(estimator, X, y=None, *, groups=None, scoring=None, cv=None, n_jobs=None, verbose=0, params=None, pre_dispatch='2*n_jobs', error_score=nan)

Parameters

NameTypeDescription
estimatorestimator object implementing fitEstimator implementing fit; the object fitted on each training split.
Xarray-like or sparse matrix of shape (n_samples, n_features)Input samples used to fit the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=NoneTargets for supervised learning; defaults to None.
groupsarray-like of shape (n_samples,), default=NoneGroup labels used when splitting samples into train and test sets.
scoringstr or callable, default=NoneMetric name or scorer callable; defaults to the estimator's scorer when available.
cvint, cross-validation generator or iterable, default=NoneCross-validation splitter specification; defaults to the documented default strategy.
n_jobsint, default=NoneNumber of parallel jobs; None uses the default job behavior.
verboseint, default=0Verbosity level.
paramsdict, default=NoneMetadata routed to estimator fit, scorer, and splitter methods.
pre_dispatchint or str, default='2*n_jobs'Number of jobs dispatched ahead of execution.
error_score‘raise’ or numeric, default=nanValue assigned when scoring raises an error; defaults to NaN.

Returns

scores: ndarray of float of shape (len(list(cv)),), containing the estimator score for each cross-validation run.

Key points

  • cross_val_score is a compact helper for measuring an estimator through cross-validation.
  • estimator supplies the model that is fitted independently for each split.
  • cv determines how observations are partitioned into training and validation portions.
  • scoring selects the metric or callable used for each held-out evaluation.
  • n_jobs controls how many split evaluations may run concurrently.
  • The result is a one-dimensional floating-point array containing one score per CV run.

Common mistakes

  • Leaving scoring as None delegates metric selection to the estimator's default scorer, when one exists.
  • A custom scorer must accept the estimator, validation data, and targets, and return exactly one value.
  • n_jobs parallelizes independent folds, so resource use can increase substantially when many jobs are enabled.

Example: Five-fold classification scoring

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

X, y = load_iris(return_X_y=True)
model = LogisticRegression(max_iter=1000)
scores = cross_val_score(model, X, y, cv=5)
print(scores)
print(scores.mean())

Fits the classifier separately on each of five folds and prints one validation score per fold plus their mean.

When to use it

Model evaluation

Estimate generalization performance for a classifier or regressor before final training.

Estimator comparison

Compare candidate estimators using the same folds and scoring rule.

Structured validation

Measure performance with grouped observations or a custom split generator.

Interview questions

QWhat does cross_val_score return?

It evaluates an estimator on each cross-validation split and returns one floating-point score per run. The output is an array whose length equals the number of generated CV splits.

Follow-up: How would you obtain several metrics or timing information? Use cross_validate, which is designed to return multiple metrics and additional fit and score details.

QHow can the cross-validation strategy be selected?

The cv argument controls the splitting strategy. It can be omitted, set to an integer, supplied as a CV splitter, or provided as an iterable yielding train/test index arrays.

Follow-up: What happens with an integer cv for classification? The default strategy is stratified when appropriate, while KFold is used otherwise.

QWhat are the essential inputs?

The estimator must implement fit, and X contains the samples and features used for fitting. y is the target array for supervised learning and defaults to None for cases where targets are unnecessary.

Follow-up: How can computation be distributed? Set n_jobs to the desired parallel-job count; fitting and scoring are parallelized over CV splits.

  • cross_validate
  • cross_val_predict
  • KFold
  • StratifiedKFold

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