-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGP_ML_project.py
119 lines (92 loc) · 4.26 KB
/
GP_ML_project.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import numpy as np
import matplotlib.pyplot as plt
from functools import partial
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern
from modAL.models import BayesianOptimizer
from modAL.acquisition import max_EI, max_PI, max_UCB
#%% First sample
X = np.linspace(0, 1, 1000).reshape(-1, 1)
X_initial = np.array([[0],[0.1], [0.25], [0.5], [0.75] ,[1]])
y_initial = np.array([[0.2821],[0.3524],[0.412],[0.43] , [0.29] , [0.2851]])
# defining the kernel for the Gaussian process
kernel = Matern(length_scale=1.0)
regressor = GaussianProcessRegressor(kernel=kernel)
optimizer = BayesianOptimizer(
estimator=regressor,
X_training=X_initial, y_training=y_initial,
query_strategy=max_EI #max_EI, max_PI, max-UCB
)
query_idx, query_inst = optimizer.query(X)
print("Next sample to be measured: ", query_inst)
## plotting
y_pred, y_std = optimizer.predict(X, return_std=True)
y_pred, y_std = y_pred.ravel(), y_std.ravel()
X_max, y_max = optimizer.get_max()
with plt.style.context('seaborn-white'):
plt.figure(figsize=(10, 5))
plt.scatter(optimizer.X_training, optimizer.y_training, c='k', s=50, label='Observed Samples')
plt.axvline(x = query_inst, color = 'r', label = 'Next Sample (query): x ={}'.format(np.round(*query_inst[0],8)))
plt.plot(X.ravel(), y_pred, label='GP regressor')
plt.fill_between(X.ravel(), y_pred - y_std, y_pred + y_std, alpha=0.5)
plt.title('Initial GP with first selected query')
plt.legend()
plt.savefig('1.jpg', dpi = 300)
plt.show()
#%% Second Sample
X = np.linspace(0, 1, 1000).reshape(-1, 1)
X_initial = np.array([[0],[0.1], [0.25],[0.40840841], [0.5], [0.75] ,[1]])
y_initial = np.array([[0.2821],[0.3524],[0.412], [0.4432] ,[0.43] , [0.29] , [0.2851]])
# defining the kernel for the Gaussian process
kernel = Matern(length_scale=1.0)
regressor = GaussianProcessRegressor(kernel=kernel)
optimizer = BayesianOptimizer(
estimator=regressor,
X_training=X_initial, y_training=y_initial,
query_strategy=max_EI #max_EI, max_PI, max-UCB
)
query_idx, query_inst = optimizer.query(X)
print("Next sample to be measured: ", query_inst)
## plotting
y_pred, y_std = optimizer.predict(X, return_std=True)
y_pred, y_std = y_pred.ravel(), y_std.ravel()
X_max, y_max = optimizer.get_max()
with plt.style.context('seaborn-white'):
plt.figure(figsize=(10, 5))
plt.scatter(optimizer.X_training, optimizer.y_training, c='k', s=50, label='Observed Samples')
plt.axvline(x = query_inst, color = 'r', label = 'Next Sample (query): x ={}'.format(np.round(*query_inst[0],8)))
plt.plot(X.ravel(), y_pred, label='GP regressor')
plt.fill_between(X.ravel(), y_pred - y_std, y_pred + y_std, alpha=0.5)
plt.title('Updated GP with second selected query')
plt.legend()
plt.savefig('2.jpg', dpi = 300)
plt.show()
#%% Final GP
X = np.linspace(0, 1, 1000).reshape(-1, 1)
X_initial = np.array([[0],[0.1], [0.25],[0.40840841],[0.43543544], [0.5], [0.75] ,[1]])
y_initial = np.array([[0.2821],[0.3524],[0.412], [0.4432] , [0.4117],[0.43] , [0.29] , [0.2851]])
# defining the kernel for the Gaussian process
kernel = Matern(length_scale=1.0)
regressor = GaussianProcessRegressor(kernel=kernel)
optimizer = BayesianOptimizer(
estimator=regressor,
X_training=X_initial, y_training=y_initial,
query_strategy=max_EI #max_EI, max_PI, max-UCB
)
query_idx, query_inst = optimizer.query(X)
print("Next sample to be measured: ", query_inst)
## plotting
y_pred, y_std = optimizer.predict(X, return_std=True)
y_pred, y_std = y_pred.ravel(), y_std.ravel()
X_max, y_max = optimizer.get_max()
with plt.style.context('seaborn-white'):
plt.figure(figsize=(10, 5))
plt.scatter(optimizer.X_training, optimizer.y_training, c='k', s=50, label='Observed Samples')
plt.scatter(X_max, y_max, c='r', s=70, label='Best Observed Sample')
#plt.axvline(x = query_inst, color = 'r', label = 'Next Sample (query): x ={}'.format(np.round(*query_inst[0],8)))
plt.plot(X.ravel(), y_pred, label='GP regressor')
plt.fill_between(X.ravel(), y_pred - y_std, y_pred + y_std, alpha=0.5)
plt.title('Final GP')
plt.legend()
plt.savefig('3.jpg', dpi = 300)
plt.show()