MACHINE LEARNING • LESSON 6

Splitting Data Correctly

Before training a machine learning model, we need to separate our dataset into different parts. This allows us to train the model on one part and check how well it works on data it has not seen before.

THE SIMPLEST DEFINITION

Split your data so the model learns from one part and is tested on another.

The most important rule is simple: the model should not get access to the test data while it is learning.

01

Why Do We Split the Data?

Imagine you want to test a student before an exam. If you give the student the exact exam questions while studying, the final score will not tell you whether the student actually understands the subject.

SIMPLE ANALOGY Training questions → Learn Exam questions → Test

Machine learning works in a similar way.

The model learns from training data. We then use separate data to check whether it can make good predictions on information it has not seen before.

02

Training Data and Test Data

Suppose we have 1,000 customer records.

COMPLETE DATASET 1,000 records
TRAINING DATA 800 records

Used to learn

TEST DATA 200 records

Used to evaluate

A common starting point is an 80/20 split:

80% Training
20% Testing

80/20 is not a strict rule. The appropriate split depends on the size and nature of the dataset.

03

The Important Rule: Keep Test Data Separate

The test data should represent data that the model has not seen during training.

TRAINING DATA Model learns
TRAINED MODEL Ready for evaluation
TEST DATA Model has not seen it

This gives us a more honest estimate of how the model may perform on new data.

If the model has already seen the test examples during training, the test result is no longer a fair test.
04

Why Random Splitting Is Common

Suppose our dataset is ordered like this:

Customers from January
Customers from February
Customers from March
Customers from April

If we simply take the first 80% for training and the last 20% for testing, we might accidentally create groups that are very different from each other.

For many ordinary datasets, randomly splitting the records gives a better mixture of examples in the training and test sets.

Random splitting helps prevent the split from depending only on the original order of the dataset.

However, random splitting is not appropriate for every problem. Time-series data, for example, often needs to respect chronological order.

05

A Common Mistake: Data Leakage

One of the biggest problems when splitting data is data leakage.

Data leakage happens when information from data that should be unseen accidentally influences the training process.

WRONG Scale the entire dataset first

Then split it into training and test data.

BETTER Split first

Fit preprocessing using training data, then transform the other data.

For example, if you are using feature scaling, do not calculate the scaling parameters using the entire dataset before the split.

The test set should remain unseen during the learning and preprocessing process.

06

Train, Validation, and Test

For simple projects, you may use only training and test data.

When developing and tuning a model, however, we often use three separate parts:

TRAINING Learn

Used to train the model.

VALIDATION Tune

Used to compare settings and improve the model.

TEST Final Check

Used for final evaluation.

You will study the detailed purpose of these three sets in the earlier Lesson 4 topic, Training, Validation, and Test Data.

07

Splitting Data With Python

Scikit-learn provides a simple function called train_test_split().

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.2,
    random_state=42
)

Here is what the important parts mean:

X Input features
y Target / label
test_size=0.2 20% goes to the test set
random_state=42 Makes the random split reproducible

So if we have 1,000 records, approximately 800 will be used for training and 200 for testing.

08

A Complete Example

Imagine we want to predict whether a customer will buy a product.

Age Orders Income Purchased
25 5 ₹50K Yes
32 2 ₹40K No
28 7 ₹80K Yes
45 1 ₹60K No

The input features are:

Age
Orders
Income

The label is:

Purchased → Yes / No

We split the complete dataset before training the model.

COMPLETE DATA 1,000 customers
TRAIN 800 customers

Learn patterns

TEST 200 customers

Check predictions

REMEMBER THIS

Never Let the Model Learn From the Test Set.

Split the data before training. Use the training data to learn, and keep the test data separate for evaluating how well the model works on unseen examples.

QUICK CHECK

Which Approach Is Correct?

Train on 80% and test on 20% ✅ Common approach
Train on the complete dataset and test on the same data ❌ Not a fair evaluation
Split first, then train ✅ Keeps the test data separate
Answer

The correct basic workflow is to split the dataset, train the model using the training data, and evaluate it using separate test data. The test data should not influence the training process.

NEXT TOPIC

Preparing a Real Dataset

Now that we understand cleaning, encoding, scaling, and splitting, we can put these preparation steps together and prepare a real dataset for machine learning.