Linear regression

We want to approximate a discipline with two inputs and two outputs:

  • \(y_1=1+2x_1+3x_2\)

  • \(y_2=-1-2x_1-3x_2\)

over the unit hypercube \([0,1]\times[0,1]\).

Import

from __future__ import annotations

from gemseo.api import configure_logger
from gemseo.api import create_design_space
from gemseo.api import create_discipline
from gemseo.api import create_scenario
from gemseo.mlearning.api import create_regression_model
from numpy import array

configure_logger()
<RootLogger root (INFO)>

Create the discipline to learn

We can implement this analytic discipline by means of the AnalyticDiscipline class.

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
)

Create the input sampling space

We create the input sampling space by adding the variables one by one.

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)

Create the learning set

We can build a learning set by means of a DOEScenario with a full factorial design of experiments. The number of samples can be equal to 9 for example.

scenario = create_scenario(
    [discipline], "DisciplinaryOpt", "y_1", design_space, scenario_type="DOE"
)
scenario.execute({"algo": "fullfact", "n_samples": 9})
    INFO - 16:59:33:
    INFO - 16:59:33: *** Start DOEScenario execution ***
    INFO - 16:59:33: DOEScenario
    INFO - 16:59:33:    Disciplines: func
    INFO - 16:59:33:    MDO formulation: DisciplinaryOpt
    INFO - 16:59:33: Optimization problem:
    INFO - 16:59:33:    minimize y_1(x_1, x_2)
    INFO - 16:59:33:    with respect to x_1, x_2
    INFO - 16:59:33:    over the design space:
    INFO - 16:59:33:    +------+-------------+-------+-------------+-------+
    INFO - 16:59:33:    | name | lower_bound | value | upper_bound | type  |
    INFO - 16:59:33:    +------+-------------+-------+-------------+-------+
    INFO - 16:59:33:    | x_1  |      0      |  None |      1      | float |
    INFO - 16:59:33:    | x_2  |      0      |  None |      1      | float |
    INFO - 16:59:33:    +------+-------------+-------+-------------+-------+
    INFO - 16:59:33: Solving optimization problem with algorithm fullfact:
    INFO - 16:59:33: ...   0%|          | 0/9 [00:00<?, ?it]
    INFO - 16:59:33: ...  11%|█         | 1/9 [00:00<00:00, 353.95 it/sec, obj=1]
    INFO - 16:59:33: ...  22%|██▏       | 2/9 [00:00<00:00, 587.48 it/sec, obj=2]
    INFO - 16:59:33: ...  33%|███▎      | 3/9 [00:00<00:00, 765.48 it/sec, obj=3]
    INFO - 16:59:33: ...  44%|████▍     | 4/9 [00:00<00:00, 903.12 it/sec, obj=2.5]
    INFO - 16:59:33: ...  56%|█████▌    | 5/9 [00:00<00:00, 1012.43 it/sec, obj=3.5]
    INFO - 16:59:33: ...  67%|██████▋   | 6/9 [00:00<00:00, 1079.06 it/sec, obj=4.5]
    INFO - 16:59:33: ...  78%|███████▊  | 7/9 [00:00<00:00, 1138.03 it/sec, obj=4]
    INFO - 16:59:33: ...  89%|████████▉ | 8/9 [00:00<00:00, 1192.92 it/sec, obj=5]
    INFO - 16:59:33: ... 100%|██████████| 9/9 [00:00<00:00, 1247.81 it/sec, obj=6]
    INFO - 16:59:33: Optimization result:
    INFO - 16:59:33:    Optimizer info:
    INFO - 16:59:33:       Status: None
    INFO - 16:59:33:       Message: None
    INFO - 16:59:33:       Number of calls to the objective function by the optimizer: 9
    INFO - 16:59:33:    Solution:
    INFO - 16:59:33:       Objective: 1.0
    INFO - 16:59:33:       Design space:
    INFO - 16:59:33:       +------+-------------+-------+-------------+-------+
    INFO - 16:59:33:       | name | lower_bound | value | upper_bound | type  |
    INFO - 16:59:33:       +------+-------------+-------+-------------+-------+
    INFO - 16:59:33:       | x_1  |      0      |   0   |      1      | float |
    INFO - 16:59:33:       | x_2  |      0      |   0   |      1      | float |
    INFO - 16:59:33:       +------+-------------+-------+-------------+-------+
    INFO - 16:59:33: *** End DOEScenario execution (time: 0:00:00.016169) ***

{'eval_jac': False, 'algo': 'fullfact', 'n_samples': 9}

Create the regression model

Then, we build the linear regression model from the database and displays this model.

dataset = scenario.export_to_dataset(opt_naming=False)
model = create_regression_model("LinearRegressor", data=dataset, transformer=None)
model.learn()
print(model)
LinearRegressor(fit_intercept=True, l2_penalty_ratio=1.0, penalty_level=0.0)
   based on the scikit-learn library
   built from 9 learning samples

Predict output

Once it is built, we can use it for prediction.

input_value = {"x_1": array([1.0]), "x_2": array([2.0])}
output_value = model.predict(input_value)
print(output_value)
{'y_1': array([9.])}

Predict jacobian

We can also use it to predict the jacobian of the discipline.

jacobian_value = model.predict_jacobian(input_value)
print(jacobian_value)
{'y_1': {'x_1': array([[2.]]), 'x_2': array([[3.]])}}

Get intercept

In addition, it is possible to access the intercept of the model, either directly or by means of a method returning either a dictionary (default option) or an array.

print(model.intercept)
print(model.get_intercept())
[1.]
{'y_1': [0.9999999999999987]}

Get coefficients

In addition, it is possible to access the coefficients of the model, either directly or by means of a method returning either a dictionary (default option) or an array.

print(model.coefficients)
print(model.get_coefficients())
[[2. 3.]]
{'y_1': [{'x_1': [2.000000000000001], 'x_2': [3.0000000000000018]}]}

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

Gallery generated by Sphinx-Gallery