Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Random Search in Practice | Hyperparameter Optimization
Introduction to AutoML

bookRandom Search in Practice

Scorri per mostrare il menu

Random Search is a practical method for hyperparameter tuning that overcomes key limitations of Grid Search.

  • Grid Search: Tries every possible combination of specified hyperparameters; quickly becomes slow and expensive as the parameter space grows, especially with many or continuous values;
  • Random Search: Samples a fixed number of random combinations from the parameter space; covers more possibilities with fewer iterations and often finds strong solutions faster.

Use Grid Search for small parameter spaces when you need to test every combination. Choose Random Search for large spaces or when only a few hyperparameters likely affect model performance.

1234567891011121314151617181920212223242526272829303132333435
from sklearn.datasets import load_iris from sklearn.model_selection import RandomizedSearchCV from sklearn.neighbors import KNeighborsClassifier from scipy.stats import randint # Load dataset iris = load_iris() X, y = iris.data, iris.target # Define the model knn = KNeighborsClassifier() # Define hyperparameter distributions param_dist = { "n_neighbors": randint(1, 30), "weights": ["uniform", "distance"], "p": [1, 2] # 1: Manhattan, 2: Euclidean } # Set up RandomizedSearchCV random_search = RandomizedSearchCV( estimator=knn, param_distributions=param_dist, n_iter=10, cv=5, random_state=42 ) # Fit to data random_search.fit(X, y) # Show best parameters print("Best parameters found:", random_search.best_params_) print("Best cross-validation score:", random_search.best_score_)
copy
Note
Note

Random Search is more efficient than Grid Search when dealing with large parameter spaces, as it avoids evaluating every possible combination and can find strong candidates with fewer iterations.

question mark

What is the main advantage of using Random Search over Grid Search when tuning hyperparameters?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Sezione 2. Capitolo 2
some-alt