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.
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
EasyA 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.
- Split with that
test_sizeandrandom_state=0. - Print the sizes as below.
- 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 testTry 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler