Creating a LassoCV regressor
You'll be predicting biceps circumference on a subsample of the male ANSUR dataset using the LassoCV()
regressor that automatically tunes the regularization strength (alpha value) using Cross-Validation.
The standardized training and test data has been pre-loaded for you as X_train
, X_test
, y_train
, and y_test
.
This exercise is part of the course
Dimensionality Reduction in Python
Exercise instructions
- Create and fit the LassoCV model on the training set.
- Calculate \(R^2\) on the test set.
- Create a mask for coefficients not equal to zero.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
from sklearn.linear_model import LassoCV
# Create and fit the LassoCV model on the training set
lcv = ____
lcv.____
print(f'Optimal alpha = {lcv.alpha_:.3f}')
# Calculate R squared on the test set
r_squared = lcv.____
print(f'The model explains {r_squared:.1%} of the test set variance')
# Create a mask for coefficients not equal to zero
lcv_mask = ____
print(f'{sum(lcv_mask)} features out of {len(lcv_mask)} selected')