|
| 1 | + |
| 2 | +numpy.random |
| 3 | +============ |
| 4 | + |
| 5 | +Random numbers drawn specific distributions can be generated by |
| 6 | +instantiating a ``Generator`` object, and calling its methods. The |
| 7 | +module defines the following three functions: |
| 8 | + |
| 9 | +1. `numpy.random.Generator.normal <#normal>`__ |
| 10 | +2. `numpy.random.Generator.random <#random>`__ |
| 11 | +3. `numpy.random.Generator.uniform <#uniform>`__ |
| 12 | + |
| 13 | +The ``Generator`` object, when instantiated, takes a single integer as |
| 14 | +its argument. This integer is the seed, which will be fed to the 32-bit |
| 15 | +or 64-bit routine. More details can be found under |
| 16 | +https://www.pcg-random.org/index.html. The generator is a standard |
| 17 | +``python`` object that keeps track of its state. |
| 18 | + |
| 19 | +``numpy``: https://numpy.org/doc/stable/reference/random/index.html |
| 20 | + |
| 21 | +normal |
| 22 | +------ |
| 23 | + |
| 24 | +A random set of number from the ``normal`` distribution can be generated |
| 25 | +by calling the generator’s ``normal`` method. The method takes three |
| 26 | +optional arguments, ``loc=0.0``, the centre of the distribution, |
| 27 | +``scale=1.0``, the width of the distribution, and ``size=None``, a tuple |
| 28 | +containing the shape of the returned array. In case ``size`` is |
| 29 | +``None``, a single floating point number is returned. |
| 30 | + |
| 31 | +The ``normal`` method of the ``Generator`` object is based on the |
| 32 | +`Box-Muller |
| 33 | +transform <https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform>`__. |
| 34 | + |
| 35 | +``numpy``: |
| 36 | +https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.normal.html |
| 37 | + |
| 38 | +.. code:: |
| 39 | + |
| 40 | + # code to be run in micropython |
| 41 | + |
| 42 | + from ulab import numpy as np |
| 43 | + |
| 44 | + rng = np.random.Generator(123456) |
| 45 | + print(rng) |
| 46 | + |
| 47 | + # return single number from a distribution of scale 1, and location 0 |
| 48 | + print(rng.normal()) |
| 49 | + |
| 50 | + print(rng.normal(loc=20.0, scale=10.0, size=(3,3))) |
| 51 | + # same as above, with positional arguments |
| 52 | + print(rng.normal(20.0, 10.0, (3,3))) |
| 53 | +
|
| 54 | +.. parsed-literal:: |
| 55 | +
|
| 56 | + Gnerator() at 0x7fa9dae05340 |
| 57 | + -6.285246229407202 |
| 58 | + array([[24.95816273705659, 15.2670302229426, 14.81001577336041], |
| 59 | + [20.17589833056986, 23.14539083787544, 26.37772041367461], |
| 60 | + [41.94894234387275, 37.11027030608206, 25.65889562100477]], dtype=float64) |
| 61 | + array([[21.52562779033434, 12.74685887865834, 24.08404670765186], |
| 62 | + [4.728112596365396, 7.667757906857082, 21.61576094228444], |
| 63 | + [2.432338873595267, 27.75945683572574, 5.730827584659245]], dtype=float64) |
| 64 | + |
| 65 | + |
| 66 | +
|
| 67 | +
|
| 68 | +random |
| 69 | +------ |
| 70 | + |
| 71 | +A random set of number from the uniform distribution in the interval [0, |
| 72 | +1] can be generated by calling the generator’s ``random`` method. The |
| 73 | +method takes two optional arguments, ``size=None``, a tuple containing |
| 74 | +the shape of the returned array, and ``out``. In case ``size`` is |
| 75 | +``None``, a single floating point number is returned. |
| 76 | + |
| 77 | +``out`` can be used, if a floating point array is available. An |
| 78 | +exception will be raised, if the array is not of ``float`` ``dtype``, or |
| 79 | +if both ``size`` and ``out`` are supplied, and there is a conflict in |
| 80 | +their shapes. |
| 81 | + |
| 82 | +If ``size`` is ``None``, a single floating point number will be |
| 83 | +returned. |
| 84 | + |
| 85 | +``numpy``: |
| 86 | +https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.random.html |
| 87 | + |
| 88 | +.. code:: |
| 89 | + |
| 90 | + # code to be run in micropython |
| 91 | + |
| 92 | + from ulab import numpy as np |
| 93 | + |
| 94 | + rng = np.random.Generator(123456) |
| 95 | + print(rng) |
| 96 | + |
| 97 | + # returning new objects |
| 98 | + print(rng.random()) |
| 99 | + print('\n', rng.random(size=(3,3))) |
| 100 | + |
| 101 | + # supplying a buffer |
| 102 | + a = np.array(range(9), dtype=np.float).reshape((3,3)) |
| 103 | + print('\nbuffer array before:\n', a) |
| 104 | + rng.random(out=a) |
| 105 | + print('\nbuffer array after:\n', a) |
| 106 | +
|
| 107 | +.. parsed-literal:: |
| 108 | +
|
| 109 | + Gnerator() at 0x7f299de05340 |
| 110 | + 6.384615058863119e-11 |
| 111 | + |
| 112 | + array([[0.4348157846574171, 0.7906325931024071, 0.878697619856133], |
| 113 | + [0.8738606263361598, 0.4946080034142021, 0.7765890156101152], |
| 114 | + [0.1770783715717074, 0.02080447648492112, 0.1053837559005948]], dtype=float64) |
| 115 | + |
| 116 | + buffer array before: |
| 117 | + array([[0.0, 1.0, 2.0], |
| 118 | + [3.0, 4.0, 5.0], |
| 119 | + [6.0, 7.0, 8.0]], dtype=float64) |
| 120 | + |
| 121 | + buffer array after: |
| 122 | + array([[0.8508024287393201, 0.9848489829156055, 0.7598167589604003], |
| 123 | + [0.782995698302952, 0.2866337782847831, 0.7915884498022229], |
| 124 | + [0.4614071706315902, 0.4792657443088592, 0.1581582066230718]], dtype=float64) |
| 125 | + |
| 126 | + |
| 127 | +
|
| 128 | +
|
| 129 | +uniform |
| 130 | +------- |
| 131 | + |
| 132 | +``uniform`` is similar to ``random``, except that the interval over |
| 133 | +which the numbers are distributed can be specified, while the ``out`` |
| 134 | +argument cannot. In addition to ``size`` specifying the shape of the |
| 135 | +output, ``low=0.0``, and ``high=1.0`` are accepted arguments. With the |
| 136 | +indicated defaults, ``uniform`` is identical to ``random``, which can be |
| 137 | +seen from the fact that the first 3-by-3 tensor below is the same as the |
| 138 | +one produced by ``rng.random(size=(3,3))`` above. |
| 139 | + |
| 140 | +If ``size`` is ``None``, a single floating point number will be |
| 141 | +returned. |
| 142 | + |
| 143 | +``numpy``: |
| 144 | +https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.uniform.html |
| 145 | + |
| 146 | +.. code:: |
| 147 | + |
| 148 | + # code to be run in micropython |
| 149 | + |
| 150 | + from ulab import numpy as np |
| 151 | + |
| 152 | + rng = np.random.Generator(123456) |
| 153 | + print(rng) |
| 154 | + |
| 155 | + print(rng.uniform()) |
| 156 | + # returning numbers between 0, and 1 |
| 157 | + print('\n', rng.uniform(size=(3,3))) |
| 158 | + |
| 159 | + # returning numbers between 10, and 20 |
| 160 | + print('\n', rng.uniform(low=10, high=20, size=(3,3))) |
| 161 | + |
| 162 | + # same as above, without the keywords |
| 163 | + print('\n', rng.uniform(10, 20, (3,3))) |
| 164 | +
|
| 165 | +.. parsed-literal:: |
| 166 | +
|
| 167 | + Gnerator() at 0x7f1891205340 |
| 168 | + 6.384615058863119e-11 |
| 169 | + |
| 170 | + array([[0.4348157846574171, 0.7906325931024071, 0.878697619856133], |
| 171 | + [0.8738606263361598, 0.4946080034142021, 0.7765890156101152], |
| 172 | + [0.1770783715717074, 0.02080447648492112, 0.1053837559005948]], dtype=float64) |
| 173 | + |
| 174 | + array([[18.5080242873932, 19.84848982915605, 17.598167589604], |
| 175 | + [17.82995698302952, 12.86633778284783, 17.91588449802223], |
| 176 | + [14.6140717063159, 14.79265744308859, 11.58158206623072]], dtype=float64) |
| 177 | + |
| 178 | + array([[14.3380400319162, 12.72487657409978, 15.77119643621117], |
| 179 | + [13.61835831436355, 18.96062889255558, 15.78847796795966], |
| 180 | + [12.59435855187034, 17.68262037443622, 14.77943040598734]], dtype=float64) |
| 181 | + |
| 182 | + |
| 183 | +
|
0 commit comments