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 absolute_import, division, print_function, unicode_literals
from future import standard_library
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()
standard_library.install_aliases()
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:
{'type': 'object', 'properties': {'transformer': {'description': 'transformation strategy for data groups.\nIf None, do not transform data. Default: None.\n:type transformer: dict(str)\n'}, 'input_names': {'description': 'names of the input variables.\n:type input_names: list(str)\n'}, 'output_names': {'description': 'names of the output variables.\n:type output_names: list(str)\n'}, 'kernel': {'description': 'kernel function. If None, use a Matern(2.5).\nDefault: None.\n:type kernel: openturns.Kernel\n'}, 'alpha': {'type': 'number', 'description': 'nugget effect. Default: 1e-10.\n:type alpha: float or array\n'}, 'optimizer': {'type': 'string', 'description': "optimization algorithm. Default: 'fmin_l_bfgs_b'.\n:type optimizer: str or callable\n"}, 'n_restarts_optimizer': {'type': 'integer', 'description': 'number of restarts of the optimizer.\nDefault: 10.\n:type n_restarts_optimizer: int\n'}, 'random_state': {'description': 'the seed used to initialize the centers.\nIf None, the random number generator is the RandomState instance\nused by `np.random`\nDefault: None.\n:type random_state: int'}}, '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:
LinearRegression(fit_intercept=True, penalty_level=0.0, l2_penalty_ratio=1.0)
| based on the scikit-learn library
| built from 9 learning samples
Total running time of the script: ( 0 minutes 0.102 seconds)