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.
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:
• 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:
• 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
- 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
- How to get classification probabilities from PySpark MultilayerPerceptronClassifier?
- How to get comparable and reproducible results from LogisticRegressionCV and GridSearchCV
- how to get covariance matrix in tensorflow?
- how to get covariance matrix in tensorflow?
- How to get current available GPUs in tensorflow?
- How to get current available GPUs in tensorflow?
- How to get current TensorFlow name scope
- how to get data type of a tensor in tensorflow?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.