Skip to content

Instantly share code, notes, and snippets.

View ShuaiGuo16's full-sized avatar
🎯
Focusing

Shuai Guo ShuaiGuo16

🎯
Focusing
View GitHub Profile
@ShuaiGuo16
ShuaiGuo16 / GP_test2DFunc.py
Created January 24, 2021 10:13
Test function 2D
def Test_2D(X):
"""2D Test Function"""
y = (1-X[:,0])**2 + 100*(X[:,1]-X[:,0]**2)**2
return y
@ShuaiGuo16
ShuaiGuo16 / GP_test2D.py
Last active January 24, 2021 10:11
Train a 2D GP model
# 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)
@ShuaiGuo16
ShuaiGuo16 / GP_test1D.py
Last active January 23, 2021 21:36
one dimensional testing
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)
@ShuaiGuo16
ShuaiGuo16 / GP_score.py
Created January 23, 2021 21:20
GP prediction score
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
------
@ShuaiGuo16
ShuaiGuo16 / GP_predict.py
Created January 23, 2021 21:12
GP model Predict
def predict(self, X_test):
"""GP model predicting
Input
-----
X_test: test set, array of shape (n_samples, n_features)
Output
------
f: GP predictions
@ShuaiGuo16
ShuaiGuo16 / GP_fit.py
Last active January 23, 2021 19:01
Fit a GP model
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
@ShuaiGuo16
ShuaiGuo16 / GP_likelihood.py
Last active January 26, 2021 10:16
Calculate negative log-likelihood
def Neglikelihood(self, theta):
"""Negative likelihood function
Input
-----
theta: array, logarithm of the correlation legnths for different dimensions
Output
------
LnLike: likelihood value"""
@ShuaiGuo16
ShuaiGuo16 / GP_correlation.py
Last active January 22, 2021 10:00
Compute correlation matrix between feature matrics
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
------
@ShuaiGuo16
ShuaiGuo16 / GP_initialization.py
Last active January 23, 2021 18:58
Create the initialization function
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
@ShuaiGuo16
ShuaiGuo16 / GP_packages.py
Last active January 23, 2021 18:57
Load packages
# 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