This example loads the iris flower data, splits it, and uses RandomizedSearchCV to find good settings for a random forest. It prints the best settings and test accuracy.
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import accuracy_score
# Load data
X, y = load_iris(return_X_y=True)
# Split data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Define model and parameter distribution
model = RandomForestClassifier(random_state=42)
param_dist = {
'n_estimators': [10, 50, 100],
'max_depth': [None, 5, 10],
'min_samples_split': [2, 5, 10]
}
# Setup RandomizedSearchCV
random_search = RandomizedSearchCV(model, param_dist, n_iter=5, cv=3, random_state=42)
# Fit model
random_search.fit(X_train, y_train)
# Best parameters
best_params = random_search.best_params_
# Predict on test data
y_pred = random_search.predict(X_test)
# Calculate accuracy
acc = accuracy_score(y_test, y_pred)
print(f"Best parameters: {best_params}")
print(f"Test accuracy: {acc:.2f}")