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', 'LinearRegressor', 'MOERegressor', 'PCERegressor', 'PolynomialRegressor', 'RBFRegressor', 'RandomForestRegressor']

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 parameter length scales when ``kernel`` is ``none``.   |            null           |
|                           | either a unique lower-upper pair common to all the inputs or lower-upper pairs for some of |                           |
|                           |  them. when ``bounds`` is ``none`` or when an input has no pair, the lower bound is 0.01   |                           |
|                           |                                and the upper bound is 100.                                 |                           |
|        input_names        |   The names of the input variables. if ``none``, consider all the input variables of the   |            null           |
|                           |                                     learning dataset.                                      |                           |
|           kernel          |        The kernel specifying the covariance model. if ``none``, use a matérn(2.5).         |            null           |
|    n_restarts_optimizer   |                          The number of restarts of the optimizer.                          |          integer          |
|         optimizer         |              The optimization algorithm to find the parameter length scales.               |           string          |
|        output_names       |  The names of the output variables. if ``none``, consider all the output variables of the  |            null           |
|                           |                                     learning dataset.                                      |                           |
|        random_state       |  The random state passed to the random number generator. use an integer for reproducible   |          integer          |
|                           |                                          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:58:10: +---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
    INFO - 08:58:10: |            Name           |                                        Description                                         |            Type           |
    INFO - 08:58:10: +---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
    INFO - 08:58:10: |           alpha           |                         The nugget effect to regularize the model.                         |           number          |
    INFO - 08:58:10: |           bounds          |   The lower and upper bounds of the parameter length scales when ``kernel`` is ``none``.   |            null           |
    INFO - 08:58:10: |                           | either a unique lower-upper pair common to all the inputs or lower-upper pairs for some of |                           |
    INFO - 08:58:10: |                           |  them. when ``bounds`` is ``none`` or when an input has no pair, the lower bound is 0.01   |                           |
    INFO - 08:58:10: |                           |                                and the upper bound is 100.                                 |                           |
    INFO - 08:58:10: |        input_names        |   The names of the input variables. if ``none``, consider all the input variables of the   |            null           |
    INFO - 08:58:10: |                           |                                     learning dataset.                                      |                           |
    INFO - 08:58:10: |           kernel          |        The kernel specifying the covariance model. if ``none``, use a matérn(2.5).         |            null           |
    INFO - 08:58:10: |    n_restarts_optimizer   |                          The number of restarts of the optimizer.                          |          integer          |
    INFO - 08:58:10: |         optimizer         |              The optimization algorithm to find the parameter length scales.               |           string          |
    INFO - 08:58:10: |        output_names       |  The names of the output variables. if ``none``, consider all the output variables of the  |            null           |
    INFO - 08:58:10: |                           |                                     learning dataset.                                      |                           |
    INFO - 08:58:10: |        random_state       |  The random state passed to the random number generator. use an integer for reproducible   |          integer          |
    INFO - 08:58:10: |                           |                                          results.                                          |                           |
    INFO - 08:58:10: |        transformer        |           The strategies to transform the variables. the values are instances of           |           object          |
    INFO - 08:58:10: |                           |   :class:`.basetransformer` while the keys are the names of either the variables or the    |                           |
    INFO - 08:58:10: |                           |   groups of variables, e.g. ``"inputs"`` or ``"outputs"`` in the case of the regression    |                           |
    INFO - 08:58:10: |                           | algorithms. if a group is specified, the :class:`.basetransformer` will be applied to all  |                           |
    INFO - 08:58:10: |                           |     the variables of this group. if :attr:`.identity`, do not transform the variables.     |                           |
    INFO - 08:58:10: +---------------------------+--------------------------------------------------------------------------------------------+---------------------------+

{'$schema': 'http://json-schema.org/schema#', 'type': 'object', 'properties': {'transformer': {'description': 'The strategies to transform the variables. The values are instances of :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.', 'type': 'object'}, 'input_names': {'description': 'The names of the input variables. If ``None``, consider all the input variables of the learning dataset.', 'type': 'null'}, 'output_names': {'description': 'The names of the output variables. If ``None``, consider all the output variables of the learning dataset.', 'type': 'null'}, 'kernel': {'description': 'The kernel specifying the covariance model. If ``None``, use a Matérn(2.5).', 'type': 'null'}, 'bounds': {'description': 'The lower and upper bounds of the parameter length scales when ``kernel`` is ``None``. Either a unique lower-upper pair common to all the inputs or lower-upper pairs for some of them. When ``bounds`` is ``None`` or when an input has no pair, the lower bound is 0.01 and the upper bound is 100.', 'type': 'null'}, 'alpha': {'description': 'The nugget effect to regularize the model.', 'type': 'number'}, 'optimizer': {'description': 'The optimization algorithm to find the parameter length scales.', 'type': 'string'}, 'n_restarts_optimizer': {'description': 'The number of restarts of the optimizer.', 'type': 'integer'}, 'random_state': {'description': 'The random state passed to the random number generator. Use an integer for reproducible results.', 'type': 'integer'}}}

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", l_b=0.0, u_b=1.0)
design_space.add_variable("x_2", l_b=0.0, u_b=1.0)

