Implementation of Ridge and Lasso Regression on Diabetes Dataset with Hyperparameter Tuning
Experiment
Implementation of Ridge and Lasso Regression on Diabetes Dataset with Hyperparameter Tuning
๐ฏ Aim
To implement and compare Linear, Ridge, and Lasso Regression using evaluation metrics and visualize their performance.
Objectives
- Load and preprocess dataset
- Implement Linear, Ridge, and Lasso regression
- Tune hyperparameters using cross-validation
- Compare using MSE and R²
- Visualize predictions and coefficient shrinkage
๐ Additional Visualization Insight
We will plot:
- Actual vs Predicted values
- Coefficient comparison (shrinkage effect)
๐ป Program
๐ Sample Output (Typical)

Coefficients: Feature Linear Ridge Lasso 0 age 37.904021 42.758013 0.000000 1 sex -241.964362 -208.277776 -168.165267 2 bmi 542.428759 509.219395 554.134694 3 bp 347.703844 319.219682 311.629705 4 s1 -931.488846 -115.066100 -101.862897 5 s2 518.062277 -84.195275 -0.000000 6 s3 163.419983 -188.950272 -235.137459 7 s4 275.317902 153.017313 0.000000 8 s5 736.198859 397.153387 460.585713 9 s6 48.670657 78.115291 36.922057
Best Alpha Values: Ridge Alpha: 0.09102981779915217 Lasso Alpha: 0.07813983904476526
๐ Graph Explanation
๐ Graph 1: Actual vs Predicted
- Points closer to diagonal → better predictions
- Ridge & Lasso usually cluster better than Linear
๐ Graph 2: Coefficient Comparison
- Linear → large coefficients
- Ridge → shrunk coefficients
- Lasso → some coefficients become zero
๐ Interpretation
-
Linear Regression
- Baseline model
- No regularization
-
Ridge Regression
- Slightly better performance
- Reduces overfitting
-
Lasso Regression
- Performs feature selection
- Some coefficients become zero
๐ Observations
| Model | Behavior |
|---|---|
| Linear | No regularization |
| Ridge | Stable, better generalization |
| Lasso | Sparse model |
Result
Ridge and Lasso regression models were successfully implemented and tuned using cross-validation, showing improved or comparable performance to standard linear regression.
- Regularization improves model generalization
- Ridge works well when all features are important
- Lasso helps in feature selection
- Cross-validation is essential for choosing alpha
- Visualization helps understand model behavior clearly

Comments
Post a Comment