easyvvuq.analysis.gp_analyse
Will create a Gaussian Process surrogate of your model. For the sampler you can use the random sampler or the quasi-random sampler. Don't forget to set the analysis class to GaussianProcessSurrogate as is shown in the example below.
This uses the Gaussian Process model from sklearn.
Examples
>>> campaign = uq.Campaign(name='surrogate')
>>> sampler = uq.sampling.RandomSampler(
vary = {"Pe": cp.Uniform(100.0, 200.0), "f": cp.Uniform(0.95, 1.05)}
max_num=100, analysis_class=uq.analysis.GaussianProcessSurrogate)
>>> campaign.add_app(name="sc", params=params, actions=actions)
>>> campaign.set_sampler(sampler)
>>> campaign.execute().collate()
>>> results = campaign.analyse(qoi_cols=output_columns)
>>> surrogate = results.surrogate()
>>> surrogate({'Pe' : 110.0, 'f': 1.0})
1"""Will create a Gaussian Process surrogate of your model. For 2the sampler you can use the random sampler or the quasi-random 3sampler. Don't forget to set the analysis class to GaussianProcessSurrogate 4as is shown in the example below. 5 6This uses the Gaussian Process model from sklearn. 7 8Examples 9-------- 10>>> campaign = uq.Campaign(name='surrogate') 11>>> sampler = uq.sampling.RandomSampler( 12 vary = {"Pe": cp.Uniform(100.0, 200.0), "f": cp.Uniform(0.95, 1.05)} 13 max_num=100, analysis_class=uq.analysis.GaussianProcessSurrogate) 14>>> campaign.add_app(name="sc", params=params, actions=actions) 15>>> campaign.set_sampler(sampler) 16>>> campaign.execute().collate() 17>>> results = campaign.analyse(qoi_cols=output_columns) 18>>> surrogate = results.surrogate() 19>>> surrogate({'Pe' : 110.0, 'f': 1.0}) 20""" 21 22from .base import BaseAnalysisElement 23from sklearn.gaussian_process import GaussianProcessRegressor 24from .results import AnalysisResults 25import numpy as np 26 27 28class GaussianProcessSurrogateResults(AnalysisResults): 29 """Gaussian process surrogate results class. You would never 30 create this manually in normal use. It is meant to be returned as the 31 result of GaussianProcessSurrogate analyse method. 32 33 Parameters 34 ---------- 35 gps: list 36 This will be one GP model for each coordinate of a vector QoI. 37 parameters: list 38 A list of input parameter names. 39 qoi: str 40 Output variable name. 41 """ 42 43 def __init__(self, gp, parameters, qoi): 44 self.gp = gp 45 self.parameters = parameters 46 self.qoi = qoi 47 48 def surrogate(self): 49 """Returns the GP surrogate model as a Python function. 50 51 Returns 52 ------- 53 function 54 Returns a function that takes a dictionary and returns a dictionary. 55 These dictionaries use the same format as Encoder and Decoder used 56 to construct the surrogate. 57 """ 58 def surrogate_fn(inputs): 59 values = np.array([[inputs[key] for key in self.parameters]]) 60 return {self.qoi[0]: [x for x in self.gp.predict(values)[0]]} 61 return surrogate_fn 62 63 def get_params(self): 64 return self.gp.kernel_.get_params() 65 66 67class GaussianProcessSurrogate(BaseAnalysisElement): 68 69 def __init__(self, sampler, qoi_cols, **kwargs): 70 """An analysis class that can construct a Gaussian Process surrogate 71 of your model. Based on the sklearn GaussianProgressRegressor class. 72 73 Parameters 74 ---------- 75 sampler : Sampler 76 `Sampler` that was used to generate samples to train this surrogate model. 77 qoi_cols : list 78 Corresponding target values (can be vectors). 79 """ 80 self.sampler = sampler 81 self.attr_cols = list(sampler.vary.get_keys()) 82 self.target_cols = qoi_cols 83 self.kwargs = kwargs 84 85 def analyse(self, data_frame=None): 86 """Construct a Gaussian Process surrogate based on data in `data_frame`. 87 88 Parameters 89 ---------- 90 data_frame : pandas.DataFrame 91 Data which you want to use to fit the Gaussian Process to. 92 kwargs : keyword arguments 93 These arguments will be passed to sklearn's GaussianProcessRegressor. 94 For details on what this could be, please see 95 96 Returns 97 ------- 98 easyvvuq.analysis.gp.GaussianProcessSurrogateResults 99 `GaussianProcessSurrogateResults` instance. Used to interact with the surrogate 100 model and to possibly access other functionality provided by the fitted model. 101 """ 102 x = data_frame[self.attr_cols].values # lgtm [py/hash-unhashable-value] 103 y = data_frame[self.target_cols].values # lgtm [py/hash-unhashable-value] 104 gp = GaussianProcessRegressor(**self.kwargs) 105 gp = gp.fit(x, y) 106 return GaussianProcessSurrogateResults(gp, self.attr_cols, self.target_cols)
29class GaussianProcessSurrogateResults(AnalysisResults): 30 """Gaussian process surrogate results class. You would never 31 create this manually in normal use. It is meant to be returned as the 32 result of GaussianProcessSurrogate analyse method. 33 34 Parameters 35 ---------- 36 gps: list 37 This will be one GP model for each coordinate of a vector QoI. 38 parameters: list 39 A list of input parameter names. 40 qoi: str 41 Output variable name. 42 """ 43 44 def __init__(self, gp, parameters, qoi): 45 self.gp = gp 46 self.parameters = parameters 47 self.qoi = qoi 48 49 def surrogate(self): 50 """Returns the GP surrogate model as a Python function. 51 52 Returns 53 ------- 54 function 55 Returns a function that takes a dictionary and returns a dictionary. 56 These dictionaries use the same format as Encoder and Decoder used 57 to construct the surrogate. 58 """ 59 def surrogate_fn(inputs): 60 values = np.array([[inputs[key] for key in self.parameters]]) 61 return {self.qoi[0]: [x for x in self.gp.predict(values)[0]]} 62 return surrogate_fn 63 64 def get_params(self): 65 return self.gp.kernel_.get_params()
Gaussian process surrogate results class. You would never create this manually in normal use. It is meant to be returned as the result of GaussianProcessSurrogate analyse method.
Parameters
- gps (list): This will be one GP model for each coordinate of a vector QoI.
- parameters (list): A list of input parameter names.
- qoi (str): Output variable name.
49 def surrogate(self): 50 """Returns the GP surrogate model as a Python function. 51 52 Returns 53 ------- 54 function 55 Returns a function that takes a dictionary and returns a dictionary. 56 These dictionaries use the same format as Encoder and Decoder used 57 to construct the surrogate. 58 """ 59 def surrogate_fn(inputs): 60 values = np.array([[inputs[key] for key in self.parameters]]) 61 return {self.qoi[0]: [x for x in self.gp.predict(values)[0]]} 62 return surrogate_fn
Returns the GP surrogate model as a Python function.
Returns
- function: Returns a function that takes a dictionary and returns a dictionary. These dictionaries use the same format as Encoder and Decoder used to construct the surrogate.
68class GaussianProcessSurrogate(BaseAnalysisElement): 69 70 def __init__(self, sampler, qoi_cols, **kwargs): 71 """An analysis class that can construct a Gaussian Process surrogate 72 of your model. Based on the sklearn GaussianProgressRegressor class. 73 74 Parameters 75 ---------- 76 sampler : Sampler 77 `Sampler` that was used to generate samples to train this surrogate model. 78 qoi_cols : list 79 Corresponding target values (can be vectors). 80 """ 81 self.sampler = sampler 82 self.attr_cols = list(sampler.vary.get_keys()) 83 self.target_cols = qoi_cols 84 self.kwargs = kwargs 85 86 def analyse(self, data_frame=None): 87 """Construct a Gaussian Process surrogate based on data in `data_frame`. 88 89 Parameters 90 ---------- 91 data_frame : pandas.DataFrame 92 Data which you want to use to fit the Gaussian Process to. 93 kwargs : keyword arguments 94 These arguments will be passed to sklearn's GaussianProcessRegressor. 95 For details on what this could be, please see 96 97 Returns 98 ------- 99 easyvvuq.analysis.gp.GaussianProcessSurrogateResults 100 `GaussianProcessSurrogateResults` instance. Used to interact with the surrogate 101 model and to possibly access other functionality provided by the fitted model. 102 """ 103 x = data_frame[self.attr_cols].values # lgtm [py/hash-unhashable-value] 104 y = data_frame[self.target_cols].values # lgtm [py/hash-unhashable-value] 105 gp = GaussianProcessRegressor(**self.kwargs) 106 gp = gp.fit(x, y) 107 return GaussianProcessSurrogateResults(gp, self.attr_cols, self.target_cols)
Base class for all EasyVVUQ analysis elements.
Attributes
70 def __init__(self, sampler, qoi_cols, **kwargs): 71 """An analysis class that can construct a Gaussian Process surrogate 72 of your model. Based on the sklearn GaussianProgressRegressor class. 73 74 Parameters 75 ---------- 76 sampler : Sampler 77 `Sampler` that was used to generate samples to train this surrogate model. 78 qoi_cols : list 79 Corresponding target values (can be vectors). 80 """ 81 self.sampler = sampler 82 self.attr_cols = list(sampler.vary.get_keys()) 83 self.target_cols = qoi_cols 84 self.kwargs = kwargs
An analysis class that can construct a Gaussian Process surrogate of your model. Based on the sklearn GaussianProgressRegressor class.
Parameters
- sampler (Sampler):
Samplerthat was used to generate samples to train this surrogate model. - qoi_cols (list): Corresponding target values (can be vectors).
86 def analyse(self, data_frame=None): 87 """Construct a Gaussian Process surrogate based on data in `data_frame`. 88 89 Parameters 90 ---------- 91 data_frame : pandas.DataFrame 92 Data which you want to use to fit the Gaussian Process to. 93 kwargs : keyword arguments 94 These arguments will be passed to sklearn's GaussianProcessRegressor. 95 For details on what this could be, please see 96 97 Returns 98 ------- 99 easyvvuq.analysis.gp.GaussianProcessSurrogateResults 100 `GaussianProcessSurrogateResults` instance. Used to interact with the surrogate 101 model and to possibly access other functionality provided by the fitted model. 102 """ 103 x = data_frame[self.attr_cols].values # lgtm [py/hash-unhashable-value] 104 y = data_frame[self.target_cols].values # lgtm [py/hash-unhashable-value] 105 gp = GaussianProcessRegressor(**self.kwargs) 106 gp = gp.fit(x, y) 107 return GaussianProcessSurrogateResults(gp, self.attr_cols, self.target_cols)
Construct a Gaussian Process surrogate based on data in data_frame.
Parameters
- data_frame (pandas.DataFrame): Data which you want to use to fit the Gaussian Process to.
- kwargs (keyword arguments): These arguments will be passed to sklearn's GaussianProcessRegressor. For details on what this could be, please see
Returns
- easyvvuq.analysis.gp.GaussianProcessSurrogateResults:
GaussianProcessSurrogateResultsinstance. Used to interact with the surrogate model and to possibly access other functionality provided by the fitted model.