.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/manage_data_and_samples/plot_quick_start_point_and_sample.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_auto_data_analysis_manage_data_and_samples_plot_quick_start_point_and_sample.py: A quick start guide to the `Point` and `Sample` classes ======================================================= .. GENERATED FROM PYTHON SOURCE LINES 7-14 Abstract -------- In this example, we present the :class:`~openturns.Point` and :class:`~openturns.Sample` classes, two fundamental objects in the library. We present the principles behind these classes and the way to create and use these objects. We show how to extract a row or a column with the slicing operator. We show how these objects interact with Python variables and with the Numpy module. .. GENERATED FROM PYTHON SOURCE LINES 16-24 Introduction ------------ Two fundamental objects in the library are: * :class:`~openturns.Point`: a multidimensional point in :math:`d` dimensions (:math:`\in \Rset^d`) ; * :class:`~openturns.Sample`: a multivariate sample made of :math:`n` points in :math:`d` dimensions. .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: Python import numpy as np import openturns as ot .. GENERATED FROM PYTHON SOURCE LINES 31-40 The `Point` class ----------------- In this section, we see how to: * create a point in :math:`\mathbb{R}^3`, * access its components, * update its components. .. GENERATED FROM PYTHON SOURCE LINES 42-43 By default, points are filled with zeros. .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python p = ot.Point(3) p .. raw:: html
class=Point name=Unnamed dimension=3 values=[0,0,0]


.. GENERATED FROM PYTHON SOURCE LINES 49-51 The following statement returns the value of the second component (with index 1). Python beginners should remember that Python indices start at zero. .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python p[1] .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 56-57 The following statement sets the second component. .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python p[1] = 2 p .. raw:: html
class=Point name=Unnamed dimension=3 values=[0,2,0]


.. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python p.getDimension() .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 66-78 The `Sample` class ------------------ The :class:`~openturns.Sample` class represents a multivariate sample made of :math:`\sampleSize` points in :math:`\Rset^d`. * :math:`d` is the *dimension* of the sample, * :math:`n` is the *size* of the sample. A `Sample` can be seen as an array of with :math:`n` rows and :math:`d` columns. *Remark.* The :class:`~openturns.ProcessSample` class can be used to manage a sample of stochastic processes. .. GENERATED FROM PYTHON SOURCE LINES 80-81 The script below creates a `Sample` with size :math:`n=5` and dimension :math:`d=3`. .. GENERATED FROM PYTHON SOURCE LINES 83-86 .. code-block:: Python data = ot.Sample(5, 3) data .. raw:: html
v0v1v2
0000
1000
2000
3000
4000


.. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python data.getSize() .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: Python data.getDimension() .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 93-94 The following statement sets the third component (with index 2) of the fourth point (with index 3) in the `Sample`. .. GENERATED FROM PYTHON SOURCE LINES 96-99 .. code-block:: Python data[3, 2] = 32 data .. raw:: html
v0v1v2
0000
1000
2000
30032
4000


.. GENERATED FROM PYTHON SOURCE LINES 100-101 Notice that the rendering is different when we use the `print` statement. .. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: Python print(data) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 : [ 0 0 0 ] 1 : [ 0 0 0 ] 2 : [ 0 0 0 ] 3 : [ 0 0 32 ] 4 : [ 0 0 0 ] .. GENERATED FROM PYTHON SOURCE LINES 106-108 We can customize the format used to print the floating point numbers with the `Sample-PrintFormat` key of the :class:`~openturns.ResourceMap`. .. GENERATED FROM PYTHON SOURCE LINES 110-115 Get a row or a column of a `Sample` ----------------------------------- As with `numpy` arrays, we can extract a row or a column with the `:` slicing operator. As a reminder for Python beginners, *slicing* is the fact of extracting a part of an array with one single statement; this avoids `for` loops and improves performance and readability. .. GENERATED FROM PYTHON SOURCE LINES 117-120 .. code-block:: Python row = data[3, :] row .. raw:: html
class=Point name=Unnamed dimension=3 values=[0,0,32]


