RandomizedSearchCV
is a powerful tool for hyperparameter optimization that allows you to sample from distributions of hyperparameter values.
After running a random search, you can access the best score achieved across all the hyperparameter configurations using the best_score_
attribute.
The best_score_
attribute returns the mean cross-validated score of the best performing hyperparameter configuration. Accessing best_score_
is useful for quickly identifying the highest performance achieved by the random search, which can help guide the selection of the optimal hyperparameters for your model.
from sklearn.datasets import make_classification
from sklearn.svm import SVC
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform
# Generate a random classification dataset
X, y = make_classification(n_samples=1000, n_classes=2, n_informative=5, n_redundant=5, random_state=42)
# Set up an SVC classifier
svc = SVC(random_state=42)
# Define hyperparameter distributions to sample from
param_dist = {
'C': uniform(0.1, 10),
'kernel': ['linear', 'rbf', 'poly'],
'gamma': uniform(0.01, 1),
}
# Run random search with 5-fold cross-validation
random_search = RandomizedSearchCV(svc, param_distributions=param_dist, n_iter=10, cv=5, random_state=42)
random_search.fit(X, y)
# Access the best score
best_score = random_search.best_score_
# Print the best score
print("Best score:", best_score)
Running the example gives an output like:
Best score: 0.8300000000000001
The steps are as follows:
- Import required libraries and prepare a synthetic classification dataset using
make_classification
. - Configure an
SVC
classifier and define distributions to sample hyperparameters from. - Run
RandomizedSearchCV
with the classifier, hyperparameter distributions, 10 iterations, and 5-fold cross-validation. - After fitting, access the
best_score_
attribute from therandom_search
object. - Print the value of
best_score_
, which represents the mean cross-validated score of the best performing hyperparameter configuration.
By accessing the best_score_
attribute, you can quickly identify the highest performance achieved by the random search, helping you to select the optimal hyperparameters for your model.