-
Notifications
You must be signed in to change notification settings - Fork 0
/
sklearn_tuning.py
62 lines (48 loc) · 1.91 KB
/
sklearn_tuning.py
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
import keras_tuner
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import RidgeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import make_scorer
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import train_test_split
SKLEARN_TRIALS_DIR = "."
SKLEARN_PROJECT_NAME = "sklearn_hp"
def build_hypermodel(hp):
model_type = hp.Choice("model_type", values=["random_forest", "decision_tree", "ridge",])
match model_type:
case "random_forest":
return RandomForestClassifier(
n_estimators=hp.Int("n_estimators", 10, 50, step=10),
max_depth=hp.Int("max_depth", 3, 10)
)
case "decision_tree":
return DecisionTreeClassifier(
criterion=hp.Choice("dense_activation", values=["gini", "entropy", "log_loss"]),
max_depth=hp.Int("max_deptth", 4, 10, step=1)
)
case "ridge":
return RidgeClassifier(
alpha=hp.Float("alpha", 1e-3, 1, sampling="log")
)
case _:
return None
if __name__ == "__main__":
X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
optimizer = keras_tuner.oracles.BayesianOptimizationOracle(
objective=keras_tuner.Objective("score", "max"),
max_trials=100
)
tuner = keras_tuner.tuners.SklearnTuner(
oracle=optimizer,
hypermodel=build_hypermodel,
scoring=make_scorer(accuracy_score),
cv=StratifiedKFold(5),
directory=SKLEARN_TRIALS_DIR,
project_name=SKLEARN_PROJECT_NAME
)
tuner.search(X_train, y_train)
best_model = tuner.get_best_models(num_models=1)[0]
print(f"Best model: {best_model}")