-
Notifications
You must be signed in to change notification settings - Fork 9
/
wrappers.py
57 lines (44 loc) · 1.45 KB
/
wrappers.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
50
51
52
53
54
55
56
57
import numpy as np
import openturns as ot
from sklearn.base import BaseEstimator
class GpOTtoSklearnStd(BaseEstimator):
"""
Wrapper for OpenTURNS Gaussian Process to be used in MAPIE.
"""
def __init__(
self, scale: int, amplitude: float,
nu: float, noise: float = None
) -> None:
self.scale = scale
self.amplitude = amplitude
self.nu = nu
self.trained_ = False
self.noise = noise
def fit(self, X_train, y_train):
input_dim = X_train.shape[1]
scale = input_dim * [self.scale]
amplitude = [self.amplitude]
covarianceModel = ot.MaternModel(scale, amplitude, self.nu)
if self.noise:
covarianceModel.setNuggetFactor(self.noise)
basis = ot.ConstantBasisFactory(input_dim).build()
self.gp = ot.KrigingAlgorithm(
ot.Sample(X_train),
ot.Sample(y_train.reshape(-1, 1)),
covarianceModel, basis
)
self.gp.run()
self.trained_ = True
def predict(self, X_test, return_std=False):
metamodel = self.gp.getResult()(X_test)
y_pred = metamodel.getMean()
y_std = metamodel.getStandardDeviation()
if not return_std:
return np.array(y_pred)
else:
return np.array(y_pred), np.array(y_std)
def __sklearn_is_fitted__(self):
if self.trained_:
return True
else:
return False