.. GENERATED FROM PYTHON SOURCE LINES 121-123 .. code-block:: Python print(type(row)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 124-127 .. code-block:: Python column = data[:, 2] column .. raw:: html
v2
00
10
20
332
40


.. GENERATED FROM PYTHON SOURCE LINES 128-130 .. code-block:: Python print(type(column)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 131-137 We see that: * the `row` is a `Point`, * the `column` is a `Sample`. This is consistent with the fact that, in a dimension :math:`d`, a row is a :math:`d`-dimensional `Point`. .. GENERATED FROM PYTHON SOURCE LINES 139-140 The following statement extracts several columns (with indices 0 and 2) and creates a new `Sample`. .. GENERATED FROM PYTHON SOURCE LINES 142-144 .. code-block:: Python data.getMarginal([0, 2]) .. raw:: html
v0v1
000
100
200
3032
400


.. GENERATED FROM PYTHON SOURCE LINES 145-147 Set a row or a column of a `Sample` ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 149-150 Slicing can also be used to set a `Sample` row or column. .. GENERATED FROM PYTHON SOURCE LINES 152-155 .. code-block:: Python sample = ot.Sample([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) sample .. raw:: html
v0v1
012
134
256


.. GENERATED FROM PYTHON SOURCE LINES 156-157 Set the third row: this must be a `Point` or must be convertible to. .. GENERATED FROM PYTHON SOURCE LINES 157-161 .. code-block:: Python p = [8.0, 10.0] sample[2, :] = p sample .. raw:: html
v0v1
012
134
2810


.. GENERATED FROM PYTHON SOURCE LINES 162-163 Set the second column: this must be a `Sample` or must be convertible to. .. GENERATED FROM PYTHON SOURCE LINES 163-168 .. code-block:: Python sample = ot.Sample([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) s = ot.Sample([[3.0], [5.0], [7.0]]) sample[:, 1] = s sample .. raw:: html
v0v1
013
135
257


.. GENERATED FROM PYTHON SOURCE LINES 169-171 Sometimes, we want to set a column with a list of floats. This can be done using the :meth:`~openturns.Sample.BuildFromPoint` static method. .. GENERATED FROM PYTHON SOURCE LINES 171-176 .. code-block:: Python sample = ot.Sample([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) s = ot.Sample.BuildFromPoint([3.0, 5.0, 7.0]) sample[:, 1] = s sample .. raw:: html
v0v1
013
135
257


.. GENERATED FROM PYTHON SOURCE LINES 177-181 Create a `Point` or a `Sample` from a Python list ------------------------------------------------- The following statement creates a `Point` from a Python list. .. GENERATED FROM PYTHON SOURCE LINES 183-186 .. code-block:: Python p1 = ot.Point([2, 3]) p1 .. raw:: html
class=Point name=Unnamed dimension=2 values=[2,3]


.. GENERATED FROM PYTHON SOURCE LINES 187-190 .. code-block:: Python p2 = ot.Point(range(2)) p2 .. raw:: html
class=Point name=Unnamed dimension=2 values=[0,1]


.. GENERATED FROM PYTHON SOURCE LINES 191-194 The first useful *Pythonism* that we will review is the *list comprehension*. This creates a list from a `for` loop. This kind of statements is often used in the examples, so that they can be as short as possible. In the following statement, we create a point by iterating over the components of a `Point`. .. GENERATED FROM PYTHON SOURCE LINES 196-199 .. code-block:: Python p3 = ot.Point([i * i for i in p1]) p3 .. raw:: html
class=Point name=Unnamed dimension=2 values=[4,9]


.. GENERATED FROM PYTHON SOURCE LINES 200-203 The second useful *Pythonism* is the repetition with the `*` operator. The following statements creates a list with three 5s. .. GENERATED FROM PYTHON SOURCE LINES 205-208 .. code-block:: Python p4 = [5] * 3 p4 .. rst-class:: sphx-glr-script-out .. code-block:: none [5, 5, 5] .. GENERATED FROM PYTHON SOURCE LINES 209-210 We can also create a `Sample` from a list of `Point`. .. GENERATED FROM PYTHON SOURCE LINES 212-215 .. code-block:: Python sample = ot.Sample([p1, p2, p3]) sample .. raw:: html
v0v1
023
101
249


.. GENERATED FROM PYTHON SOURCE LINES 216-217 We can loop over the points in a sample, using a list comprehension. In the following example, we compute the Euclidian norm of the points in the previous sample. .. GENERATED FROM PYTHON SOURCE LINES 219-221 .. code-block:: Python [point.norm() for point in sample] .. rst-class:: sphx-glr-script-out .. code-block:: none [3.605551275463989, 1.0, 9.848857801796104] .. GENERATED FROM PYTHON SOURCE LINES 222-223 We can also create a `Sample` based on a `Point`, repeated three times. .. GENERATED FROM PYTHON SOURCE LINES 225-228 .. code-block:: Python sample = ot.Sample([p4] * 3) sample .. raw:: html
v0v1v2
0555
1555
2555


.. GENERATED FROM PYTHON SOURCE LINES 229-230 A nested list of floats is the easiest way to create a non-trivial `Sample`. .. GENERATED FROM PYTHON SOURCE LINES 232-235 .. code-block:: Python sample = ot.Sample([[0, 1], [2, 3], [4, 5]]) sample .. raw:: html
v0v1
001
123
245


.. GENERATED FROM PYTHON SOURCE LINES 236-241 Interactions with Numpy ----------------------- Classes defined in pure Python modules cannot be used by the library. This is why it is useful to know how to convert to and from more basic Python variable types, especially Numpy arrays. .. GENERATED FROM PYTHON SOURCE LINES 243-244 The following statement creates a :class:`~openturns.Sample` and converts it into a bidimensional Numpy `array`. .. GENERATED FROM PYTHON SOURCE LINES 246-250 .. code-block:: Python sample = ot.Sample(5, 3) array = np.array(sample) array .. rst-class:: sphx-glr-script-out .. code-block:: none array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) .. GENERATED FROM PYTHON SOURCE LINES 251-253 .. code-block:: Python print(type(array)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 254-255 Conversely, the following script creates a Numpy `array`, then converts it into a :class:`~openturns.Sample`. .. GENERATED FROM PYTHON SOURCE LINES 257-261 .. code-block:: Python array = 3.14 * np.ones((5, 3)) sample = ot.Sample(array) sample .. raw:: html
v0v1v2
03.143.143.14
13.143.143.14
23.143.143.14
33.143.143.14
43.143.143.14


.. GENERATED FROM PYTHON SOURCE LINES 262-264 .. code-block:: Python sample.getSize() .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 265-267 .. code-block:: Python sample.getDimension() .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 268-278 There is an ambiguous situation: a `Sample` based on several scalar values. For example, is a `Sample` based on 5 values: * a `Sample` with size 5 in 1 dimension or * a `Sample` with size 1 in 5 dimensions? In order to solve the case, we can use the second input argument of the `Sample` constructor, which specifies the dimension. The following statement creates an array containing 5 values from 0 to 1. .. GENERATED FROM PYTHON SOURCE LINES 280-283 .. code-block:: Python u = np.linspace(0, 1, 5) u .. rst-class:: sphx-glr-script-out .. code-block:: none array([0. , 0.25, 0.5 , 0.75, 1. ]) .. GENERATED FROM PYTHON SOURCE LINES 284-285 Choice A: we create a `Sample` with size 5 in 1 dimension. .. GENERATED FROM PYTHON SOURCE LINES 287-290 .. code-block:: Python sample = ot.Sample([[ui] for ui in u]) sample .. raw:: html
v0
00
10.25
20.5
30.75
41


.. GENERATED FROM PYTHON SOURCE LINES 291-292 Choice B: we create a `Sample` with size 1 in 5 dimensions. .. GENERATED FROM PYTHON SOURCE LINES 294-297 .. code-block:: Python sample = ot.Sample([u[i : i + 5] for i in range(len(u) // 5)]) sample .. raw:: html
v0v1v2v3v4
000.250.50.751


.. GENERATED FROM PYTHON SOURCE LINES 298-300 When there is an ambiguous case, the library cannot solve the issue and an `InvalidArgumentException` is generated. .. GENERATED FROM PYTHON SOURCE LINES 302-308 More precisely, the code: .. code-block:: sample = ot.Sample(u) .. GENERATED FROM PYTHON SOURCE LINES 310-316 produces the exception: .. code-block:: TypeError: InvalidArgumentException : Invalid array dimension: 1 .. GENERATED FROM PYTHON SOURCE LINES 318-320 In order to solve that problem, we can use the :meth:`~openturns.Sample.BuildFromPoint` static method. .. GENERATED FROM PYTHON SOURCE LINES 320-322 .. code-block:: Python sample = ot.Sample.BuildFromPoint([ui for ui in u]) sample .. raw:: html
v0
00
10.25
20.5
30.75
41


.. _sphx_glr_download_auto_data_analysis_manage_data_and_samples_plot_quick_start_point_and_sample.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_quick_start_point_and_sample.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_quick_start_point_and_sample.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_quick_start_point_and_sample.zip `