Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Supervised Learning with scikit-learn

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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()
Code bewerken en uitvoeren