Get startedGet started for free

Visualizing regression model performance

Now you have seen how to evaluate multiple models out of the box, you will build three regression models to predict a song's "energy" levels.

The music_df dataset has had dummy variables for "genre" added. Also, feature and target arrays have been created, and these have been split into X_train, X_test, y_train, and y_test.

The following have been imported for you: LinearRegression, Ridge, Lasso, cross_val_score, and KFold.

This exercise is part of the course

Supervised Learning with scikit-learn

View Course

Exercise instructions

  • Write a for loop using model as the iterator, and model.values() as the iterable.
  • Perform cross-validation on the training features and the training target array using the model, setting cv equal to the KFold object.
  • Append the model's cross-validation scores to the results list.
  • Create a box plot displaying the results, with the x-axis labels as the names of the models.

Hands-on interactive exercise

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

models = {"Linear Regression": LinearRegression(), "Ridge": Ridge(alpha=0.1), "Lasso": Lasso(alpha=0.1)}
results = []

# Loop through the models' values
for ____ in models.values():
  kf = KFold(n_splits=6, random_state=42, shuffle=True)
  
  # Perform cross-validation
  cv_scores = ____(____, ____, ____, cv=____)
  
  # Append the results
  ____.____(____)

# Create a box plot of the results
plt.____(____, labels=____.____())
plt.show()
Edit and Run Code