Editor

Run Cross-Validation

Advanced · Write the code · from Evaluating ML Models: Cross-Validation, Metrics, and Overfitting

From the lesson: Evaluating ML Models: Cross-Validation, Metrics, and Overfitting

You've trained a model and it gets 95% accuracy. Should you celebrate? Not yet. That number might be lying to you. Maybe your test set was too easy. Maybe the model memorized the training data. Maybe accuracy is the wrong metric entirely.

Read the lesson

Use cross_val_score with 5-fold CV to evaluate a LogisticRegression(random_state=42). Print the mean accuracy rounded to 4 decimal places, then the standard deviation rounded to 4 decimal places on a new line.

from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=200, n_features=6,
random_state=42)
# Run 5-fold cross-validation and print mean + std

More Model Evaluation exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz