CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Dimensionality Reduction in Python

Afficher le cours

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.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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')
Modifier et exécuter le code