Last active
January 23, 2021 19:01
-
-
Save ShuaiGuo16/86695f630a819d68d27c67bf8758b720 to your computer and use it in GitHub Desktop.
Fit a GP model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fit(self, X, y): | |
"""GP model training | |
Input | |
----- | |
X: 2D array of shape (n_samples, n_features) | |
y: 2D array of shape (n_samples, 1) | |
""" | |
self.X, self.y = X, y | |
lb, ub = -3, 2 | |
# Generate random starting points (Latin Hypercube) | |
lhd = lhs(self.X.shape[1], samples=self.n_restarts) | |
# Scale random samples to the given bounds | |
initial_points = (ub-lb)*lhd + lb | |
# Create A Bounds instance for optimization | |
bnds = Bounds(lb*np.ones(X.shape[1]),ub*np.ones(X.shape[1])) | |
# Run local optimizer on all points | |
opt_para = np.zeros((self.n_restarts, self.X.shape[1])) | |
opt_func = np.zeros((self.n_restarts, 1)) | |
for i in range(self.n_restarts): | |
res = minimize(self.Neglikelihood, initial_points[i,:], method=self.optimizer, | |
bounds=bnds) | |
opt_para[i,:] = res.x | |
opt_func[i,:] = res.fun | |
# Locate the optimum results | |
self.theta = opt_para[np.argmin(opt_func)] | |
# Update attributes | |
self.NegLnlike = self.Neglikelihood(self.theta) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment