SKLearner Home | About | Contact | Examples

Configure RandomForestClassifier "random_state" Parameter

The random_state parameter in scikit-learn’s RandomForestClassifier controls the randomness of the model, allowing for reproducible results across multiple runs.

Random Forest is an ensemble learning method that combines multiple decision trees, each trained on a random subset of the data and features. This randomness helps to reduce overfitting and improve generalization performance.

The random_state parameter is used to seed the random number generator, ensuring that the same splits and feature subsets are used each time the model is trained with the same random_state value.

By default, random_state is set to None, meaning the randomness is not controlled and results may vary across runs.

In practice, it is common to set random_state to an arbitrary fixed integer value (e.g., 42) to ensure reproducibility.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5,
                           n_redundant=0, random_state=42)

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train with different random_state values
random_state_values = [None, 42, 123]
accuracies = []

for rs in random_state_values:
    rf = RandomForestClassifier(n_estimators=100, random_state=rs)
    rf.fit(X_train, y_train)
    y_pred = rf.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)
    print(f"random_state={rs}, Accuracy: {accuracy:.3f}")

Running the example gives an output like:

random_state=None, Accuracy: 0.925
random_state=42, Accuracy: 0.920
random_state=123, Accuracy: 0.930

The key steps in this example are:

  1. Generate a synthetic binary classification dataset
  2. Split the data into train and test sets
  3. Train RandomForestClassifier models with different random_state values
  4. Evaluate the accuracy of each model on the test set

Some tips and heuristics for setting random_state:

Issues to consider:



See Also