Passed
Push — master ( 6d85fb...cf85a8 )
by Simon
01:29
created

bayesian_optimization.GPR.fit()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 3
1
import GPy
2
from sklearn.model_selection import cross_val_score
3
from sklearn.ensemble import GradientBoostingClassifier
4
from sklearn.datasets import load_breast_cancer
5
from hyperactive import Hyperactive
6
7
data = load_breast_cancer()
8
X, y = data.data, data.target
9
10
11
def model(para, X, y):
12
    gbc = GradientBoostingClassifier(
13
        n_estimators=para["n_estimators"],
14
        max_depth=para["max_depth"],
15
        min_samples_split=para["min_samples_split"],
16
    )
17
    scores = cross_val_score(gbc, X, y, cv=3)
18
19
    return scores.mean()
20
21
22
search_config = {
23
    model: {
24
        "n_estimators": range(10, 100, 10),
25
        "max_depth": range(2, 12),
26
        "min_samples_split": range(2, 12),
27
    }
28
}
29
30
class GPR:
31
    def __init__(self):
32
        kernel = GPy.kern.RBF(input_dim=1, variance=1., lengthscale=1.)
33
        
34
    def fit(self, X, y):
35
        m = GPy.models.GPRegression(X, y, kernel)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable kernel does not seem to be defined.
Loading history...
36
        m.optimize(messages=True)
37
38
    def predict(self, X):
39
        return m.predict(X)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable m does not seem to be defined.
Loading history...
40
41
bayes_opt = {"Bayesian": {"gpr": GPR()}}
42
43
opt = Hyperactive(X, y)
44
opt.search(search_config, n_iter=30, optimizer=bayes_opt)
45