SKLearner Home | About | Contact | Examples

Configure SVC "random_state" Parameter

Support Vector Machines (SVM) are a powerful class of algorithms for classification and regression tasks. The SVC class in scikit-learn implements Support Vector Classification for binary and multi-class problems.

The random_state parameter in SVC is used to set the seed of the random number generator used for shuffling the data and initializing the model’s parameters. Setting this parameter ensures that the results are reproducible across different runs.

By default, random_state is set to None, which means the random number generator is initialized using the current system time. This can lead to different results each time the model is trained, even with the same data and hyperparameters.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

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

for rs in random_state_values:
    svc = SVC(random_state=rs)
    svc.fit(X_train, y_train)
    y_pred = svc.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.920
random_state=42, Accuracy: 0.920
random_state=123, Accuracy: 0.920

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 SVC 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