.. 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-37 .. code-block:: default from __future__ import annotations from gemseo import configure_logger from gemseo.datasets.dataset import Dataset from numpy import concatenate from numpy.random import rand configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-46 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 46-51 .. code-block:: default n_samples = 5 inputs = rand(n_samples, 3) outputs = rand(n_samples, 3) data = concatenate((inputs, outputs), 1) .. GENERATED FROM PYTHON SOURCE LINES 52-56 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 56-59 .. code-block:: default 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.631043 0.019469 0.733367 0.884586 0.705865 0.491662
1 0.902215 0.310880 0.021208 0.039995 0.773718 0.912503
2 0.408196 0.217063 0.603613 0.488699 0.792577 0.203123
3 0.293174 0.945196 0.110868 0.176658 0.650542 0.962812
4 0.493227 0.437936 0.655254 0.565650 0.567926 0.386245


.. GENERATED FROM PYTHON SOURCE LINES 60-63 A dataset with custom names ~~~~~~~~~~~~~~~~~~~~~~~~~~~ We can also pass the names and sizes of the variables: .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: default 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.631043 0.019469 0.733367 0.884586 0.705865 0.491662
1 0.902215 0.310880 0.021208 0.039995 0.773718 0.912503
2 0.408196 0.217063 0.603613 0.488699 0.792577 0.203123
3 0.293174 0.945196 0.110868 0.176658 0.650542 0.962812
4 0.493227 0.437936 0.655254 0.565650 0.567926 0.386245


.. GENERATED FROM PYTHON SOURCE LINES 68-74 .. 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 76-79 A dataset with custom groups ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We can also use the notions of groups of variables: .. GENERATED FROM PYTHON SOURCE LINES 79-83 .. code-block:: default 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.631043 0.019469 0.733367 0.884586 0.705865 0.491662
1 0.902215 0.310880 0.021208 0.039995 0.773718 0.912503
2 0.408196 0.217063 0.603613 0.488699 0.792577 0.203123
3 0.293174 0.945196 0.110868 0.176658 0.650542 0.962812
4 0.493227 0.437936 0.655254 0.565650 0.567926 0.386245


.. GENERATED FROM PYTHON SOURCE LINES 84-90 .. 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.018 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-python :download:`Download Python source code: plot_dataset_from_array.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dataset_from_array.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_