Complete the code to split data into training and testing sets.
from sklearn.model_selection import [1] X_train, X_test, y_train, y_test = [1](X, y, test_size=0.2, random_state=42)
The train_test_split function splits data into training and testing sets, which is essential for comparing models.
Complete the code to perform 5-fold cross-validation on a model.
from sklearn.model_selection import cross_val_score scores = cross_val_score(model, X, y, cv=[1])
Using cv=5 means the data is split into 5 parts for cross-validation, a common choice for model comparison.
Fix the error in the code to calculate accuracy score after predictions.
from sklearn.metrics import accuracy_score accuracy = accuracy_score(y_true, [1])
The accuracy_score function compares true labels y_true with predicted labels y_pred to calculate accuracy.
Fill both blanks to create a dictionary of mean cross-validation scores for each model.
cv_scores = {model_name: cross_val_score(model, X, y, cv=5).[1]() for model_name, model in [2].items()}We use mean() to get the average score from cross-validation. The models are stored in a dictionary named models.
Fill all three blanks to select the best model based on highest mean cross-validation score.
best_model_name = max(cv_scores, key=lambda k: cv_scores[[1]]) best_score = cv_scores[[2]] print(f"Best model: [3] with score {best_score:.2f}")
The max function uses k to check keys in cv_scores. Then we use best_model_name to get the best score and print the model name.
