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.
Diese Übung ist Teil des Kurses
Dimensionality Reduction in Python
Anleitung zur Übung
- 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.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
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')