scenario = create_scenario(
    [discipline], "DisciplinaryOpt", "y_1", design_space, scenario_type="DOE"
)
scenario.execute({"algo": "fullfact", "n_samples": 9})

dataset = scenario.to_dataset(opt_naming=False)
model = create_regression_model("LinearRegressor", data=dataset)
model.learn()
model
INFO - 08:58:10:
INFO - 08:58:10: *** Start DOEScenario execution ***
INFO - 08:58:10: DOEScenario
INFO - 08:58:10:    Disciplines: func
INFO - 08:58:10:    MDO formulation: DisciplinaryOpt
INFO - 08:58:10: Optimization problem:
INFO - 08:58:10:    minimize y_1(x_1, x_2)
INFO - 08:58:10:    with respect to x_1, x_2
INFO - 08:58:10:    over the design space:
INFO - 08:58:10:       +------+-------------+-------+-------------+-------+
INFO - 08:58:10:       | Name | Lower bound | Value | Upper bound | Type  |
INFO - 08:58:10:       +------+-------------+-------+-------------+-------+
INFO - 08:58:10:       | x_1  |      0      |  None |      1      | float |
INFO - 08:58:10:       | x_2  |      0      |  None |      1      | float |
INFO - 08:58:10:       +------+-------------+-------+-------------+-------+
INFO - 08:58:10: Solving optimization problem with algorithm fullfact:
INFO - 08:58:10:     11%|█         | 1/9 [00:00<00:00, 368.67 it/sec, obj=1]
INFO - 08:58:10:     22%|██▏       | 2/9 [00:00<00:00, 602.15 it/sec, obj=2]
INFO - 08:58:10:     33%|███▎      | 3/9 [00:00<00:00, 770.21 it/sec, obj=3]
INFO - 08:58:10:     44%|████▍     | 4/9 [00:00<00:00, 895.79 it/sec, obj=2.5]
INFO - 08:58:10:     56%|█████▌    | 5/9 [00:00<00:00, 998.69 it/sec, obj=3.5]
INFO - 08:58:10:     67%|██████▋   | 6/9 [00:00<00:00, 1081.98 it/sec, obj=4.5]
INFO - 08:58:10:     78%|███████▊  | 7/9 [00:00<00:00, 1145.72 it/sec, obj=4]
INFO - 08:58:10:     89%|████████▉ | 8/9 [00:00<00:00, 1202.45 it/sec, obj=5]
INFO - 08:58:10:    100%|██████████| 9/9 [00:00<00:00, 1241.57 it/sec, obj=6]
INFO - 08:58:10: Optimization result:
INFO - 08:58:10:    Optimizer info:
INFO - 08:58:10:       Status: None
INFO - 08:58:10:       Message: None
INFO - 08:58:10:       Number of calls to the objective function by the optimizer: 9
INFO - 08:58:10:    Solution:
INFO - 08:58:10:       Objective: 1.0
INFO - 08:58:10:       Design space:
INFO - 08:58:10:          +------+-------------+-------+-------------+-------+
INFO - 08:58:10:          | Name | Lower bound | Value | Upper bound | Type  |
INFO - 08:58:10:          +------+-------------+-------+-------------+-------+
INFO - 08:58:10:          | x_1  |      0      |   0   |      1      | float |
INFO - 08:58:10:          | x_2  |      0      |   0   |      1      | float |
INFO - 08:58:10:          +------+-------------+-------+-------------+-------+
INFO - 08:58:10: *** End DOEScenario execution (time: 0:00:00.018071) ***
LinearRegressor(fit_intercept=True, l2_penalty_ratio=1.0, penalty_level=0.0, random_state=0)
  • based on the scikit-learn library
  • built from 9 learning samples


Total running time of the script: (0 minutes 0.055 seconds)

Gallery generated by Sphinx-Gallery