Training and Testing

What Is Training and Testing?

Machine learning models learn patterns from data and are evaluated on unseen data to measure how well they generalize.

01

Training Data — Used to teach the model patterns

02

Validation Data — Used to improve and tune the model

03

Test Data — Used for final performance evaluation

Why Do We Split Data?

Evaluating a model on the same data used for training can produce misleading results. Data splitting provides a realistic estimate of real-world performance.

Bad Evaluation

Train and test on the same data.

Result: Artificially high accuracy.

Good Evaluation

Train on one dataset, test on unseen data.

Result: Realistic performance estimate.

Training Data

Training data teaches the model by providing examples from which it can learn patterns and relationships.

Training Example
  • 1500 sq ft → $300,000
  • 2000 sq ft → $400,000
  • 2500 sq ft → $500,000

What Happens During Training?

01

Model receives input data

02

Model makes a prediction

03

Calculate error between prediction and actual answer

04

Update model parameters to reduce error

05

Repeat thousands or millions of times

Training Loss

Training loss measures prediction error during learning.

Loss Over Time

Beginning — Loss = 5.0

Later — Loss = 0.8

Lower loss indicates better learning.

Validation Data

Validation data is used during development to evaluate performance and guide model improvements. The model never learns from validation data.

Overfitting Warning

Training Accuracy — 99%

Validation Accuracy — 70%

A large gap suggests the model is memorizing training data.

Hyperparameter Tuning

Hyperparameter Examples
  • Learning rate
  • Batch size
  • Number of layers
  • Number of trees
  • Regularization strength

Early Stopping

Validation performance can be monitored to stop training before overfitting begins.

Example

Epoch 10 → 90%

Epoch 20 → 92%

Epoch 30 → 89%

Best model: saved at Epoch 20.

Test Data

The test set is used only once training and tuning are complete. It provides the final estimate of model performance.

Final Evaluation

Test Accuracy — 95%

Represents expected real-world performance.

Typical Data Splits

SplitTrainingValidationTesting
80/2080%-20%
70/15/1570%15%15%
60/20/2060%20%20%

Generalization

Generalization measures how well a model performs on new, unseen data.

Good Generalization

Training: 92%

Validation: 90%

Testing: 89%

Similar values across all three datasets.

Poor Generalization

Training: 99%

Validation: 65%

Testing: 63%

Large gap suggests overfitting.

Overfitting

Signs of Overfitting
  • Excellent training performance
  • Poor validation performance
  • Poor test performance

Underfitting

Signs of Underfitting
  • Poor training performance
  • Poor validation performance
  • Poor test performance

Cross-Validation

5-Fold Cross Validation

Fold 1 → Validate, Folds 2-5 → Train

Fold 2 → Validate, Folds 1,3,4,5 → Train

Repeat until all folds have been used.

Benefits — Better evaluation, reduced bias, more reliable estimates

Training vs Validation vs Testing

DatasetPurposeUpdates Model?
TrainingLearn patternsYes
ValidationTune modelNo
TestingFinal evaluationNo

Data Leakage

Leakage Examples
  • Using test data during training
  • Using future information as features
  • Preprocessing the full dataset before splitting

Result — Unrealistically high accuracy

Best Practices

Best Practices
  • Keep training, validation, and test data separate
  • Never train on test data
  • Use validation data for tuning
  • Monitor overfitting
  • Use cross-validation when appropriate
  • Evaluate using multiple metrics
  • Prevent data leakage

Practice Prompt

A company has 50,000 customer records and wants to build a machine learning model. Explain how the dataset could be divided into training, validation, and test sets and describe the purpose of each dataset.

Need Help?

Ask the AI if you need help understanding training data, validation data, testing, overfitting, or model evaluation.