forked from belakaria/MESMO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPmodel.py
executable file
·49 lines (42 loc) · 1.46 KB
/
GPmodel.py
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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 29 14:34:01 2018
@author: Syrine Belakaria
"""
#import math
import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel,Matern
#import logging
class GaussianProcess:
def __init__(self, dim):
self.dim = dim
self.kernel = RBF(length_scale=1, length_scale_bounds=(1e-3, 1e2))
self.beta=1e6
self.xValues = []
self.yValues = []
self.yValuesNorm=[]
self.model = GaussianProcessRegressor(kernel=self.kernel,n_restarts_optimizer=5)
# self.model = GaussianProcessRegressor(kernel=self.kernel,normalize_y=True,n_restarts_optimizer=5)
def fitNormal(self):
y_mean = np.mean(self.yValues)
y_std = self.getstd()
self.yValuesNorm= (self.yValues - y_mean)/y_std
self.model.fit(self.xValues, self.yValuesNorm)
def fitModel(self):
self.model.fit(self.xValues, self.yValues)
def addSample(self, x, y):
self.xValues.append(x)
self.yValues.append(y)
def getPrediction(self, x):
mean, std = self.model.predict(x.reshape(1,-1),return_std=True)
if std[0]==0:
std[0]=np.sqrt(1e-5)*self.getstd()
return mean, std
def getmean(self):
return np.mean(self.yValues)
def getstd(self):
y_std=np.std(self.yValues)
if y_std==0:
y_std=1
return y_std