gemseo / disciplines

Show inherited members

surrogate module

Surrogate discipline.

class gemseo.disciplines.surrogate.SurrogateDiscipline(surrogate, data=None, transformer=mappingproxy({'inputs': <gemseo.mlearning.transformers.scaler.min_max_scaler.MinMaxScaler object>, 'outputs': <gemseo.mlearning.transformers.scaler.min_max_scaler.MinMaxScaler object>}), disc_name=None, default_inputs=None, input_names=None, output_names=None, **parameters)[source]

Bases: MDODiscipline

A discipline wrapping a regression model built from a dataset.

Examples

>>> import numpy as np
>>> from gemseo.datasets.io_dataset import IODataset
>>> from gemseo.disciplines.surrogate import SurrogateDiscipline
>>>
>>> # Create an input-output dataset.
>>> dataset = IODataset()
>>> dataset.add_input_variable("x", np.array([[1.0], [2.0], [3.0]]))
>>> dataset.add_output_variable("y", np.array([[3.0], [5.0], [6.0]]))
>>>
>>> # Build a surrogate discipline relying on a linear regression model.
>>> surrogate_discipline = SurrogateDiscipline("LinearRegressor", dataset)
>>>
>>> # Assess its quality with the R2 measure.
>>> r2 = surrogate_discipline.get_error_measure("R2Measure")
>>> learning_r2 = r2.evaluate_learn()
>>>
>>> # Execute the surrogate discipline, with default or custom input values.
>>> surrogate_discipline.execute()
>>> surrogate_discipline.execute({"x": np.array([1.5])})

Initialize self. See help(type(self)) for accurate signature.

Parameters:
  • surrogate (str | BaseMLRegressionAlgo) – Either the name of a class deriving from BaseMLRegressionAlgo or the instance of an BaseMLRegressionAlgo.

  • data (IODataset | None) – The learning dataset to train the regression model. If None, the regression model is supposed to be trained.

  • transformer (TransformerType) –

    The strategies to transform the variables. The values are instances of BaseTransformer while the keys are the names of either the variables or the groups of variables, e.g. "inputs" or "outputs" in the case of the regression algorithms. If a group is specified, the BaseTransformer will be applied to all the variables of this group. If The :attr:.BaseMLRegressionAlgo.DEFAULT_TRANSFORMER` uses the MinMaxScaler strategy for both input and output variables.

    By default it is set to {‘inputs’: <gemseo.mlearning.transformers.scaler.min_max_scaler.MinMaxScaler object at 0x7f1dec69e040>, ‘outputs’: <gemseo.mlearning.transformers.scaler.min_max_scaler.MinMaxScaler object at 0x7f1dec69e0d0>}.

  • disc_name (str | None) – The name to be given to the surrogate discipline. If None, concatenate SHORT_ALGO_NAME and data.name.

  • default_inputs (dict[str, ndarray] | None) – The default values of the inputs. If None, use the center of the learning input space.

  • input_names (Iterable[str] | None) – The names of the input variables. If None, consider all input variables mentioned in the learning dataset.

  • output_names (Iterable[str] | None) – The names of the output variables. If None, consider all input variables mentioned in the learning dataset.

  • **parameters (MLAlgoParameterType) – The parameters of the machine learning algorithm.

Raises:

ValueError – If the learning dataset is missing whilst the regression model is not trained.

get_error_measure(measure_name, **measure_options)[source]

Return an error measure.

Parameters:
  • measure_name (str) – The class name of the error measure.

  • **measure_options (Any) – The options of the error measure.

Returns:

The error measure.

Return type:

BaseMLErrorMeasure

get_quality_viewer()[source]

Return a viewer of the quality of the underlying regressor.

Returns:

A viewer of the quality of the underlying regressor.

Return type:

MLRegressorQualityViewer

cache: AbstractCache | None

The cache containing one or several executions of the discipline according to the cache policy.

data_processor: DataProcessor

A tool to pre- and post-process discipline data.

exec_for_lin: bool

Whether the last execution was due to a linearization.

input_grammar: BaseGrammar

The input grammar.

jac: MutableMapping[str, MutableMapping[str, ndarray | csr_array | JacobianOperator]]

The Jacobians of the outputs wrt inputs.

The structure is {output: {input: matrix}}.

name: str

The name of the discipline.

output_grammar: BaseGrammar

The output grammar.

re_exec_policy: ReExecutionPolicy

The policy to re-execute the same discipline.

regression_model: BaseMLRegressionAlgo

The regression model called by the surrogate discipline.

residual_variables: dict[str, str]

The output variables mapping to their inputs, to be considered as residuals; they shall be equal to zero.

run_solves_residuals: bool

Whether the run method shall solve the residuals.

Examples using SurrogateDiscipline

Create a surrogate discipline

Create a surrogate discipline

Plug a surrogate discipline in a Scenario

Plug a surrogate discipline in a Scenario

Error from surrogate discipline

Error from surrogate discipline