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 Test_2D(X): | |
"""2D Test Function""" | |
y = (1-X[:,0])**2 + 100*(X[:,1]-X[:,0]**2)**2 | |
return y |
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
# Training data | |
sample_num = 25 | |
lb, ub = np.array([-2, -1]), np.array([2, 3]) | |
X_train = (ub-lb)*lhs(2, samples=sample_num) + lb | |
y_train = Test_2D(X_train).reshape(-1,1) | |
# Test data | |
X1 = np.linspace(-2, 2, 20) | |
X2 = np.linspace(-1, 3, 20) | |
X1, X2 = np.meshgrid(X1, X2) |
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 Test_1D(X): | |
"""1D Test Function""" | |
y = (X*6-2)**2*np.sin(X*12-4) | |
return y | |
# Training data | |
X_train = np.array([0.0, 0.1, 0.2, 0.4, 0.5, 0.6, 0.8, 1], ndmin=2).T | |
y_train = Test_1D(X_train) |
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 score(self, X_test, y_test): | |
"""Calculate root mean squared error | |
Input | |
----- | |
X_test: test set, array of shape (n_samples, n_features) | |
y_test: test labels, array of shape (n_samples, ) | |
Output | |
------ |
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 predict(self, X_test): | |
"""GP model predicting | |
Input | |
----- | |
X_test: test set, array of shape (n_samples, n_features) | |
Output | |
------ | |
f: GP predictions |
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 |
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 Neglikelihood(self, theta): | |
"""Negative likelihood function | |
Input | |
----- | |
theta: array, logarithm of the correlation legnths for different dimensions | |
Output | |
------ | |
LnLike: likelihood value""" |
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 Corr(self, X1, X2, theta): | |
"""Construct the correlation matrix between X1 and X2 | |
Input | |
----- | |
X1, X2: 2D arrays, (n_samples, n_features) | |
theta: array, correlation legnths for different dimensions | |
Output | |
------ |
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
class GaussianProcess: | |
"""A Gaussian Process class for creating and exploiting | |
a Gaussian Process model""" | |
def __init__(self, n_restarts, optimizer): | |
"""Initialize a Gaussian Process model | |
Input | |
------ | |
n_restarts: number of restarts of the local optimizer |
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
# import all packages and set plots to be embedded inline | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from scipy.optimize import minimize | |
from scipy.optimize import Bounds | |
from pyDOE import lhs | |
from sklearn.preprocessing import MinMaxScaler | |
from sklearn.pipeline import Pipeline | |
%matplotlib inline |
NewerOlder