machine learning25 min

Train/Test Split

Splitting data to honestly evaluate how well a model generalizes

0/9Not Started

Why This Matters

Imagine studying for an exam by memorizing every answer in the practice test — then being tested with the exact same questions. You would score 100%, but it would not prove you understood the material. This is the core problem in machine learning: a model that performs perfectly on its training data may fail badly on new data.

The train/test split is how we get an honest measurement of a model's real-world performance. It is one of the simplest yet most important concepts in all of machine learning. Skip it and you will have no idea whether your model actually works.

Define Terms

Visual Model

Full Dataset
Train 70%
Val 15%
Test 15%
Split
Train Model
Tune Params
Final Eval

The full process at a glance. Click Start tour to walk through each step.

Data is split into training, validation, and test sets to ensure honest evaluation of model performance.

Code Example

Code
// Splitting a dataset into train and test sets

function trainTestSplit(data, testRatio = 0.2) {
  // Shuffle the data to avoid ordering bias
  const shuffled = [...data].sort(() => Math.random() - 0.5);
  const splitIndex = Math.floor(shuffled.length * (1 - testRatio));
  return {
    train: shuffled.slice(0, splitIndex),
    test: shuffled.slice(splitIndex),
  };
}

// Example dataset
const dataset = [
  { x: 1, y: 2 }, { x: 2, y: 4 }, { x: 3, y: 6 },
  { x: 4, y: 8 }, { x: 5, y: 10 }, { x: 6, y: 12 },
  { x: 7, y: 14 }, { x: 8, y: 16 }, { x: 9, y: 18 },
  { x: 10, y: 20 },
];

const { train, test } = trainTestSplit(dataset, 0.2);
console.log("Train size:", train.length); // ~8
console.log("Test size:", test.length);   // ~2
console.log("Test examples:", test);

Interactive Experiment

Try these exercises with the code above:

  • Change the test ratio from 0.2 to 0.5. What are the tradeoffs of a larger test set?
  • Run the split multiple times. Notice the results are different each time — why does shuffling matter?
  • Implement a three-way split: train (70%), validation (15%), test (15%).
  • What would happen if you sorted your data by label before splitting without shuffling?

Quick Quiz

Coding Challenge

K-Fold Cross-Validation

Write a function called `kFoldSplit` that takes an array and a number k, and returns an array of k objects each containing `train` and `test` arrays. Each fold uses a different 1/k portion as the test set and the rest as training. Do not shuffle — just split sequentially.

Loading editor...

Real-World Usage

Train/test splitting is a non-negotiable step in every ML pipeline:

  • Kaggle competitions: Participants submit predictions on a hidden test set they never see during development.
  • A/B testing at tech companies: Models are validated on held-out data before being deployed to real users.
  • Medical AI: Regulatory agencies require validation on independent patient cohorts before approving diagnostic models.
  • Financial modeling: Backtesting on historical data that the model was not trained on simulates real trading conditions.
  • Production ML systems: Shadow deployment evaluates new models on live traffic before switching from the old model.

Connections