-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGP_model_selection.py
More file actions
158 lines (132 loc) · 5.43 KB
/
GP_model_selection.py
File metadata and controls
158 lines (132 loc) · 5.43 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import UtilsNetwork as Utils
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF
from sklearn.gaussian_process.kernels import Matern
from matplotlib import rc
import os
import sys
import warnings
from termcolor import colored
from sklearn.model_selection import train_test_split
import numpy as np
os.system('color')
warnings.filterwarnings("ignore")
def fit_gaussian_process(X_train_, y_train_, kernel="rbf", nu_=2.5):
bound = (1e-012, 1000000.0)
rbf_kernel = RBF(length_scale=1, length_scale_bounds=bound)
matern_kernel = Matern(length_scale=1.0, length_scale_bounds=bound, nu=nu_) # remeber you change it
# periodic_kernel = ExpSineSquared(length_scale=1.0, periodicity=1.0, length_scale_bounds=bound, periodicity_bounds=bound)
# rq_kernel = RationalQuadratic(length_scale=1.0, alpha=1.0, length_scale_bounds=bound)
# white_kernel = WhiteKernel(noise_level=1, noise_level_bounds=bound)
if kernel == "rbf":
gp_kernel = rbf_kernel
else:
gp_kernel = matern_kernel
model = GaussianProcessRegressor(kernel=gp_kernel, n_restarts_optimizer=1500)
model.fit(X_train_, y_train_)
return model
if sys.platform == "win32":
rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']})
rc('text', usetex=True)
keyword = sys.argv[1]
variable_name = sys.argv[2]
samples = int(sys.argv[3])
level_single = sys.argv[4]
level_c = sys.argv[5]
level_f = sys.argv[6]
validation_size = float(sys.argv[7])
string_norm = sys.argv[8]
scaler = sys.argv[9]
point = sys.argv[10]
scaler = scaler.replace("'","")
string_norm = string_norm.replace("'","")
print(keyword)
print(variable_name)
print(samples)
print(level_single)
print(level_c)
print(level_f)
print(string_norm)
print(scaler)
print(validation_size)
# ====================================================
if string_norm == "true" or string_norm == "'true'":
norm = True
elif string_norm == "false" or string_norm == "'false'":
norm = False
else:
raise ValueError("Norm can be 'true' or 'false'")
# ====================================================
if keyword == "parab":
n_input = 7
case_folder = "Parabolic"
elif keyword == "parab_diff":
n_input = 7
case_folder = "Parabolic"
elif keyword == "shock":
n_input = 6
case_folder = "ShockTube"
elif keyword == "shock_diff":
n_input = 6
case_folder = "ShockTube"
elif keyword == "airf_diff":
n_input = 6
case_folder = "Airfoil"
elif keyword == "airf":
n_input = 6
case_folder = "Airfoil"
else:
raise ValueError("Chose one option between parab and shock")
# ====================================================
if "diff" in keyword:
X, y, _, _, min_val, max_val = Utils.get_data_diff(keyword, samples, variable_name, level_c, level_f, n_input,
model_path_folder=None, normalize=norm, scaler=scaler, point=point)
else:
X, y, _, _, min_val, max_val = Utils.get_data(keyword, samples, variable_name, level_single, n_input,
model_path_folder=None, normalize=norm, scaler=scaler, point=point)
mean_error_reg, std_error_reg, model_reg = Utils.linear_regression(keyword, variable_name, X.shape[0], level_c, level_f, level_single, n_input, norm, scaler=scaler, point=point)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=validation_size, random_state=42)
y_test = y_test.reshape(-1,)
if norm:
if scaler == "m" or scaler == "'m'":
y_test = y_test * (max_val - min_val) + min_val
elif scaler == "s" or scaler == "'s'":
y_test = y_test * max_val + min_val
best_model = None
best_value=1000
for nu in [0.5, 1.5, 2.5]:
print("\n##########################################")
print("Matern Kernel, nu=", nu)
gpr = fit_gaussian_process(X_train, y_train, kernel="", nu_=nu)
y_pred, y_std = gpr.predict(X_test, return_std=True)
y_pred = y_pred.reshape(-1,)
if norm:
if scaler == "m" or scaler == "'m'":
y_pred = y_pred * (max_val - min_val) + min_val
elif scaler == "s" or scaler == "'s'":
y_pred = y_pred *max_val + min_val
print(colored("\nFinal prediction error:", "green", attrs=['bold']))
mean_error = Utils.compute_mean_prediction_error(y_test, y_pred, 2) * 100
variance_error = Utils.compute_prediction_error_variance(y_test, y_pred, 2) * 100
print(str(mean_error) + "%")
if mean_error<best_value:
best_value=mean_error
best_model = "Matern Kernel, nu=" + str(nu)
print("\n##########################################")
print("RBF Kernel")
gpr = fit_gaussian_process(X_train, y_train)
y_pred, y_std = gpr.predict(X_test, return_std=True)
y_pred = y_pred.reshape(-1,)
if norm:
if scaler == "m" or scaler == "'m'":
y_pred = y_pred * (max_val - min_val) + min_val
elif scaler == "s" or scaler == "'s'":
y_pred = y_pred *max_val + min_val
print(colored("\nFinal prediction error:", "green", attrs=['bold']))
mean_error = Utils.compute_mean_prediction_error(y_test, y_pred, 2) * 100
variance_error = Utils.compute_prediction_error_variance(y_test, y_pred, 2) * 100
print(str(mean_error) + "%")
if mean_error < best_value:
best_value = mean_error
best_model = "RBF Kernel"
print("Best performing configuration: ", best_model)