SKLearner Home | About | Contact | Examples

Configure AdaBoostClassifier "random_state" Parameter

The random_state parameter in scikit-learn’s AdaBoostClassifier controls the random number generation for the algorithm’s stochastic components.

AdaBoost (Adaptive Boosting) is an ensemble learning method that combines weak learners sequentially, giving more weight to misclassified samples in each iteration. The random_state parameter ensures reproducibility of the model’s random processes.

Setting random_state to a fixed integer allows you to reproduce the same model results across different runs. This is crucial for debugging, comparing models, and ensuring consistent results in production environments.

The default value for random_state is None, which uses the system’s current time as a seed. In practice, any integer value can be used, with common choices being 42, 0, or 123.

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

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=10,
                           n_redundant=5, 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, 0, 42, 123]
accuracies = []

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

# Train multiple times with random_state=None
print("\nMultiple runs with random_state=None:")
for _ in range(3):
    clf = AdaBoostClassifier(random_state=None)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    print(f"Accuracy: {accuracy:.3f}")

Running the example gives an output like:

random_state=None, Accuracy: 0.825
random_state=0, Accuracy: 0.825
random_state=42, Accuracy: 0.825
random_state=123, Accuracy: 0.825

Multiple runs with random_state=None:
Accuracy: 0.825
Accuracy: 0.825
Accuracy: 0.825

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 AdaBoostClassifier models with different random_state values
  4. Evaluate the accuracy of each model on the test set
  5. Demonstrate the variability of results when random_state is None

Some tips and considerations for using random_state:



See Also