.. 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_sort_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_sort_sample.py: Sort a sample ============= .. GENERATED FROM PYTHON SOURCE LINES 6-7 In this example we present useful methods of the `Sample` object such as marginals extraction and various sorting strategies. .. GENERATED FROM PYTHON SOURCE LINES 10-16 .. code-block:: Python import openturns as ot ot.Log.Show(ot.Log.NONE) ot.RandomGenerator.SetSeed(0) .. GENERATED FROM PYTHON SOURCE LINES 17-18 We start by defining the distribution of a regular non-biased die. .. GENERATED FROM PYTHON SOURCE LINES 18-20 .. code-block:: Python die_distribution = ot.UserDefined([[i] for i in range(1, 7)]) .. GENERATED FROM PYTHON SOURCE LINES 21-22 We consider now an experiment with two independent dice and build the corresponding random vector : .. GENERATED FROM PYTHON SOURCE LINES 22-24 .. code-block:: Python two_dice_distribution = ot.ComposedDistribution([die_distribution] * 2) .. GENERATED FROM PYTHON SOURCE LINES 25-26 We now build a sample of size :math:`n=5` from this distribution : .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: Python n = 5 sample = two_dice_distribution.getSample(n) print(sample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ v0 X0 ] 0 : [ 1 3 ] 1 : [ 3 2 ] 2 : [ 4 3 ] 3 : [ 4 4 ] 4 : [ 5 5 ] .. GENERATED FROM PYTHON SOURCE LINES 31-33 Useful methods -------------- .. GENERATED FROM PYTHON SOURCE LINES 35-36 We have access to the marginals by providing a list of the wanted indices : .. GENERATED FROM PYTHON SOURCE LINES 36-43 .. code-block:: Python # the first marginal sample_die1 = sample.getMarginal([0]) # the second marginal sample_die2 = sample.getMarginal([1]) .. GENERATED FROM PYTHON SOURCE LINES 44-46 Suppose we are interested in the sum of the two dice. This can be done by summing the two samples `die1` and `die2`. Provided the dimensions are the same we can add samples with the `+` operator and produce a new sample : .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: Python experiment = sample_die1 + sample_die2 .. GENERATED FROM PYTHON SOURCE LINES 49-50 Note that the `+=` operator is defined as well. .. GENERATED FROM PYTHON SOURCE LINES 52-53 We can concatenate two samples having the same size with the `stack` method : .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: Python sample_die1.stack(sample_die2) print(sample_die1) .. rst-class:: sphx-glr-script-out .. code-block:: none [ v0 X0 ] 0 : [ 1 3 ] 1 : [ 3 2 ] 2 : [ 4 3 ] 3 : [ 4 4 ] 4 : [ 5 5 ] .. GENERATED FROM PYTHON SOURCE LINES 57-58 We can split a sample in two by giving an index (here 2). .. GENERATED FROM PYTHON SOURCE LINES 58-63 .. code-block:: Python remaining = sample_die1.split(2) print(sample_die1) print(remaining) .. rst-class:: sphx-glr-script-out .. code-block:: none [ v0 X0 ] 0 : [ 1 3 ] 1 : [ 3 2 ] [ v0 X0 ] 0 : [ 4 3 ] 1 : [ 4 4 ] 2 : [ 5 5 ] .. GENERATED FROM PYTHON SOURCE LINES 64-66 Sorting samples --------------- .. GENERATED FROM PYTHON SOURCE LINES 69-70 We can extract any marginal and sort it by ascending order by specifying the index : .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python sorted_marginal = sample.sort(1) print(sorted_marginal) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 : [ 2 ] 1 : [ 3 ] 2 : [ 3 ] 3 : [ 4 ] 4 : [ 5 ] .. GENERATED FROM PYTHON SOURCE LINES 74-76 We can sort the sample in place, that is without creating a new sample, as well with sortInPlace. When the dimension is greater than one the sort is made according to the first marginal. .. GENERATED FROM PYTHON SOURCE LINES 76-79 .. code-block:: Python sample.sortInPlace() print(sample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ v0 X0 ] 0 : [ 1 3 ] 1 : [ 3 2 ] 2 : [ 4 3 ] 3 : [ 4 4 ] 4 : [ 5 5 ] .. GENERATED FROM PYTHON SOURCE LINES 80-81 We can sort the rows according to the second marginal with the `sortAccordingToAComponent` : .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: Python another_sample = sample.sortAccordingToAComponent(1) print(another_sample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ v0 X0 ] 0 : [ 3 2 ] 1 : [ 1 3 ] 2 : [ 4 3 ] 3 : [ 4 4 ] 4 : [ 5 5 ] .. GENERATED FROM PYTHON SOURCE LINES 85-86 There is also a `sortAccordingToAComponentInPlace` method that does the same without creating a new sample. .. GENERATED FROM PYTHON SOURCE LINES 88-89 We can sort and remove the duplicates at the same time .. GENERATED FROM PYTHON SOURCE LINES 89-92 .. code-block:: Python print(sample_die2) print(sample_die2.sortUnique()) .. rst-class:: sphx-glr-script-out .. code-block:: none [ X0 ] 0 : [ 3 ] 1 : [ 2 ] 2 : [ 3 ] 3 : [ 4 ] 4 : [ 5 ] [ X0 ] 0 : [ 2 ] 1 : [ 3 ] 2 : [ 4 ] 3 : [ 5 ] .. GENERATED FROM PYTHON SOURCE LINES 93-94 We note that the sample is smaller as expected. Sorting in place is also possible : .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python sample_die2.sortUniqueInPlace() print(sample_die2) .. rst-class:: sphx-glr-script-out .. code-block:: none [ X0 ] 0 : [ 2 ] 1 : [ 3 ] 2 : [ 4 ] 3 : [ 5 ] .. GENERATED FROM PYTHON SOURCE LINES 98-99 Let's try with the sample in dimension 2 : .. GENERATED FROM PYTHON SOURCE LINES 99-102 .. code-block:: Python sampleUnique = sample.sortUnique() print(sampleUnique) .. rst-class:: sphx-glr-script-out .. code-block:: none [ v0 X0 ] 0 : [ 1 3 ] 1 : [ 3 2 ] 2 : [ 4 3 ] 3 : [ 4 4 ] 4 : [ 5 5 ] .. GENERATED FROM PYTHON SOURCE LINES 103-104 Nothing happens here because pairs are already unique ! .. _sphx_glr_download_auto_data_analysis_manage_data_and_samples_plot_sort_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_sort_sample.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sort_sample.py `