Menu
CoddyTech

Train and Test Sets

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 2 of 58.

A model can memorize the rows it studied, so testing it on them proves nothing. Hide some rows instead: the test set stands in for new data.

Twelve numbered rows in order are shuffled and dealt out: nine blue cells, 10 2 8 1 7 9 3 0 5, labelled train, and three orange cells, 6 11 4, labelled test

The first model in this journey studied 112 iris flowers and named 38. This line dealt them out:

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
print(X_train.shape, X_test.shape)

This prints (112, 4) (38, 4). The four parts always come in this order, and random_state=0 makes the shuffle repeat.

Train on the training set. Open the test set once, at the end.

challenge icon

Challenge

Easy

A night camera in a wood snapped 200 animals, each a fox or a badger, in camera.csv. Will a split give a fair test? The starter builds X and y and reads test_size.

  1. Split with that test_size and random_state=0.
  2. Print the sizes as below.
  3. Print the share of foxes in each part, in whole percents.

For another wood and 0.25:

Train: 90 snapshots
Test: 30 snapshots
Foxes: 52% of train, 47% of test

Try it yourself

import pandas as pd

# One row per snapshot from the night camera
camera = pd.read_csv("camera.csv")
X = camera[["minutes_after_sunset", "length_cm", "speed_kmh"]]
y = camera["animal"]
test_size = float(input())

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler