-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example with multiple different optimization algos. in parallel
- Loading branch information
1 parent
9fb2c3d
commit 5be519d
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
examples/optimization_applications/multiple_different_optimizers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import numpy as np | ||
|
||
from sklearn.model_selection import cross_val_score | ||
from sklearn.ensemble import GradientBoostingClassifier | ||
from sklearn.ensemble import RandomForestClassifier | ||
from sklearn.datasets import load_breast_cancer | ||
|
||
from hyperactive import Hyperactive | ||
from hyperactive.optimizers import ( | ||
HillClimbingOptimizer, | ||
RandomRestartHillClimbingOptimizer, | ||
) | ||
|
||
data = load_breast_cancer() | ||
X, y = data.data, data.target | ||
|
||
|
||
def model_rfc(opt): | ||
rfc = RandomForestClassifier( | ||
n_estimators=opt["n_estimators"], | ||
criterion=opt["criterion"], | ||
max_features=opt["max_features"], | ||
min_samples_split=opt["min_samples_split"], | ||
min_samples_leaf=opt["min_samples_leaf"], | ||
bootstrap=opt["bootstrap"], | ||
) | ||
scores = cross_val_score(rfc, X, y, cv=3) | ||
|
||
return scores.mean() | ||
|
||
|
||
def model_gbc(opt): | ||
gbc = GradientBoostingClassifier( | ||
n_estimators=opt["n_estimators"], | ||
learning_rate=opt["learning_rate"], | ||
max_depth=opt["max_depth"], | ||
min_samples_split=opt["min_samples_split"], | ||
min_samples_leaf=opt["min_samples_leaf"], | ||
subsample=opt["subsample"], | ||
max_features=opt["max_features"], | ||
) | ||
scores = cross_val_score(gbc, X, y, cv=3) | ||
|
||
return scores.mean() | ||
|
||
|
||
search_space_rfc = { | ||
"n_estimators": list(range(10, 200, 10)), | ||
"criterion": ["gini", "entropy"], | ||
"max_features": list(np.arange(0.05, 1.01, 0.05)), | ||
"min_samples_split": list(range(2, 21)), | ||
"min_samples_leaf": list(range(1, 21)), | ||
"bootstrap": [True, False], | ||
} | ||
|
||
|
||
search_space_gbc = { | ||
"n_estimators": list(range(10, 200, 10)), | ||
"learning_rate": [1e-3, 1e-2, 1e-1, 0.5, 1.0], | ||
"max_depth": list(range(1, 11)), | ||
"min_samples_split": list(range(2, 21)), | ||
"min_samples_leaf": list(range(1, 21)), | ||
"subsample": list(np.arange(0.05, 1.01, 0.05)), | ||
"max_features": list(np.arange(0.05, 1.01, 0.05)), | ||
} | ||
|
||
optimizer1 = HillClimbingOptimizer() | ||
optimizer2 = RandomRestartHillClimbingOptimizer() | ||
|
||
|
||
hyper = Hyperactive() | ||
hyper.add_search( | ||
model_rfc, | ||
search_space_rfc, | ||
n_iter=50, | ||
optimizer=optimizer1, | ||
) | ||
hyper.add_search( | ||
model_gbc, | ||
search_space_gbc, | ||
n_iter=50, | ||
optimizer=optimizer2, | ||
n_jobs=2, | ||
) | ||
hyper.run(max_time=5) |