forked from hyperactive-project/Hyperactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_search_example.py
More file actions
65 lines (53 loc) · 2.23 KB
/
Copy pathgrid_search_example.py
File metadata and controls
65 lines (53 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Grid Search Example - Exhaustive Parameter Space Search
Grid search systematically evaluates all possible combinations of parameters
from a predefined discrete grid. While computationally expensive, it guarantees
finding the optimal solution within the defined grid and provides comprehensive
coverage of the parameter space.
Characteristics:
- Exhaustive search over discrete parameter grid
- Guaranteed to find optimal solution in the grid
- Systematic and reproducible results
- Computationally expensive for large grids
- Best for small discrete parameter spaces
"""
import numpy as np
from sklearn.datasets import load_wine
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from hyperactive.experiment.integrations import SklearnCvExperiment
from hyperactive.opt.gfo import GridSearch
# Load dataset
X, y = load_wine(return_X_y=True)
print(f"Dataset: Wine classification ({X.shape[0]} samples, {X.shape[1]} features)")
# Create experiment
estimator = RandomForestClassifier(random_state=42)
experiment = SklearnCvExperiment(estimator=estimator, X=X, y=y, cv=3)
# Define discrete grid - grid search requires explicit discrete values
search_space = {
"n_estimators": [10, 50, 100, 200], # Discrete values only
"max_depth": [5, 10, 15, 20], # Discrete depth values
"min_samples_split": [2, 5, 10], # Small discrete set
"min_samples_leaf": [1, 2, 5], # Discrete leaf values
}
# Calculate total combinations
total_combinations = 1
for param_values in search_space.values():
total_combinations *= len(param_values)
print(f"Total parameter combinations: {total_combinations}")
# Configure Grid Search optimizer
optimizer = GridSearch(
search_space=search_space,
n_iter=total_combinations, # Evaluate all combinations
random_state=42,
experiment=experiment
)
# Run optimization
# Grid search will systematically evaluate every possible combination
# of parameters from the defined grid, ensuring complete coverage
best_params = optimizer.solve()
# Results
print("\n=== Results ===")
print(f"Best parameters: {best_params}")
print(f"Evaluated {total_combinations} parameter combinations")
print("Grid search completed successfully")