sklearn
GridSearchCV
MSE
R2
machine learning

How to get both MSE and R2 from a sklearn GridSearchCV?

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

Introduction

In machine learning, model evaluation is a crucial step to ensure the robustness and effectiveness of predictive models. The Scikit-learn library offers numerous tools that simplify model evaluation. Specifically, GridSearchCV is a valuable technique for hyperparameter tuning, while also providing the ability to cross-validate the model. However, extracting multiple evaluation metrics such as Mean Squared Error (MSE) and R-squared (R²) from GridSearchCV can be tricky. This article will demonstrate how to achieve this, with technical explanations and examples.

Understanding Key Concepts

Mean Squared Error (MSE)

Definition: MSE is a measure of the average squared difference between the estimated values (y_hat ) and the actual value (y ). • Formula: MSE=1n_i=1n(y_iy^_i)2\text{MSE} = \frac{1}{n} \sum\_{i=1}^{n} (y\_i - \hat{y}\_i)^2

Used For: Measuring the quality of an estimator; the lower the MSE, the better the estimation quality.

R-squared (R²)

Definition: R² provides the proportion of the variance in the dependent variable that is predictable from the independent variables. • Formula: R2=1(y_iy^_i)2(y_iyˉ)2R^2 = 1 - \frac{\sum (y\_i - \hat{y}\_i)^2}{\sum (y\_i - \bar{y})^2}

Used For: Determining the goodness-of-fit; R-squared values closer to 1 indicate a better fit.

Using GridSearchCV to Obtain MSE and R²

Step-by-step Guide

  1. Importing Libraries

Hyperparameter Tuning: GridSearchCV iterates over parameter values specified in param_grid to find the optimal combination. • Custom Scorers: make_scorer is used to create custom scoring metrics to calculate MSE and R². • Parallel Evaluation: By setting the scoring parameter as a dictionary and specifying refit='R2' , each fit is judged by both metrics but optimized for R². • Accessing Results: The cv_results_ attribute provides access to scores from cross-validation.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free 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

All Rights Reserved.