cross_val_score
dataset
training set
machine learning
scikit-learn

Do I give cross_val_score the entire dataset or just the training-set?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Understanding `cross_val_score()` in Scikit-Learn

When training machine learning models using Scikit-learn, one of the most common questions is whether to pass the entire dataset or just the training set to the `cross_val_score()` function. This function is instrumental for evaluating the performance of a model through cross-validation. In this article, we will explore the nuances of using `cross_val_score()`, providing technical explanations augmented with examples to guide you in making the best decision for your dataset.

Understanding Cross-Validation

Cross-validation is a statistical method used to estimate the skill of machine learning models. It is suitable for understanding how a model will generalize to an independent dataset (unseen data). The basic idea is to divide the dataset into a number of groups, or "folds." The model is trained on all but one of the folds and validated on the remaining fold. This process is repeated for each fold, and the results are averaged to produce a single performance metric.

The Role of `cross_val_score()`

In Scikit-learn, `cross_val_score()` automates the process of cross-validation, providing a simple interface to evaluate the accuracy, precision, recall, or any other metric. Understanding the input to this function is crucial for obtaining meaningful cross-validation results.

Input to `cross_val_score()`: Entire Dataset vs. Training Set

  • Entire Dataset: In most workflows, you pass the entire dataset to `cross_val_score()`. This method follows the K-fold validation mechanism: the data is split into K subsets, and the holdout method is repeated K times. Each time, one of the K subsets is used as the test set, and the other K-1 subsets form the training set.
  • Training Set Only: Some practitioners may intuitively feel the need to provide just the training set, excluding a holdout for test data. This is generally not recommended in the context of Scikit-learn's `cross_val_score()` when the goal is to assess model performance as part of selection or tuning.

Practical Example

Here is an example to illustrate using `cross_val_score()` with the entire dataset:

  • By default, `cross_val_score()` computes the accuracy of each fold. However, you can specify any other scoring metric by passing the `scoring` parameter, such as `scoring='precision'`, `scoring='recall'`, etc.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design