Note
Go to the end to download the full example code.
API#
Here are some examples of the machine learning API applied to regression models.
from __future__ import annotations
from gemseo import configure_logger
from gemseo import create_design_space
from gemseo import create_discipline
from gemseo import create_scenario
from gemseo.mlearning import create_regression_model
from gemseo.mlearning import get_regression_models
from gemseo.mlearning import get_regression_options
configure_logger()
<RootLogger root (INFO)>
Get available regression models#
get_regression_models()
['GaussianProcessRegressor', 'GradientBoostingRegressor', 'LinearRegressor', 'MLPRegressor', 'MOERegressor', 'OTGaussianProcessRegressor', 'PCERegressor', 'PolynomialRegressor', 'RBFRegressor', 'RandomForestRegressor', 'RegressorChain', 'SVMRegressor', 'TPSRegressor']
Get regression model options#
get_regression_options("GaussianProcessRegressor")
+---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
| Name | Description | Type |
+---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
| alpha | The nugget effect to regularize the model. | number |
| bounds | The lower and upper bounds of the length scales. either a unique lower-upper pair common | None |
| | to all the inputs or lower-upper pairs for some of them. when ``bounds`` is empty or when | |
| | an input has no pair, the lower bound is 0.01 and the upper bound is 100. this argument | |
| | is ignored when ``kernel`` is ``none``. | |
| input_names | The names of the input variables | array |
| kernel | The kernel specifying the covariance model. if ``none``, use a matérn(2.5). | None |
| n_restarts_optimizer | The number of restarts of the optimizer. | integer |
| optimizer | The optimization algorithm to find the parameter length scales. | None |
| output_names | The names of the output variables | array |
| parameters | Other parameters. | object |
| random_state | The random state parameter. if ``none``, use the global random state instance from | None |
| | ``numpy.random``. creating the model multiple times will produce different results. if | |
| | ``int``, use a new random number generator seeded by this integer. this will produce the | |
| | same results. | |
| transformer | The strategies to transform the variables. the values are instances of | object |
| | :class:`.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 :class:`.basetransformer` will be applied to all | |
| | the variables of this group. if :attr:`.identity`, do not transform the variables. | |
+---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
INFO - 08:37:42: +---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
INFO - 08:37:42: | Name | Description | Type |
INFO - 08:37:42: +---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
INFO - 08:37:42: | alpha | The nugget effect to regularize the model. | number |
INFO - 08:37:42: | bounds | The lower and upper bounds of the length scales. either a unique lower-upper pair common | None |
INFO - 08:37:42: | | to all the inputs or lower-upper pairs for some of them. when ``bounds`` is empty or when | |
INFO - 08:37:42: | | an input has no pair, the lower bound is 0.01 and the upper bound is 100. this argument | |
INFO - 08:37:42: | | is ignored when ``kernel`` is ``none``. | |
INFO - 08:37:42: | input_names | The names of the input variables | array |
INFO - 08:37:42: | kernel | The kernel specifying the covariance model. if ``none``, use a matérn(2.5). | None |
INFO - 08:37:42: | n_restarts_optimizer | The number of restarts of the optimizer. | integer |
INFO - 08:37:42: | optimizer | The optimization algorithm to find the parameter length scales. | None |
INFO - 08:37:42: | output_names | The names of the output variables | array |
INFO - 08:37:42: | parameters | Other parameters. | object |
INFO - 08:37:42: | random_state | The random state parameter. if ``none``, use the global random state instance from | None |
INFO - 08:37:42: | | ``numpy.random``. creating the model multiple times will produce different results. if | |
INFO - 08:37:42: | | ``int``, use a new random number generator seeded by this integer. this will produce the | |
INFO - 08:37:42: | | same results. | |
INFO - 08:37:42: | transformer | The strategies to transform the variables. the values are instances of | object |
INFO - 08:37:42: | | :class:`.basetransformer` while the keys are the names of either the variables or the | |
INFO - 08:37:42: | | groups of variables, e.g. ``"inputs"`` or ``"outputs"`` in the case of the regression | |
INFO - 08:37:42: | | algorithms. if a group is specified, the :class:`.basetransformer` will be applied to all | |
INFO - 08:37:42: | | the variables of this group. if :attr:`.identity`, do not transform the variables. | |
INFO - 08:37:42: +---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
{'additionalProperties': False, 'description': 'The settings of the Gaussian process regressor from scikit-learn.', 'properties': {'transformer': {'description': 'The strategies to transform the variables.\n\nThe values are instances of :class:`.BaseTransformer`\nwhile the keys are the names of\neither the variables\nor the groups of variables,\ne.g. ``"inputs"`` or ``"outputs"``\nin the case of the regression algorithms.\nIf a group is specified,\nthe :class:`.BaseTransformer` will be applied\nto all the variables of this group.\nIf :attr:`.IDENTITY`, do not transform the variables.', 'title': 'Transformer', 'type': 'object'}, 'parameters': {'description': 'Other parameters.', 'title': 'Parameters', 'type': 'object'}, 'input_names': {'default': [], 'description': 'The names of the input variables', 'items': {'type': 'string'}, 'title': 'Input Names', 'type': 'array'}, 'output_names': {'default': [], 'description': 'The names of the output variables', 'items': {'type': 'string'}, 'title': 'Output Names', 'type': 'array'}, 'kernel': {'anyOf': [{}, {'type': 'null'}], 'default': None, 'description': 'The kernel specifying the covariance model.\n\nIf ``None``, use a Matérn(2.5).', 'title': 'Kernel'}, 'bounds': {'anyOf': [{'items': {}, 'type': 'array'}, {'maxItems': 2, 'minItems': 2, 'prefixItems': [{'type': 'number'}, {'type': 'number'}], 'type': 'array'}, {'additionalProperties': {'maxItems': 2, 'minItems': 2, 'prefixItems': [{'type': 'number'}, {'type': 'number'}], 'type': 'array'}, 'type': 'object'}], 'default': [], 'description': 'The lower and upper bounds of the length scales.\n\nEither a unique lower-upper pair common to all the inputs\nor lower-upper pairs for some of them.\nWhen ``bounds`` is empty or when an input has no pair,\nthe lower bound is 0.01 and the upper bound is 100.\n\nThis argument is ignored when ``kernel`` is ``None``.', 'title': 'Bounds'}, 'alpha': {'default': 1e-10, 'description': 'The nugget effect to regularize the model.', 'title': 'Alpha', 'type': 'number'}, 'optimizer': {'anyOf': [{'type': 'string'}, {}], 'default': 'fmin_l_bfgs_b', 'description': 'The optimization algorithm to find the parameter length scales.', 'title': 'Optimizer'}, 'n_restarts_optimizer': {'default': 10, 'description': 'The number of restarts of the optimizer.', 'minimum': 0, 'title': 'N Restarts Optimizer', 'type': 'integer'}, 'random_state': {'anyOf': [{'minimum': 0, 'type': 'integer'}, {'type': 'null'}], 'default': 0, 'description': 'The random state parameter.\n\nIf ``None``, use the global random state instance from ``numpy.random``.\nCreating the model multiple times will produce different results.\nIf ``int``, use a new random number generator seeded by this integer.\nThis will produce the same results.', 'title': 'Random State'}}, 'title': 'GaussianProcessRegressor_Settings', 'type': 'object'}
Create regression model#
expressions = {"y_1": "1+2*x_1+3*x_2", "y_2": "-1-2*x_1-3*x_2"}
discipline = create_discipline(
"AnalyticDiscipline", name="func", expressions=expressions
)
design_space = create_design_space()
design_space.add_variable("x_1", lower_bound=0.0, upper_bound=1.0)
design_space.add_variable("x_2", lower_bound=0.0, upper_bound=1.0)
scenario = create_scenario(
[discipline],
"y_1",
design_space,
scenario_type="DOE",
formulation_name="DisciplinaryOpt",
)
scenario.execute(algo_name="PYDOE_FULLFACT", n_samples=9)
dataset = scenario.to_dataset(opt_naming=False)
model = create_regression_model("LinearRegressor", data=dataset)
model.learn()
model
INFO - 08:37:42:
INFO - 08:37:42: *** Start DOEScenario execution ***
INFO - 08:37:42: DOEScenario
INFO - 08:37:42: Disciplines: func
INFO - 08:37:42: MDO formulation: DisciplinaryOpt
INFO - 08:37:42: Optimization problem:
INFO - 08:37:42: minimize y_1(x_1, x_2)
INFO - 08:37:42: with respect to x_1, x_2
INFO - 08:37:42: over the design space:
INFO - 08:37:42: +------+-------------+-------+-------------+-------+
INFO - 08:37:42: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:37:42: +------+-------------+-------+-------------+-------+
INFO - 08:37:42: | x_1 | 0 | None | 1 | float |
INFO - 08:37:42: | x_2 | 0 | None | 1 | float |
INFO - 08:37:42: +------+-------------+-------+-------------+-------+
INFO - 08:37:42: Solving optimization problem with algorithm PYDOE_FULLFACT:
INFO - 08:37:42: 11%|█ | 1/9 [00:00<00:00, 391.41 it/sec, obj=1]
INFO - 08:37:42: 22%|██▏ | 2/9 [00:00<00:00, 654.95 it/sec, obj=2]
INFO - 08:37:42: 33%|███▎ | 3/9 [00:00<00:00, 849.34 it/sec, obj=3]
INFO - 08:37:42: 44%|████▍ | 4/9 [00:00<00:00, 1008.79 it/sec, obj=2.5]
INFO - 08:37:42: 56%|█████▌ | 5/9 [00:00<00:00, 1136.79 it/sec, obj=3.5]
INFO - 08:37:42: 67%|██████▋ | 6/9 [00:00<00:00, 1241.53 it/sec, obj=4.5]
INFO - 08:37:42: 78%|███████▊ | 7/9 [00:00<00:00, 1328.03 it/sec, obj=4]
INFO - 08:37:42: 89%|████████▉ | 8/9 [00:00<00:00, 1406.36 it/sec, obj=5]
INFO - 08:37:42: 100%|██████████| 9/9 [00:00<00:00, 1474.39 it/sec, obj=6]
INFO - 08:37:42: Optimization result:
INFO - 08:37:42: Optimizer info:
INFO - 08:37:42: Status: None
INFO - 08:37:42: Message: None
INFO - 08:37:42: Number of calls to the objective function by the optimizer: 9
INFO - 08:37:42: Solution:
INFO - 08:37:42: Objective: 1.0
INFO - 08:37:42: Design space:
INFO - 08:37:42: +------+-------------+-------+-------------+-------+
INFO - 08:37:42: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:37:42: +------+-------------+-------+-------------+-------+
INFO - 08:37:42: | x_1 | 0 | 0 | 1 | float |
INFO - 08:37:42: | x_2 | 0 | 0 | 1 | float |
INFO - 08:37:42: +------+-------------+-------+-------------+-------+
INFO - 08:37:42: *** End DOEScenario execution (time: 0:00:00.010274) ***
Total running time of the script: (0 minutes 0.049 seconds)