0Pricing
Python Academy · Lesson

Model Evaluation and Cross-Validation

Evaluate models with accuracy, F1, ROC-AUC, and k-fold CV.

Train/Validation/Test Split

Use a three-way split: train to fit, validation to tune hyperparameters, test to report final performance. Never use the test set during development.

from sklearn.model_selection import train_test_split
import numpy as np

X = np.random.rand(1000, 10)
y = (X[:,0] > 0.5).astype(int)

X_tr, X_rest, y_tr, y_rest = train_test_split(X, y, test_size=0.3)
X_val, X_te, y_val, y_te = train_test_split(X_rest, y_rest, test_size=0.5)

Accuracy Score

accuracy_score is the fraction of correct predictions. Use it for balanced classes; for imbalanced data, prefer F1 or AUC.

from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

X, y = make_classification(random_state=0)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, random_state=0)
model = LogisticRegression().fit(X_tr, y_tr)
print("Accuracy:", accuracy_score(y_te, model.predict(X_te)))

All lessons in this course

  1. The scikit-learn API: fit, transform, predict
  2. Linear Models: Regression and Classification
  3. Tree-Based Models: Decision Trees and Random Forests
  4. Model Evaluation and Cross-Validation
← Back to Python Academy