.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/dataset/creation/plot_dataset_from_array.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_dataset_creation_plot_dataset_from_array.py: Dataset from a NumPy array ========================== In this example, we will see how to build a :class:`.Dataset` from an NumPy array. .. GENERATED FROM PYTHON SOURCE LINES 27-40 .. code-block:: Python from __future__ import annotations from numpy import concatenate from numpy.random import default_rng from gemseo import configure_logger from gemseo.datasets.dataset import Dataset configure_logger() rng = default_rng(1) .. GENERATED FROM PYTHON SOURCE LINES 41-49 Let us consider three parameters :math:`x_1`, :math:`x_2` and :math:`x_3` of size 1, 2 and 3 respectively. We generate 5 random samples of the inputs where - x_1 is stored in the first column, - x_2 is stored in the 2nd and 3rd columns and 5 random samples of the outputs: .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: Python n_samples = 5 inputs = rng.random((n_samples, 3)) outputs = rng.random((n_samples, 3)) data = concatenate((inputs, outputs), 1) .. GENERATED FROM PYTHON SOURCE LINES 55-59 A dataset with default names ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We create a :class:`.Dataset` from the NumPy array only and let GEMSEO give default names to its columns: .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python dataset = Dataset.from_array(data) dataset .. raw:: html
GROUP parameters
VARIABLE x_0 x_1 x_2 x_3 x_4 x_5
COMPONENT 0 0 0 0 0 0
0 0.511822 0.950464 0.144160 0.453498 0.134042 0.403113
1 0.948649 0.311831 0.423326 0.203455 0.262313 0.750365
2 0.827703 0.409199 0.549594 0.280409 0.485191 0.980737
3 0.027559 0.753513 0.538143 0.961657 0.724790 0.541227
4 0.329732 0.788429 0.303195 0.276891 0.160652 0.969925


.. GENERATED FROM PYTHON SOURCE LINES 63-66 A dataset with custom names ~~~~~~~~~~~~~~~~~~~~~~~~~~~ We can also pass the names and sizes of the variables: .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: Python names_to_sizes = {"x_1": 1, "x_2": 2, "y_1": 3} dataset = Dataset.from_array(data, ["x_1", "x_2", "y_1"], names_to_sizes) dataset .. raw:: html
GROUP parameters
VARIABLE x_1 x_2 y_1
COMPONENT 0 0 1 0 1 2
0 0.511822 0.950464 0.144160 0.453498 0.134042 0.403113
1 0.948649 0.311831 0.423326 0.203455 0.262313 0.750365
2 0.827703 0.409199 0.549594 0.280409 0.485191 0.980737
3 0.027559 0.753513 0.538143 0.961657 0.724790 0.541227
4 0.329732 0.788429 0.303195 0.276891 0.160652 0.969925


.. GENERATED FROM PYTHON SOURCE LINES 71-77 .. warning:: The number of variables names must be equal to the number of columns of the data array. Otherwise, the user has to specify the sizes of the different variables by means of a dictionary and be careful that the total size is equal to this number of columns. .. GENERATED FROM PYTHON SOURCE LINES 79-82 A dataset with custom groups ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We can also use the notions of groups of variables: .. GENERATED FROM PYTHON SOURCE LINES 82-86 .. code-block:: Python groups = {"x_1": "inputs", "x_2": "inputs", "y_1": "outputs"} dataset = Dataset.from_array(data, ["x_1", "x_2", "y_1"], names_to_sizes, groups) dataset .. raw:: html
GROUP inputs outputs
VARIABLE x_1 x_2 y_1
COMPONENT 0 0 1 0 1 2
0 0.511822 0.950464 0.144160 0.453498 0.134042 0.403113
1 0.948649 0.311831 0.423326 0.203455 0.262313 0.750365
2 0.827703 0.409199 0.549594 0.280409 0.485191 0.980737
3 0.027559 0.753513 0.538143 0.961657 0.724790 0.541227
4 0.329732 0.788429 0.303195 0.276891 0.160652 0.969925


.. GENERATED FROM PYTHON SOURCE LINES 87-93 .. note:: The groups are specified by means of a dictionary where indices are the variables names and values are the groups. If a variable is missing, the default group :attr:`.Dataset.DEFAULT_GROUP` is considered. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.015 seconds) .. _sphx_glr_download_examples_dataset_creation_plot_dataset_from_array.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dataset_from_array.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_dataset_from_array.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_