Lesson Introduction

Hi there! Today, we're diving into a significant concept in machine learning known as cross-validation. Imagine you're baking a cake. You wouldn't just taste one slice, right? You'd want to taste slices from different parts to ensure they are evenly good. That's what cross-validation does for machine learning models. It ensures our models work well on different sections of the data.

By the end of this lesson, you'll understand cross-validation, perform it using Scikit-Learn, and interpret the results. Let's get started!

Introduction to Cross-Validation

What is cross-validation?

Cross-validation evaluates a machine learning model by splitting the data in multiple ways. Instead of just one split into training and testing sets, we split it multiple times, each time in a different way, and train and test the model on these splits. This gives a more reliable performance estimate.

Think of it like trying different slices of your cake to ensure it's consistently good.

In cross-validation, a fold refers to a single iteration of splitting the data into training and validation sets. For example, in 5-fold cross-validation, the entire dataset is divided into 5 parts (called folds). Each fold takes a turn being the validation set while the remaining folds together form the training set. This process repeats 5 times.

Example of Cross-Validation

Let's see how to do this in Python.

First, we need a real-world dataset. We'll use the "wine dataset" from Scikit-Learn.

Python
from sklearn.datasets import load_wine
from sklearn.preprocessing import StandardScaler

# Load the wine dataset
X, y = load_wine(return_X_y=True)
X = StandardScaler().fit_transform(X)

Here, X contains the features (input data), and y contains the target (output labels). Note that we scale the features to improve the model's convergence.

Next, we'll split the data into training and testing sets. Even when using cross-validation, it's essential to hold back a portion of the data for final testing. Cross-validation will be performed only on the training data.

from sklearn.model_selection import train_test_split

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

And we'll use DecisionTreeClassifier, a simple yet well-performing model.

from sklearn.tree import DecisionTreeClassifier

# Create a Decision Tree model
decision_tree = DecisionTreeClassifier(random_state=42)
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal