Why do we set a random state in machine learning models?
You may already use random state in your machine learning models. Do you know that random state is a model hyperparameter used to control the randomness involved in machine learning models?
In Scikit-learn, the random state hyperparameter is denoted by random_state . It usually takes one of the following values.
- None: This is the default value. This allows the function to use the global random state instance from np.random . If you call the same function multiple times with random_state=None , that function will produce different results across different executions.
- int: We can use an integer for random_state . Yes! We can use any integer including 0, but not negative ones, only positive integers. The most popular integers are 0 and 42. When we use an integer for random_state , the function will produce the same results across different executions. The results are only changed if we change the integer value.
Using the random state — A classic example
Let’s see how random state works with an example. For this, we use Scikit-learn’s train_test_split() function and LinearRegression() function. The train_test_split() function is used to split the dataset into train and test sets. By default, the function shuffles the data (with shuffle=True ) before splitting. The random state hyperparameter in the train_test_split() function controls the shuffling process.
With random_state=None , we get different train and test sets across different executions and the shuffling process is out of control.
With random_state=0 , we get the same train and test sets across different executions. With random_state=42 , we get the same train and test sets across different executions, but in this time, the train and test sets are different from the previous case with random_state=0 .
The train and test sets directly affect the model’s performance score. Because we get different train and test sets with different integer values for random_state in the train_test_split() function, the value of the random state hyperparameter indirectly affects the model’s performance score.
sklearn.model_selection .train_test_split¶
Split arrays or matrices into random train and test subsets.
Quick utility that wraps input validation, next(ShuffleSplit().split(X, y)) , and application to input data into a single call for splitting (and optionally subsampling) data into a one-liner.
Parameters : *arrays sequence of indexables with same length / shape[0]
Allowed inputs are lists, numpy arrays, scipy-sparse matrices or pandas dataframes.
test_size float or int, default=None
If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is set to the complement of the train size. If train_size is also None, it will be set to 0.25.
train_size float or int, default=None
If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size.
random_state int, RandomState instance or None, default=None
Controls the shuffling applied to the data before applying the split. Pass an int for reproducible output across multiple function calls. See Glossary .
shuffle bool, default=True
Whether or not to shuffle the data before splitting. If shuffle=False then stratify must be None.
stratify array-like, default=None
If not None, data is split in a stratified fashion, using this as the class labels. Read more in the User Guide .
Returns : splitting list, length=2 * len(arrays)
List containing train-test split of inputs.
New in version 0.16: If the input is sparse, the output will be a scipy.sparse.csr_matrix . Else, output type is the same as the input type.
>>> import numpy as np >>> from sklearn.model_selection import train_test_split >>> X, y = np.arange(10).reshape((5, 2)), range(5) >>> X array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]) >>> list(y) [0, 1, 2, 3, 4]
>>> X_train, X_test, y_train, y_test = train_test_split( . X, y, test_size=0.33, random_state=42) . >>> X_train array([[4, 5], [0, 1], [6, 7]]) >>> y_train [2, 0, 3] >>> X_test array([[2, 3], [8, 9]]) >>> y_test [1, 4]
>>> train_test_split(y, shuffle=False) [[0, 1, 2], [3, 4]]