Complete Evaluation Pipeline
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 lessonSplit data 80/20, then use cross_val_score (5-fold, scoring='f1') on the training set to evaluate a LogisticRegression(random_state=42). Print the mean CV F1 score rounded to 4 decimal places. Then train on full training set, predict on test set, and print the test ROC-AUC rounded to 4 decimal places on a new line.
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.datasets import make_classification
from sklearn.metrics import roc_auc_score
X, y = make_classification(n_samples=300, n_features=6,
random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
# 1. Cross-validate on training set (F1 scoring)
# 2. Train on full training set, compute test ROC-AUC
More Model Evaluation exercises
- Run Cross-ValidationAdvanced
- Detect OverfittingAdvanced
- Compute Precision, Recall, and F1Advanced
- Find Best HyperparametersAdvanced
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz