|
1
|
|
|
import time |
|
2
|
|
|
import numpy as np |
|
3
|
|
|
from sklearn.datasets import load_iris |
|
4
|
|
|
from sklearn.model_selection import cross_val_score |
|
5
|
|
|
from sklearn.tree import DecisionTreeClassifier |
|
6
|
|
|
from hyperactive import Hyperactive |
|
7
|
|
|
|
|
8
|
|
|
data = load_iris() |
|
9
|
|
|
X, y = data.data, data.target |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def model(para, X, y): |
|
13
|
|
|
dtc = DecisionTreeClassifier( |
|
14
|
|
|
max_depth=para["max_depth"], min_samples_split=para["min_samples_split"], |
|
15
|
|
|
) |
|
16
|
|
|
scores = cross_val_score(dtc, X, y, cv=2) |
|
17
|
|
|
|
|
18
|
|
|
return scores.mean() |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
search_space = { |
|
22
|
|
|
"max_depth": list(range(1, 21)), |
|
23
|
|
|
"min_samples_split": list(range(2, 21)), |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def model1(para, X, y): |
|
28
|
|
|
dtc = DecisionTreeClassifier( |
|
29
|
|
|
max_depth=para["max_depth"], min_samples_split=para["min_samples_split"], |
|
30
|
|
|
) |
|
31
|
|
|
scores = cross_val_score(dtc, X, y, cv=3) |
|
32
|
|
|
|
|
33
|
|
|
return scores.mean() |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
search_space1 = { |
|
37
|
|
|
"max_depth": list(range(1, 21)), |
|
38
|
|
|
"min_samples_split": list(range(2, 21)), |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
n_iter = 30 |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def test_hyperactive_0(): |
|
46
|
|
|
hyper = Hyperactive(X, y) |
|
47
|
|
|
hyper.add_search(model, search_space, n_iter=n_iter) |
|
48
|
|
|
hyper.run() |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
def test_hyperactive_1(): |
|
52
|
|
|
hyper = Hyperactive(X, y) |
|
53
|
|
|
hyper.add_search(model, search_space, n_iter=n_iter, n_jobs=2) |
|
54
|
|
|
hyper.run() |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def test_hyperactive_2(): |
|
58
|
|
|
hyper = Hyperactive(X, y) |
|
59
|
|
|
hyper.add_search(model, search_space, n_iter=n_iter, n_jobs=4) |
|
60
|
|
|
hyper.run() |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
def test_hyperactive_3(): |
|
64
|
|
|
hyper = Hyperactive(X, y) |
|
65
|
|
|
hyper.add_search(model, search_space, n_iter=n_iter) |
|
66
|
|
|
hyper.add_search(model, search_space, n_iter=n_iter) |
|
67
|
|
|
hyper.run() |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def test_hyperactive_4(): |
|
71
|
|
|
hyper = Hyperactive(X, y) |
|
72
|
|
|
hyper.add_search(model, search_space, n_iter=n_iter) |
|
73
|
|
|
hyper.add_search(model1, search_space1, n_iter=n_iter) |
|
74
|
|
|
hyper.run() |
|
75
|
|
|
|
|
76
|
|
|
|