Code / AI & ML Basics / Unit 1: Data: what models learn from

Train and test ट्रेन और टेस्ट

Lesson 3 of 13 · about 10 minutes · Keep some data hidden to check the model honestly.

⚡ 0 XP

Step 1 of 1

Don’t peek at the exam paper

If a teacher gives you the exact exam questions to practise, scoring 100% proves nothing. Models are the same.

So we split the data:

  • training set: the model learns from these
  • test set: hidden during training, used only to check it at the end

A common split is 80% / 20%. Usually rows are shuffled randomly first; here we use a simple, repeatable rule so everyone gets the same answer: every 5th row goes to the test set (rows 5, 10, 15… — that is index 4, 9, 14…).

Example
for (let i = 0; i < 10; i++) {
  const goesToTest = (i + 1) % 5 === 0;
  console.log("row", i + 1, goesToTest ? "test" : "train");
}

🎯 Challenge: Split the data +15 XP

Write trainTestSplit(rows) that returns an object { train, test }. Every 5th row (index 4, 9, 14…) goes into test; all others go into train, keeping their order. Don’t use Math.random — the split must be the same every time.

Try twice to unlock the solution (0/2)

script.jsCtrl ⏎
Console

Output from console.log appears here.

Checklist

Press Run to check your code.

  • Rows 5 and 10 go to test
  • The other rows go to train, in order
  • Works on rows of objects; no Math.random
Lesson notes (text version)

1. Don’t peek at the exam paper

If a teacher gives you the exact exam questions to practise, scoring 100% proves nothing. Models are the same.

So we split the data:

  • training set: the model learns from these
  • test set: hidden during training, used only to check it at the end

A common split is 80% / 20%. Usually rows are shuffled randomly first; here we use a simple, repeatable rule so everyone gets the same answer: every 5th row goes to the test set (rows 5, 10, 15… — that is index 4, 9, 14…).

                for (let i = 0; i < 10; i++) {
  const goesToTest = (i + 1) % 5 === 0;
  console.log("row", i + 1, goesToTest ? "test" : "train");
}
              

Challenge: Split the data

Write trainTestSplit(rows) that returns an object { train, test }. Every 5th row (index 4, 9, 14…) goes into test; all others go into train, keeping their order. Don’t use Math.random — the split must be the same every time.

Search by task, format or tool name.