Evaluating Model with Cross-Validation
Lesson Overview
Welcome to today's lesson on Evaluating Model with Cross-Validation! Our goal is to understand how to reliably assess the performance of our Gradient Boosting model using cross-validation techniques. This lesson will guide you through a quick review of data preparation, introduce the concept and importance of cross-validation, demonstrate implementing cross-validation with the cross_val_score function, and visualize model predictions to better understand the model's performance.
Review of Data Preparation
Before we dive into evaluating our model with cross-validation, let's quickly review the data preparation steps we performed. This will ensure that we're on the same page regarding the dataset and features we're using.
First, we loaded the Tesla ($TSLA) historical prices dataset:
Next, we performed feature engineering to add technical indicators and the target variable:
Finally, we selected our features and target, and standardized the features:
This brings us to the prepared data that we'll use for model training and evaluation.
Introduction to Cross-Validation
Cross-validation is a key technique in evaluating the performance of machine learning models. It helps in assessing how well our model generalizes to an independent dataset. By using cross-validation, we minimize the risk of overfitting and ensure our model's robustness.
In K-Fold Cross-Validation, we split our dataset into k portions (folds). The model is trained on k - 1 folds and tested on the remaining fold. This process is repeated k times, each time using a different fold as the test set. The scores from each fold are then averaged to get a more reliable performance estimate.
Here's a quick explanation of how K-Fold Cross-Validation works:
- First, we split data into
kfolds - Then we train on
k - 1folds and test on the remaining fold - We repeat this
ktimes, each time with a different fold as the test set - Finally, we take the average of the results from each fold
We will use the cross_val_score function from sklearn.model_selection to perform cross-validation efficiently.

