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

bookRandom Search in Practice

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 2. Luku 2
some-alt