Random Search in Practice
Svep för att visa menyn
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.
1234567891011121314151617181920212223242526272829303132333435from 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_)
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.
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 2. Kapitel 2
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Avsnitt 2. Kapitel 2