Scikit-learn Train Test Split — random_state and shuffle
The random_state and shuffle are very confusing parameters. Here we will see what’s their purposes.

First let’s import the modules with the below codes and create x, y arrays of integers from 0 to 9.

import numpy as np

from sklearn.model_selection import train_test_split x=np.arange(10)

y=np.arange(10) print(x)

[0 1 2 3 4 5 6 7 8 9]

When random_state is None and shuffle is True, each execution will result in a different set of train and test data.

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=None, shuffle=True) print(x_train) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=None, shuffle=True) print(x_train)

[6 7 3 9 4 5 2 0]

[6 2 1 7 4 9 8 3]

When random_state is fixed integer and shuffle is True, the set of train and test data will be the same for each execution.

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=7, shuffle=True) print(x_train) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=7, shuffle=True) print(x_train)

[0 2 1 9 7 3 6 4]

[0 2 1 9 7 3 6 4]

When we use different random_state, the output will change.

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=42, shuffle=True) print(x_train) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=7, shuffle=True) print(x_train)

[5 0 7 2 9 4 3 6]

[0 2 1 9 7 3 6 4]

As you can see the output changes when we use a different random state number but it has resulted in the same train data when we have used random_state=7 thrice.

Till now the shuffle parameter was True for all cases. But what happens when we set shuffle as False?

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=None, shuffle=False) print(x_train) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=None, shuffle=False) print(x_train) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=7, shuffle=False) print(x_train) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=42, shuffle=False) print(x_train)

[0 1 2 3 4 5 6 7]

[0 1 2 3 4 5 6 7]

[0 1 2 3 4 5 6 7]

[0 1 2 3 4 5 6 7]

As you can see when shuffle is False then no matter what we set for random_state, it will linearly divide the data into train and test data.

Why random_state=42?

You may have seen multiple times that most commonly 42 is used for random_state. Is there any reason for that? And the answer is NO. Python has used 42 as random_state in their documentation for example and many people have copied from the example code snippet, some people may have used it subconsciously and some may have used it lazily. So there is no particular reason for using 42 as random_state number and you are free to use any number you want.
[]
['Python', 'Data Science', 'Machine Learning', 'Sklearn', 'Scikit Learn']