-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathsklearn_random_forest.py
More file actions
47 lines (39 loc) · 1.26 KB
/
sklearn_random_forest.py
File metadata and controls
47 lines (39 loc) · 1.26 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
"""Scikit-learn RandomForest example for documentation.
This snippet demonstrates how to optimize a RandomForest classifier
using Hyperactive's SklearnCvExperiment. It is included in get_started.rst.
"""
# [start:full_example]
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from hyperactive.experiment.integrations import SklearnCvExperiment
from hyperactive.opt.gfo import HillClimbing
# Load data
X, y = load_iris(return_X_y=True)
# Create an experiment that handles cross-validation
experiment = SklearnCvExperiment(
estimator=RandomForestClassifier(random_state=42),
X=X,
y=y,
cv=5,
)
# Define hyperparameter search space
search_space = {
"n_estimators": list(range(10, 200, 10)),
"max_depth": list(range(1, 20)),
"min_samples_split": list(range(2, 10)),
}
# Optimize
optimizer = HillClimbing(
search_space=search_space,
n_iter=5,
experiment=experiment,
)
best_params = optimizer.solve()
print(f"Best hyperparameters: {best_params}")
# [end:full_example]
if __name__ == "__main__":
# Verify we got valid hyperparameters
assert "n_estimators" in best_params
assert "max_depth" in best_params
assert "min_samples_split" in best_params
print("Sklearn RandomForest example passed!")