Model evaluation
Estimate generalization performance for a classifier or regressor before final training.
Scikit-Learn · Function
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.
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.
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)| Name | Type | Description |
|---|---|---|
estimator | estimator object implementing fit | Estimator implementing fit; the object fitted on each training split. |
X | array-like or sparse matrix of shape (n_samples, n_features) | Input samples used to fit the estimator. |
y | array-like of shape (n_samples,) or (n_samples, n_outputs), default=None | Targets for supervised learning; defaults to None. |
groups | array-like of shape (n_samples,), default=None | Group labels used when splitting samples into train and test sets. |
scoring | str or callable, default=None | Metric name or scorer callable; defaults to the estimator's scorer when available. |
cv | int, cross-validation generator or iterable, default=None | Cross-validation splitter specification; defaults to the documented default strategy. |
n_jobs | int, default=None | Number of parallel jobs; None uses the default job behavior. |
verbose | int, default=0 | Verbosity level. |
params | dict, default=None | Metadata routed to estimator fit, scorer, and splitter methods. |
pre_dispatch | int or str, default='2*n_jobs' | Number of jobs dispatched ahead of execution. |
error_score | ‘raise’ or numeric, default=nan | Value assigned when scoring raises an error; defaults to NaN. |
scores: ndarray of float of shape (len(list(cv)),), containing the estimator score for each cross-validation run.
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.
Estimate generalization performance for a classifier or regressor before final training.
Compare candidate estimators using the same folds and scoring rule.
Measure performance with grouped observations or a custom split generator.
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.
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.
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_validatecross_val_predictKFoldStratifiedKFold