-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsandpit.py
67 lines (54 loc) · 1.67 KB
/
sandpit.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
63
64
65
66
67
__author__ = 'James Robert Lloyd'
__description__ = 'Scraps of code before module structure becomes apparent'
from util import callback_1d
import pybo
from pybo.functions.functions import _cleanup, GOModel
import numpy as np
from sklearn.datasets import load_iris
from sklearn.datasets import make_hastie_10_2
from sklearn.ensemble import RandomForestClassifier
from sklearn.cross_validation import cross_val_score
import os
import sys
sys.path.append(os.path.dirname(__file__))
@_cleanup
class Sinusoidal(GOModel):
"""
Simple sinusoidal function bounded in [0, 2pi] given by cos(x)+sin(3x).
"""
bounds = [[0, 2*np.pi]]
xmax = 3.61439678
@staticmethod
def _f(x):
return -np.ravel(np.cos(x) + np.sin(3*x))
@_cleanup
class CV_RF(GOModel):
"""
Cross validated random forest
"""
bounds = [[1, 25]]
xmax = 10 # FIXME - should this not be optional?
@staticmethod
def _f(x):
# iris = load_iris()
X, y = X, y = make_hastie_10_2(random_state=0)
x = np.ravel(x)
f = np.zeros(x.shape)
for i in range(f.size):
clf = RandomForestClassifier(n_estimators=1, min_samples_leaf=int(np.round(x[i])), random_state=0)
# scores = cross_val_score(clf, iris.data, iris.target)
scores = cross_val_score(clf, X, y, cv=5)
f[i] = -scores.mean()
return f.ravel()
if __name__ == '__main__':
objective = CV_RF()
info = pybo.solve_bayesopt(
objective,
objective.bounds,
niter=25,
noisefree=False,
rng=0,
init='uniform',
callback=callback_1d)
print('Finished')
raw_input('Press enter to finish')