Student Performance Prediction · Phase 2 · Lesson 3 of 3
Feature Preparation and Avoiding Data Leakage
Select and encode input features available at the prediction time, split training and test data correctly, and understand why leakage produces falsely excellent models.
What you need
A computer with a web browser and a free Google account for Google Colab, or Python with Jupyter installed locally. Libraries: pandas, scikit-learn and matplotlib (all free).
In this lesson you will
- Prepare features available at prediction time
- Split data correctly and prevent leakage
Key concepts
featuredata leakagetrain-test splitstratificationone-hot encodingscaling
The lesson
Data leakage happens when the model sees information it would not have at prediction time: term-3 attendance, the final exam score itself, or a 'support received' column recorded after the exam. Leaky models look excellent in testing and fail in real use.
Split the data into training and test sets before any learning step, with stratification so both sets have the same share of students below 50%. Keep the test set untouched until final evaluation.
Encode categories (school, region) with one-hot encoding and, for models that need it, scale numeric features using statistics from the training set only.
Step by step
- List each column and mark whether it is known at the end of term 2.
- Remove leaky columns.
- Make a stratified 80/20 train-test split with a fixed random seed.
- Encode categories and scale numbers using the training set only.
Practice activity
Build the modelling table and a feature list that explains why each feature is allowed.
Expected result: A leak-free training and test set, and a documented feature list.
Common mistakes
- Scaling with statistics from the whole dataset, including the test set
- Looking at test results repeatedly while tuning
Check your understanding
-
Give an example of data leakage in this project.
Show answer
Using term-3 attendance or the final exam score as an input.
-
Why stratify the split?
Show answer
So both sets have the same proportion of students below 50%.
Further learning
Projects are instructor-guided: an instructor sets milestones, checks your work and gives feedback, online or in person. Self-study of the public lessons does not lead to a certificate. Enrolment and fees follow the Academy's existing policies — contact the Academy.