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 = train_test_split(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 proper evaluation to prevent overfitting.
Complete the code to train a model on the training data.
from sklearn.linear_model import LogisticRegression model = LogisticRegression() model.[1](X_train, y_train)
The fit method trains the model using the training data.
Fix the error in the code to evaluate the model correctly on test data.
accuracy = model.[1](X_test, y_test) print(f"Test accuracy: {accuracy:.2f}")
The score method returns the accuracy of the model on test data, which is the correct way to evaluate performance.
Fill both blanks to create a dictionary of accuracy scores for training and testing data.
scores = {
'train_accuracy': model.[1](X_train, y_train),
'test_accuracy': model.[2](X_test, y_test)
}The score method calculates accuracy on both training and testing data to compare performance and detect overfitting.
Fill all three blanks to perform cross-validation and print the mean accuracy.
from sklearn.model_selection import [1] scores = cross_val_score(model, X, y, cv=[2]) print(f"Mean CV accuracy: {scores.[3]():.2f}")
cross_val_score performs cross-validation, cv=5 sets 5 folds, and mean() calculates the average accuracy to evaluate model generalization and prevent overfitting.
