-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils_functions.py
More file actions
183 lines (134 loc) · 5.78 KB
/
utils_functions.py
File metadata and controls
183 lines (134 loc) · 5.78 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import numpy as np
import sklearn
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.model_selection import GridSearchCV
from sklearn import svm
from sklearn.svm import SVR
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score, cross_val_predict
from sklearn import metrics
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
from sklearn.gaussian_process.kernels import Matern
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.decomposition import PCA
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.model_selection import ShuffleSplit
from scipy.optimize import minimize
from scipy.integrate import simps
from scipy.stats import norm
# helper functions
def ucb(mu, std, kappa):
"""
Upper confidence bound
"""
return mu + kappa * std
def loguniform(low=0, high=1, size=None):
"""
Sample from log uniform distribution in a given range.
"""
return np.exp(np.random.uniform(low, high, size))
def get_scalarization():
"""
Random scalar generator.
"""
a = np.random.uniform()
return np.array([a, 1.0 - a])
scalarize = lambda x, y, theta: theta[0] * x + theta[1] * y
scalarize_std = lambda a, b, theta: np.sqrt((theta[0] * a) ** 2 + (theta[1] * b) ** 2)
### Train GPR using scikit learn
def trainGPR(trainData, objective, npcs, isotropic):
"""
Train Gaussian process regression model using the given data.
Parameters
----------
trainData: DataFrame
Dataframe containg x-axis (PC features of probes) and y-axis (sensitivity or selectivity).
objective: String
Objective function or y-axis of GPR.
npcs: Int
Number of PCs or dimensions of the features used for training GPR.
isotropic: Bool
Isotropic or anisotropic GPR (same kernel or different kernel for different dim/pcs).
Returns
-------
GPR model: Dict
Dictionary containing the following:
1. X - PCs used for training GPR.
2. y - objective function used for training GPR.
3. ysdplot - error on objective function.
4. gprModel - GPR model.
5. kernelParam - RBF Kernel bandwidth used for training GPR.
"""
pcListNames=[]
for (item1, item2) in zip(['pc']*npcs, np.arange(0,npcs)):
pcListNames.append(item1+str(item2))
normalizedXData = trainData[pcListNames].values.reshape(-1,npcs)
#normalizedXData = trainData[['pc0', 'pc1']].values.reshape(-1,2)
standardYData = trainData['f_obj_' + objective].values.reshape(-1,1)
standardYerrorData = trainData['f_obj_error_' + objective].values
ysdplot = standardYerrorData
standardYerrorData = standardYerrorData/(np.std(standardYData))
X = normalizedXData
y = standardYData
ysd = standardYerrorData
ysdvar= ysd*ysd
#np.shape(X)[1]
if isotropic:
nk = 1
else:
nk = npcs
kernel = RBF([100]*nk, length_scale_bounds=[(1e-3, 2e2)]*nk )
#print("Init lengthscale: ", kernel)
gprModel = GaussianProcessRegressor(kernel=kernel,n_restarts_optimizer=1000, alpha=ysdvar, normalize_y=True)
gprModel.fit(X, y)
print("Accuracy score for training data: %.4f" % (mean_squared_error(y, gprModel.predict(X))))
print("R2 score for training data: %.4f" % (r2_score(y, gprModel.predict(X))))
print(gprModel.kernel_.get_params()['length_scale'])
kernelParam = gprModel.kernel_.get_params()['length_scale']
result = {'X' : X,
'y' : y,
'ysdplot' : ysdplot,
'gprModel' : gprModel,
'kernelParam': kernelParam}
return result
#### NOT USED (Adapted from Ref: EI -- Acknowledge (original source; if used))
def expected_improvement(x, gaussian_process, evaluated_loss, greater_is_better=True, n_params=1):
"""
Expected improvement acquisition function.
Arguments:
----------
x: array-like, shape = [n_samples, n_hyperparams]
The point for which the expected improvement needs to be computed.
gaussian_process: GaussianProcessRegressor object.
Gaussian process trained on previously evaluated hyperparameters.
evaluated_loss: Numpy array.
Numpy array that contains the values off the loss function for the previously
evaluated hyperparameters.
greater_is_better: Boolean.
Boolean flag that indicates whether the loss function is to be maximised or minimised.
n_params: int.
Dimension of the hyperparameter space.
"""
x_to_predict = x.reshape(-1, n_params)
mu, sigma = gaussian_process.predict(x_to_predict, return_std=True)
if greater_is_better:
loss_optimum = np.max(evaluated_loss)
else:
loss_optimum = np.min(evaluated_loss)
#print(loss_optimum)
scaling_factor = (-1) ** (not greater_is_better)
# In case sigma equals zero
with np.errstate(divide='ignore'):
Z = scaling_factor * (mu - loss_optimum) / sigma.reshape(-1,1)
expected_improvement = scaling_factor * (mu - loss_optimum) * norm.cdf(Z) + sigma.reshape(-1,1) * norm.pdf(Z)
expected_improvement[sigma == 0.0] == 0.0
#print(X.shape, mu.shape, sigma.shape, loss_optimum, Z.shape, expected_improvement.shape)
return scaling_factor * expected_improvement