@@ -10,6 +10,15 @@ class GaussianProcess(libgp_cpp.GaussianProcess):
1010 This class provides methods for training and predicting with a Gaussian Process model.
1111 """
1212
13+ def __init__ (self , input_dim : int , covariance_function : str ) -> None :
14+ """Initialize the Gaussian Process model.
15+
16+ Parameters:
17+ - input_dim: Number of input dimensions.
18+ - covariance_function: Covariance function to use (e.g., 'RBF', 'Matern').
19+ """
20+ super ().__init__ (input_dim , covariance_function )
21+
1322 def add_pattern (self , x : np .array , y : float ) -> None :
1423 """Add a single training pattern to the Gaussian Process model.
1524
@@ -87,6 +96,15 @@ def clear_sampleset(self) -> None:
8796 """Clear the training set."""
8897 super ().clear_sampleset ()
8998
99+ def get_sampleset (self ) -> tuple :
100+ """Get the training set.
101+
102+ Returns:
103+ - A tuple containing the input features and target values.
104+ """
105+ data = super ().get_sampleset ()
106+ return data [:, :- 1 ], data [:, - 1 ]
107+
90108 def get_log_likelihood (self ) -> float :
91109 """Get the log likelihood of the current model.
92110
@@ -134,3 +152,41 @@ def get_param_dim(self) -> int:
134152 - The number of hyperparameters as an integer.
135153 """
136154 return super ().get_param_dim ()
155+
156+ def to_json (self ) -> dict :
157+ """Convert the Gaussian Process model to a JSON-compatible dictionary.
158+
159+ Returns:
160+ - A dictionary representation of the model.
161+ """
162+ x , y = self .get_sampleset ()
163+ return {
164+ "type" : "GaussianProcess" ,
165+ "covariance_function" : self .get_covariance_function (),
166+ "loghyper" : self .get_loghyper ().tolist (),
167+ "input_dim" : self .get_input_dim (),
168+ "sampleset_size" : self .get_sampleset_size (),
169+ "sampleset_x" : x .tolist (),
170+ "sampleset_y" : y .tolist ()
171+ }
172+
173+ @classmethod
174+ def from_json (cls , json_data : dict ) -> "GaussianProcess" :
175+ """Create a Gaussian Process model from a JSON-compatible dictionary.
176+
177+ Parameters:
178+ - json_data: A dictionary containing the model parameters.
179+
180+ Returns:
181+ - An instance of the GaussianProcess class.
182+ """
183+ input_dim = json_data ["input_dim" ]
184+ covariance_function = json_data ["covariance_function" ]
185+ gp = cls (input_dim , covariance_function )
186+ gp .set_loghyper (np .array (json_data ["loghyper" ]))
187+ gp .add_patterns (np .array (json_data ["sampleset_x" ]), np .array (json_data ["sampleset_y" ]))
188+ return gp
189+
190+ def __repr__ (self ) -> str :
191+ """Return a string representation of the Gaussian Process model."""
192+ return f"GaussianProcess(input_dim={ self .get_input_dim ()} , covariance_function='{ self .get_covariance_function ()} ')"
0 commit comments