11"""
22Module wrapper exploiting `GPy` for Gaussian Process Regression
33"""
4+
45import logging
56import numpy as np
67from scipy .optimize import minimize
@@ -40,19 +41,24 @@ class GPR(Approximation):
4041 >>> print(np.allclose(y, y_pred))
4142
4243 """
44+
4345 def __init__ (self , kern = None , normalizer = True , optimization_restart = 20 ):
4446 """
4547 Initialize a Gaussian Process Regressor.
46-
48+
4749 :param kern: Kernel object from sklearn. Default is None.
4850 :param bool normalizer: Whether to normalize values. Default is True.
4951 :param int optimization_restart: Number of restarts for optimization.
5052 Default is 20.
5153 """
5254
53- logger .debug ("Initializing GPR with kernel=%s, normalizer=%s, "
54- "optimization_restart=%d" ,
55- kern , normalizer , optimization_restart )
55+ logger .debug (
56+ "Initializing GPR with kernel=%s, normalizer=%s, "
57+ "optimization_restart=%d" ,
58+ kern ,
59+ normalizer ,
60+ optimization_restart ,
61+ )
5662 self .X_sample = None
5763 self .Y_sample = None
5864 self .kern = kern
@@ -67,8 +73,11 @@ def fit(self, points, values):
6773 :param array_like points: the coordinates of the points.
6874 :param array_like values: the values in the points.
6975 """
70- logger .debug ("Fitting GPR with points shape: %s, values shape: %s" ,
71- np .array (points ).shape , np .array (values ).shape )
76+ logger .debug (
77+ "Fitting GPR with points shape: %s, values shape: %s" ,
78+ np .array (points ).shape ,
79+ np .array (values ).shape ,
80+ )
7281 self .X_sample = np .array (points )
7382 self .Y_sample = np .array (values )
7483 if self .X_sample .ndim == 1 :
@@ -80,8 +89,10 @@ def fit(self, points, values):
8089
8190 logger .debug ("Creating GaussianProcessRegressor" )
8291 self .model = GaussianProcessRegressor (
83- kernel = self .kern , n_restarts_optimizer = self .optimization_restart ,
84- normalize_y = self .normalizer )
92+ kernel = self .kern ,
93+ n_restarts_optimizer = self .optimization_restart ,
94+ normalize_y = self .normalizer ,
95+ )
8596 self .model .fit (self .X_sample , self .Y_sample )
8697 logger .info ("GPR fitted successfully" )
8798
@@ -118,14 +129,14 @@ def optimal_mu(self, bounds, optimization_restart=10):
118129 def min_obj (X ):
119130 return - 1 * np .linalg .norm (self .predict (X .reshape (1 , - 1 ), True )[1 ])
120131
121- initial_starts = np .random .uniform (bounds [:, 0 ],
122- bounds [:, 1 ],
123- size = ( optimization_restart , dim ) )
132+ initial_starts = np .random .uniform (
133+ bounds [:, 0 ], bounds [:, 1 ], size = ( optimization_restart , dim )
134+ )
124135
125136 # Find the best optimum by starting from n_restart different random
126137 # points.
127138 for x0 in initial_starts :
128- res = minimize (min_obj , x0 , bounds = bounds , method = ' L-BFGS-B' )
139+ res = minimize (min_obj , x0 , bounds = bounds , method = " L-BFGS-B" )
129140
130141 if res .fun < min_val :
131142 min_val = res .fun
0 commit comments