.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/dataset/plot_dataset_from_optproblem.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_dataset_plot_dataset_from_optproblem.py: Dataset from an optimization problem ==================================== In this example, we will see how to build a :class:`.Dataset` from objects of an :class:`.OptimizationProblem`. For that, we need to import this :class:`.Dataset` class: .. GENERATED FROM PYTHON SOURCE LINES 31-42 .. code-block:: default from __future__ import absolute_import, division, print_function, unicode_literals from future import standard_library from gemseo.api import configure_logger, create_discipline, create_scenario from gemseo.problems.sellar.sellar_design_space import SellarDesignSpace configure_logger() standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 43-47 Synthetic data -------------- We can sample the :class:`.Sellar1` discipline and use the corresponding :class:`.OptimizationProblem`: .. GENERATED FROM PYTHON SOURCE LINES 47-57 .. code-block:: default discipline = create_discipline("Sellar1") design_space = SellarDesignSpace().filter(discipline.get_input_data_names()) scenario = create_scenario( [discipline], "DisciplinaryOpt", "y_0", design_space, scenario_type="DOE" ) scenario.execute({"algo": "lhs", "n_samples": 5}) opt_problem = scenario.formulation.opt_problem .. GENERATED FROM PYTHON SOURCE LINES 58-63 Create a dataset ---------------- We can easily build a dataset from this :class:`.OptimizationProblem`: either by separating the design parameters from the function (default option): .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: default dataset = opt_problem.export_to_dataset("sellar1_doe") print(dataset) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none sellar1_doe | Number of samples: 5 | Number of variables: 4 | Variables names and sizes by group: | - design_parameters: x_local (1), x_shared (2), y_1 (1) | - functions: y_0 (1) | Number of dimensions (total = 5) by group: | - design_parameters: 4 | - functions: 1 .. GENERATED FROM PYTHON SOURCE LINES 66-67 or by considering all features as default parameters: .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: default dataset = opt_problem.export_to_dataset("sellar1_doe", categorize=False) print(dataset) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none sellar1_doe | Number of samples: 5 | Number of variables: 4 | Variables names and sizes by group: | - parameters: x_local (1), x_shared (2), y_1 (1), y_0 (1) | Number of dimensions (total = 5) by group: | - parameters: 5 .. GENERATED FROM PYTHON SOURCE LINES 70-71 or by using an input-output naming rather than an optimization naming: .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: default dataset = opt_problem.export_to_dataset("sellar1_doe", opt_naming=False) print(dataset) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none sellar1_doe | Number of samples: 5 | Number of variables: 4 | Variables names and sizes by group: | - inputs: x_local (1), x_shared (2), y_1 (1) | - outputs: y_0 (1) | Number of dimensions (total = 5) by group: | - inputs: 4 | - outputs: 1 .. GENERATED FROM PYTHON SOURCE LINES 75-77 Access properties ----------------- .. GENERATED FROM PYTHON SOURCE LINES 77-78 .. code-block:: default dataset = opt_problem.export_to_dataset("sellar1_doe") .. GENERATED FROM PYTHON SOURCE LINES 79-82 Variables names ~~~~~~~~~~~~~~~ We can access the variables names: .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: default print(dataset.variables) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['x_local', 'x_shared', 'y_0', 'y_1'] .. GENERATED FROM PYTHON SOURCE LINES 85-88 Variables sizes ~~~~~~~~~~~~~~~ We can access the variables sizes: .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: default print(dataset.sizes) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x_local': 1, 'x_shared': 2, 'y_1': 1, 'y_0': 1} .. GENERATED FROM PYTHON SOURCE LINES 91-94 Variables groups ~~~~~~~~~~~~~~~~ We can access the variables groups: .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: default print(dataset.groups) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['design_parameters', 'functions'] .. GENERATED FROM PYTHON SOURCE LINES 97-102 Access data ----------- Access by group ~~~~~~~~~~~~~~~ We can get the data by group, either as an array (default option): .. GENERATED FROM PYTHON SOURCE LINES 102-103 .. code-block:: default print(dataset.get_data_by_group("design_parameters")) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[ 1.81452514 -3.72798685 7.53919754 38.41579806] [ 3.92826786 -0.48697712 2.88367228 82.15592618] [ 8.83368465 5.64262221 5.85431838 -88.87436758] [ 5.27550899 6.37407839 0.86650059 -33.1047004 ] [ 6.74914349 -8.20498248 9.36072454 -17.76739276]] .. GENERATED FROM PYTHON SOURCE LINES 104-105 or as a dictionary indexed by the variables names: .. GENERATED FROM PYTHON SOURCE LINES 105-107 .. code-block:: default print(dataset.get_data_by_group("design_parameters", True)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x_local': array([[1.81452514], [3.92826786], [8.83368465], [5.27550899], [6.74914349]]), 'x_shared': array([[-3.72798685, 7.53919754], [-0.48697712, 2.88367228], [ 5.64262221, 5.85431838], [ 6.37407839, 0.86650059], [-8.20498248, 9.36072454]]), 'y_1': array([[ 38.41579806], [ 82.15592618], [-88.87436758], [-33.1047004 ], [-17.76739276]])} .. GENERATED FROM PYTHON SOURCE LINES 108-112 Access by variable name ~~~~~~~~~~~~~~~~~~~~~~~ We can get the data by variables names, either as a dictionary indexed by the variables names (default option): .. GENERATED FROM PYTHON SOURCE LINES 112-113 .. code-block:: default print(dataset.get_data_by_names(["x_shared", "y_1"])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x_shared': array([[-3.72798685, 7.53919754], [-0.48697712, 2.88367228], [ 5.64262221, 5.85431838], [ 6.37407839, 0.86650059], [-8.20498248, 9.36072454]]), 'y_1': array([[ 38.41579806], [ 82.15592618], [-88.87436758], [-33.1047004 ], [-17.76739276]])} .. GENERATED FROM PYTHON SOURCE LINES 114-115 or as an array: .. GENERATED FROM PYTHON SOURCE LINES 115-117 .. code-block:: default print(dataset.get_data_by_names(["x_shared", "y_1"], False)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[ -3.72798685 7.53919754 38.41579806] [ -0.48697712 2.88367228 82.15592618] [ 5.64262221 5.85431838 -88.87436758] [ 6.37407839 0.86650059 -33.1047004 ] [ -8.20498248 9.36072454 -17.76739276]] .. GENERATED FROM PYTHON SOURCE LINES 118-121 Access all data ~~~~~~~~~~~~~~~ We can get all the data, either as a large array: .. GENERATED FROM PYTHON SOURCE LINES 121-122 .. code-block:: default print(dataset.get_all_data()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ({'design_parameters': array([[ 1.81452514, -3.72798685, 7.53919754, 38.41579806], [ 3.92826786, -0.48697712, 2.88367228, 82.15592618], [ 8.83368465, 5.64262221, 5.85431838, -88.87436758], [ 5.27550899, 6.37407839, 0.86650059, -33.1047004 ], [ 6.74914349, -8.20498248, 9.36072454, -17.76739276]]), 'functions': array([[3.9456874 ], [0. ], [8.01885665], [7.30697098], [9.32657944]])}, {'design_parameters': ['x_local', 'x_shared', 'y_1'], 'functions': ['y_0']}, {'x_local': 1, 'x_shared': 2, 'y_1': 1, 'y_0': 1}) .. GENERATED FROM PYTHON SOURCE LINES 123-124 or as a dictionary indexed by variables names: .. GENERATED FROM PYTHON SOURCE LINES 124-125 .. code-block:: default print(dataset.get_all_data(as_dict=True)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'design_parameters': {'x_local': array([[1.81452514], [3.92826786], [8.83368465], [5.27550899], [6.74914349]]), 'x_shared': array([[-3.72798685, 7.53919754], [-0.48697712, 2.88367228], [ 5.64262221, 5.85431838], [ 6.37407839, 0.86650059], [-8.20498248, 9.36072454]]), 'y_1': array([[ 38.41579806], [ 82.15592618], [-88.87436758], [-33.1047004 ], [-17.76739276]])}, 'functions': {'y_0': array([[3.9456874 ], [0. ], [8.01885665], [7.30697098], [9.32657944]])}} .. GENERATED FROM PYTHON SOURCE LINES 126-128 We can get these data sorted by category, either with a large array for each category: .. GENERATED FROM PYTHON SOURCE LINES 128-129 .. code-block:: default print(dataset.get_all_data(by_group=False)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (array([[ 1.81452514, -3.72798685, 7.53919754, 38.41579806, 3.9456874 ], [ 3.92826786, -0.48697712, 2.88367228, 82.15592618, 0. ], [ 8.83368465, 5.64262221, 5.85431838, -88.87436758, 8.01885665], [ 5.27550899, 6.37407839, 0.86650059, -33.1047004 , 7.30697098], [ 6.74914349, -8.20498248, 9.36072454, -17.76739276, 9.32657944]]), ['x_local', 'x_shared', 'y_1', 'y_0'], {'x_local': 1, 'x_shared': 2, 'y_1': 1, 'y_0': 1}) .. GENERATED FROM PYTHON SOURCE LINES 130-131 or with a dictionary of variables names: .. GENERATED FROM PYTHON SOURCE LINES 131-132 .. code-block:: default print(dataset.get_all_data(by_group=False, as_dict=True)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x_local': array([[1.81452514], [3.92826786], [8.83368465], [5.27550899], [6.74914349]]), 'x_shared': array([[-3.72798685, 7.53919754], [-0.48697712, 2.88367228], [ 5.64262221, 5.85431838], [ 6.37407839, 0.86650059], [-8.20498248, 9.36072454]]), 'y_1': array([[ 38.41579806], [ 82.15592618], [-88.87436758], [-33.1047004 ], [-17.76739276]]), 'y_0': array([[3.9456874 ], [0. ], [8.01885665], [7.30697098], [9.32657944]])} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.047 seconds) .. _sphx_glr_download_examples_dataset_plot_dataset_from_optproblem.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_dataset_from_optproblem.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dataset_from_optproblem.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_