A model that has learned your training data by heart tells you nothing about new data. So you keep part of your dataset aside and test on that instead.

That is why almost every machine learning tutorial starts by splitting the data.

That is why almost every machine learning tutorial starts by splitting the data.

In Machine Learning, this applies to supervised learning algorithms.

Training and test data

In the real world we have all kinds of data like financial data or customer data.

An algorithm should make new predictions based on new data.

You can simulate this by splitting the dataset in training and test data.

supervised learning

Code example

The module sklearn comes with some datasets. One of these dataset is the iris dataset.
We load this data using the method load_iris() and then get the data and labels (class of flower).

Then the data is split randomly using the method train_test_split.
As parameters we specify the train_size and test_size, both at 50%.

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

# Load dataset.
iris = load_iris()
X, y = iris.data, iris.target

# split data into training and test data.
train_X, test_X, train_y, test_y = train_test_split(X, y,
train_size=0.5,
test_size=0.5,
random_state=123)
print("Labels for training and testing data")
print(train_y)
print(test_y)

How the split works

  • from sklearn.model_selection import train_test_split
  • train_test_split(X, y, test_size=0.2) gives back four things: X_train, X_test, y_train, y_test.
  • test_size=0.2 means 20 percent goes to the test set.
  • random_state fixes the seed, so you get the same split every run.
  • Fit the model on the training set only, then score it on the test set.

If the training score is high and the test score is low, the model is overfitting. It memorised instead of learning, and it will do badly on data it has never seen.

Want more practice on this? Practise this on PyChallenge, there are short browser exercises you can run right after reading.