Note
Click here to download the full example code
API¶
Here are some examples of the machine learning API applied to regression models.
from __future__ import division, unicode_literals
from gemseo.api import (
configure_logger,
create_design_space,
create_discipline,
create_scenario,
)
from gemseo.mlearning.api import (
create_regression_model,
get_regression_models,
get_regression_options,
)
configure_logger()
Out:
<RootLogger root (INFO)>
Get available regression models¶
print(get_regression_models())
Out:
['GaussianProcessRegression', 'LinearRegression', 'MixtureOfExperts', 'PCERegression', 'PolynomialRegression', 'RBFRegression', 'RandomForestRegressor']
Get regression model options¶
print(get_regression_options("GaussianProcessRegression"))
Out:
{'$schema': 'http://json-schema.org/schema#', 'type': 'object', 'properties': {'transformer': {'type': 'null'}, 'input_names': {'type': 'null'}, 'output_names': {'type': 'null'}, 'kernel': {'description': 'The kernel function. If None, use a ``Matern(2.5)``.', 'type': 'null'}, 'alpha': {'description': 'The nugget effect to regularize the model.', 'type': 'number'}, 'optimizer': {'description': 'The optimization algorithm to find the hyperparameters.', 'type': 'string'}, 'n_restarts_optimizer': {'description': 'The number of restarts of the optimizer.', 'type': 'integer'}, 'random_state': {'description': 'The seed used to initialize the centers. If None, the random number generator is the RandomState instance used by `numpy.random`.', 'type': 'null'}}, 'required': ['alpha', 'n_restarts_optimizer', 'optimizer']}
Create regression model¶
expressions_dict = {"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_dict=expressions_dict
)
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)
discipline.set_cache_policy(discipline.MEMORY_FULL_CACHE)
scenario = create_scenario(
[discipline], "DisciplinaryOpt", "y_1", design_space, scenario_type="DOE"
)
scenario.execute({"algo": "fullfact", "n_samples": 9})
dataset = discipline.cache.export_to_dataset()
model = create_regression_model("LinearRegression", data=dataset)
model.learn()
print(model)
Out:
INFO - 09:23:13:
INFO - 09:23:13: *** Start DOE Scenario execution ***
INFO - 09:23:13: DOEScenario
INFO - 09:23:13: Disciplines: func
INFO - 09:23:13: MDOFormulation: DisciplinaryOpt
INFO - 09:23:13: Algorithm: fullfact
INFO - 09:23:13: Optimization problem:
INFO - 09:23:13: Minimize: y_1(x_1, x_2)
INFO - 09:23:13: With respect to: x_1, x_2
INFO - 09:23:13: Full factorial design required. Number of samples along each direction for a design vector of size 2 with 9 samples: 3
INFO - 09:23:13: Final number of samples for DOE = 9 vs 9 requested
INFO - 09:23:13: DOE sampling: 0%| | 0/9 [00:00<?, ?it]
INFO - 09:23:13: DOE sampling: 100%|██████████| 9/9 [00:00<00:00, 417.87 it/sec, obj=6]
INFO - 09:23:13: Optimization result:
INFO - 09:23:13: Objective value = 1.0
INFO - 09:23:13: The result is feasible.
INFO - 09:23:13: Status: None
INFO - 09:23:13: Optimizer message: None
INFO - 09:23:13: Number of calls to the objective function by the optimizer: 9
INFO - 09:23:13: Design Space:
INFO - 09:23:13: +------+-------------+-------+-------------+-------+
INFO - 09:23:13: | name | lower_bound | value | upper_bound | type |
INFO - 09:23:13: +------+-------------+-------+-------------+-------+
INFO - 09:23:13: | x_1 | 0 | 0 | 1 | float |
INFO - 09:23:13: | x_2 | 0 | 0 | 1 | float |
INFO - 09:23:13: +------+-------------+-------+-------------+-------+
INFO - 09:23:13: *** DOE Scenario run terminated ***
LinearRegression(fit_intercept=True, l2_penalty_ratio=1.0, penalty_level=0.0)
based on the scikit-learn library
built from 9 learning samples
Total running time of the script: ( 0 minutes 0.110 seconds)