Đại học Kinh tế Quốc dân – National Economics UniversityMICRO4ASIA
🏆 Arena
Sign inSign up
MICRO4ASIAThe six micro-credentials

Courses are delivered on the Limio learning platform. Limio

← Fundamental of Machine Learning

You are viewing a public preview

Sign in to save your progress, do assignments, join discussions and get feedback.

Sign in
Module 3: Model Evaluation and Optimization

3.P2 Splitting Train/Test with train_test_split

Print / Save as PDF

3.P2 Splitting Train/Test with train_test_split

After this unit you will: be able to read the train_test_split cell and know what test_size decides.

Key Points

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(
    x, y,
    test_size=0.2,
    random_state=1009
)
  • The input is the whole x and y; the output is four objects in the order x_train, x_test, y_train, y_test.
  • test_size=0.2: 20% of the rows go to the test set, and the remaining 80% go to the training set.
  • With 768 rows, the test set has about 154 rows (768 × 0.2 = 153.6, which sklearn rounds up).
  • Split the data before normalizing and training, so the test set is always data the model has not seen.

Study notes

By the end of this lesson you can:

  • Use train_test_split to divide x and y into training and test sets
  • Explain what test_size=0.2 does and estimate the size of each set
  • Unpack the four returned objects in the correct order
  • Explain why a fixed test set is needed to compare models fairly, and how random_state provides it

Key idea: train_test_split randomly holds back part of the data as a test set; fixing random_state keeps that test set identical across runs, so scores from different trainings can be compared fairly.

Why split?

The model must be judged on data it has never seen. So before preprocessing or training, we split the rows into a training set (to learn from) and a test set (to evaluate on). You could do this by hand with pandas, but scikit-learn's train_test_split makes it much easier.

The function call

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(
    x, y,
    test_size=0.2,
    random_state=1009
)
  • Inputs: the whole x and y.
  • test_size=0.2: 20% of the rows go to the test set; the other 80% form the training set.
  • Outputs: four objects, always in this order: x_train, x_test, y_train, y_test.

For diabetes.csv with 768 rows: 768 x 0.2 = 153.6, which scikit-learn rounds up to 154 test rows, leaving 614 training rows.

The split keeps features and labels aligned: row 498 of x goes to the same set as row 498 of y, so every patient's measurements stay matched with their own Outcome. The rows are also shuffled before splitting by default, which matters when a file is sorted (for example, all patients with diabetes at the end).

The labels used to train the model are in y_train; y_test is kept aside to check predictions later.

Why random_state?

Each call to train_test_split splits the data randomly. The function guarantees the proportions (20% / 80%), but not which rows end up where. Call it 10 times and you get 10 different test sets.

That is a real problem for comparison. Suppose the first training run scores 80% accuracy and the second 81%. If the two runs were evaluated on different test sets, you cannot say the second model is better: 81% on one test set is no guarantee of improvement over 80% on another.

To benchmark models consistently, we need a fixed test set. Setting random_state to a number does that: as long as you use the same number (here 1009), every run produces the same split. The split is still random, but the randomness is fixed. Because the test set is fixed, the training set is fixed too.

Worked example

Split the diabetes data and check the sizes.

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(
    x, y, test_size=0.2, random_state=1009)

print(x_train.shape, x_test.shape)
print(y_train.shape, y_test.shape)
print(len(x_train) + len(x_test))

Expected output:

(614, 8) (154, 8)
(614,) (154,)
768

Step by step:

  1. 20% of 768 is 153.6, rounded up to 154 test rows.
  2. The remaining 614 rows are for training.
  3. Features and labels are split consistently: x_train and y_train have the same number of rows, and so do x_test and y_test.
  4. The two parts add up to the original 768 rows: no row is lost or duplicated.

A quick self-check: run the cell twice and print x_test.index[:5]; with random_state=1009 the indices are identical each time.

Common mistakes

  • Unpacking in the wrong order. Python assigns by position; writing x_train, y_train, x_test, y_test = ... silently puts the wrong objects in the wrong variables.
  • Splitting after scaling or training. Split first, so the test set stays completely unseen.
  • Reading test_size as the training share. test_size=0.2 means 20% for testing, not 20% for training.
  • Comparing scores from different random splits. Without a fixed random_state, a better score may just be an easier test set.

Summary

  • train_test_split(x, y, test_size=0.2, random_state=1009) splits rows into training and test sets.
  • It returns four objects in the order x_train, x_test, y_train, y_test.
  • With 768 rows and test_size=0.2: 154 test rows and 614 training rows.
  • The split is random; random_state fixes it so every run uses the same test set.
  • A fixed test set makes comparisons between trainings fair.

Flashcards · 3.P2 Splitting Train/Test with train_test_split

8 words
WordMeaningExample
  • train_test_split()

    scikit-learn function that randomly splits features and labels into training and test sets.

    train_test_split(x, y, test_size=0.2, random_state=1009)

  • test_size

    The proportion (or number) of rows reserved for the test set.

    test_size=0.2 -> 20% test, 80% train

  • Return order

    train_test_split returns x_train, x_test, y_train, y_test, always in this order.

    x_train, x_test, y_train, y_test = train_test_split(x, y)

  • Training set

    The rows the model learns from.

    614 rows of diabetes.csv

  • Test set

    Rows held back to evaluate the model on data it has never seen.

    154 rows of diabetes.csv

  • y_train

    The labels used together with x_train to train the model.

    model.fit(x_train, y_train)

  • random_state

    A seed that fixes the random split, so every run produces the same training and test sets.

    random_state=1009

  • Fair benchmarking

    Comparing models on exactly the same test set so score differences reflect the models.

    80% vs 81% only comparable on the same test set.