File: histogram.rst

package info (click to toggle)
xtensor 0.25.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,476 kB
  • sloc: cpp: 65,302; makefile: 202; python: 171; javascript: 8
file content (80 lines) | stat: -rw-r--r-- 2,705 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
.. Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht

   Distributed under the terms of the BSD 3-Clause License.

   The full license is in the file LICENSE, distributed with this software.

.. _histogram:

Histogram
=========

Basic usage
-----------

* :cpp:func:`xt::histogram(a, bins[, weights][, density]) <xt::histogram>`
* :cpp:func:`xt::histogram_bin_edges(a[, weights][, left, right][, bins][, mode]) <xt::histogram_bin_edges>`

.. note::
    Any of the options ``[...]`` can be omitted (though the order must be preserved). The defaults are:

    *   ``weights`` = :cpp:func:`xt::ones(data.shape()) <xt::ones>`
    *   ``density`` = ``false``
    *   ``left`` = :cpp:func:`xt::amin(data)(0) <xt::amin>`
    *   ``right`` = :cpp:func:`Xt::amax(data)(0) <xt::amax>`
    *   ``bins`` = ``10``
    *   ``mode`` = :cpp:enumerator:`xt::histogram::automatic`

The behavior, in-, and output of :cpp:func:`xt::histogram` is similar to that of :any:`numpy.histogram`
with that difference that the bin-edges are obtained by a separate function call:

.. code-block:: cpp

    #include <xtensor/xtensor.hpp>
    #include <xtensor/xhistogram.hpp>
    #include <xtensor/xio.hpp>

    int main()
    {
        xt::xtensor<double,1> data = {1., 1., 2., 2., 3.};

        xt::xtensor<double,1> count = xt::histogram(data, std::size_t(2));

        xt::xtensor<double,1> bin_edges = xt::histogram_bin_edges(data, std::size_t(2));

        return 0;
    }

Bin-edges algorithm
-------------------

To customize the algorithm to be used to construct the histogram, one needs to make use of the latter
:cpp:func:`xt::histogram_bin_edges`. For example:

.. code-block:: cpp

    #include <xtensor/xtensor.hpp>
    #include <xtensor/xhistogram.hpp>
    #include <xtensor/xio.hpp>

    int main()
    {
        xt::xtensor<double,1> data = {1., 1., 2., 2., 3.};

        xt::xtensor<double,1> bin_edges = xt::histogram_bin_edges(data, std::size_t(2), xt::histogram_algorithm::uniform);

        xt::xtensor<double,1> prob = xt::histogram(data, bin_edges, true);

        std::cout << bin_edges << std::endl;
        std::cout << prob << std::endl;

        return 0;
    }

The following :cpp:enum:`xt::histogram_algorithm` are available:

* :cpp:enumerator:`~xt::histogram_algorithm::automatic`: equivalent to :cpp:enumerator:`~xt::histogram_algorithm::linspace`.
* :cpp:enumerator:`~xt::histogram_algorithm::linspace`: linearly spaced bin-edges.
* :cpp:enumerator:`~xt::histogram_algorithm::logspace`: bins that logarithmically increase in size.
* :cpp:enumerator:`~xt::histogram_algorithm::uniform`: bin-edges such that the number of data points is
  the same in all bins (as much as possible).