Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Random Search in Practice | Hyperparameter Optimization
Practice
Projects
Quizzes & Challenges
Quizer
Challenges
/
Introduction to AutoML

bookRandom Search in Practice

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 2. Kapittel 2
some-alt