Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python sklearn random_state Reproducibility

Learn how random_state in scikit-learn controls randomness and how to set it for data splits, estimators, pipelines, and cross-validation to make experiments reproducible.

~4 min readPython
#scikit-learn#random_state#reproducibility#deterministic
Illustration of a random seed controlling reproducible results in scikit-learn experiments

When you run a scikit-learn model twice and get different results, the cause is usually an unseeded random number generator. The random_state parameter controls this randomness and is the key to reproducible experiments. Setting it explicitly for the operations that accept it makes them deterministic, so the same code can produce the same output across runs.

This article explains how random_state works in scikit-learn, where it appears, and how to use it consistently across data splits, estimators, and pipelines.

What random_state Controls in scikit-learn

Many scikit-learn classes and functions accept a random_state parameter. It seeds the internal random number generator used for operations like:

  • Shuffling data in train_test_split
  • Initializing weights in neural networks
  • Selecting random feature subsets
  • Sampling in bagging and boosting
  • Starting centroids in k-means

When random_state is set to an integer, scikit-learn creates a numpy.random.RandomState instance with that seed. Every call with the same integer produces the same sequence of random numbers, as long as the algorithm and data remain unchanged.

If random_state is None, scikit-learn uses the global RandomState instance from numpy.random. This means results depend on the state of the global generator, which can change across runs and even across library versions.

Using random_state in train_test_split

The most common place developers need reproducibility is splitting data. Without a fixed random_state, train_test_split shuffles the data differently each time.

python
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=42 )

The integer 42 is a convention, but any integer works. The important point is that the same split is produced every time the code runs, given the same input data.

If you omit random_state, the split changes on each execution. This can silently alter model evaluation results and make comparisons between experiments meaningless.

Setting random_state for Estimators

Most estimators that involve randomness accept random_state in their constructor. For example:

python
from sklearn.ensemble import RandomForestClassifier model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train, y_train)

Setting it here ensures that the forest's bootstrap sampling and feature selection are deterministic. Without it, two runs of fit on the same data can produce different trees and therefore different predictions.

Some estimators, such as LinearRegression, do not use randomness and do not accept a random_state parameter. Check the documentation for each estimator to see whether it applies.

Global Seeding with numpy and random

Sometimes you want to control randomness across the entire script, including operations outside scikit-learn. You can seed the global numpy random state and the Python random module:

python
import numpy as np import random np.random.seed(42) random.seed(42)

This affects any code that uses these global generators. However, scikit-learn's random_state parameter takes precedence when it is set explicitly. If you set both, the explicit random_state wins for that particular call.

Global seeding is useful for quick scripts, but it is fragile. If a library internally uses its own random state or if you change the order of operations, results may still vary. Explicit random_state values are more reliable for reproducible experiments.

Reproducibility in Pipelines and Cross-Validation

When you combine multiple steps in a Pipeline, each step that uses randomness needs its own random_state. For example:

python
from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.ensemble import RandomForestClassifier pipeline = Pipeline([ ('scaler', StandardScaler()), ('clf', RandomForestClassifier(random_state=42)) ])

Here StandardScaler is deterministic, so only the classifier needs a seed. If you add another randomized step, such as PCA with svd_solver='randomized', give that step its own random_state.

For cross-validation, if you shuffle the data, set shuffle=True and random_state on a KFold or StratifiedKFold object to get the same folds across runs:

python
from sklearn.model_selection import StratifiedKFold cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

Pass cv to cross_val_score or GridSearchCV to ensure the same data partitions are used for each evaluation.

Common Pitfalls and Misconceptions

One misunderstanding is that setting random_state on an estimator makes the entire training process fully deterministic. This is only true if all other sources of randomness are also controlled. For example, if you use a train_test_split without a seed, the data itself changes, so the model will still differ.

Another pitfall is reusing the same RandomState instance across multiple calls. If you pass a RandomState object instead of an integer, scikit-learn will use it and advance its state. This can produce different results on subsequent calls, even though the same object is passed. To get identical results each time, pass an integer.

Maintaining Reproducibility Across Versions

Scikit-learn does not guarantee that a fixed random_state produces the same output across versions. Changes to algorithms, data handling, or the underlying numpy version can alter results. For critical applications, record the exact versions of scikit-learn, numpy, and other dependencies.

A practical approach is to store the seed and the environment description together with your results. This makes it possible to reproduce an experiment even if the library updates later.

When you upgrade dependencies, rerun your experiments and compare results. If they differ, check whether the change is due to randomness or to an actual algorithm modification.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.