START HERE · ABOUT 15 MINUTES
Your first machine learning exercise.
A model needs a fair test. Learn how to give it one, then write a small function that prepares its data.
You need basic Python: variables, lists, and functions. We’ll explain the NumPy you use. No machine learning experience or installation needed.
- 01Understand the split
- 02Write a few lines
- 03Check your answer
THE IDEA
Learn from some examples. Test on others.
Imagine a bike shop with a record of the weather and how many bikes people rented. A model can learn a pattern from those examples, such as more rentals on warm, dry days.
| Temperature | Rain | Bikes rented |
|---|---|---|
| 18°C | No | 40 |
| 25°C | No | 72 |
| 15°C | Yes | 18 |
Temperature and rain are the features: the information the model gets. Bikes rented is the target: the answer we want it to predict. In code, we call the features X and the targets y.
Checking a model only on examples it learned from is like taking an exam with the answers already memorized. Before training, set aside some examples as a test set. Train on the rest, then measure how well the model predicts the held-out answers.
What does a 20% test split look like?
With 10 examples, keep 2 for testing and 8 for training. In this exercise, shuffle the row numbers first so the split does not depend on the original order.
- 4Test
- 6Test
- 2Train
- 7Train
- 3Train
- 5Train
- 9Train
- 0Train
- 8Train
- 1Train
Quick check: can we train on those two test examples too?
No. Then they would no longer test the model on unseen examples. Keep the test set aside while you train and make improvements; use it for the final evaluation.
Random splitting suits independent examples. For forecasting future values, keep later observations for testing instead of shuffling across time.
Keep each example with its answer.
NumPy stores a table as an array. Give it an array of row numbers to select those rows in that order. Use the same numbers for X and y, so the rainy day still belongs with its 18 rentals.
import numpy as np
# Each row is [temperature, rain].
X = np.array([[18, 0], [25, 0], [15, 1]])
y = np.array([40, 72, 18]) # bikes rented
# Select the third row, then the first (counting from 0).
rows = np.array([2, 0])
print(X[rows])
# [[15 1]
# [18 0]]
print(y[rows]) # [18 40]The exercise adds one step: generate shuffled row numbers using a seed. The same seed gives the same shuffle, so you can reproduce your result. The problem statement gives you that line.
YOUR TURN
Make your first split.
Implement train_test_split. You’ll return four arrays: training features, test features, training targets, and test targets.
- Open the exercise and replace
passin the starter function. Follow the split convention in the description. - Select Check to try the visible tests. If one fails, read its message and adjust your code. The hints are there to help.
- Select Submit to run every test. A passing submission marks the exercise as solved.
Runs in your browser. The first check may take a moment while Python loads. See how the editor works.
Put the split to work.
Once your exercise passes, the bike demand project uses a train/test split to fit a model and compare its predictions with a simple average.
Prefer a full path? Explore the foundation courses →