Get startedGet started for free

Lasso model results

Now that you've trained the Lasso model, you'll score its predictive capacity (\(R^2\)) on the test set and count how many features are ignored because their coefficient is reduced to zero.

The X_test and y_test datasets have been pre-loaded for you.

The Lasso() model and StandardScaler() have been instantiated as la and scaler respectively and both were fitted to the training data.

This exercise is part of the course

Dimensionality Reduction in Python

View Course

Exercise instructions

  • Transform the test set with the pre-fitted scaler.
  • Calculate the \(R^2\) value on the scaled test data.
  • Create a list that has True values when coefficients equal 0.
  • Calculate the total number of features with a coefficient of 0.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Transform the test set with the pre-fitted scaler
X_test_std = scaler.____

# Calculate the coefficient of determination (R squared) on X_test_std
r_squared = la.____(____, ____)
print(f"The model can predict {r_squared:.1%} of the variance in the test set.")

# Create a list that has True values when coefficients equal 0
zero_coef = la.____ == ____

# Calculate how many features have a zero coefficient
n_ignored = sum(____)
print(f"The model has ignored {n_ignored} out of {len(la.coef_)} features.")
Edit and Run Code