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.
Questo esercizio fa parte del corso
Supervised Learning with scikit-learn
Istruzioni dell'esercizio
- Write a for loop using
modelas the iterator, andmodel.values()as the iterable. - Perform cross-validation on the training features and the training target array using the model, setting
cvequal to theKFoldobject. - 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.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
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()