[python] Difference between rand and randn output

Reference: [Deep Learning from scratch (p98 4.3.1)](https://www.amazon.co.jp/%E3%82%BC%E3%83%AD%E3%81%8B%E3%82 % 89% E4% BD% 9C% E3% 82% 8BDeep-Learning-% E2% 80% 95Python% E3% 81% A7% E5% AD% A6% E3% 81% B6% E3% 83% 87% E3% 82% A3% E3% 83% BC% E3% 83% 97% E3% 83% A9% E3% 83% BC% E3% 83% 8B% E3% 83% B3% E3% 82% B0% E3% 81% AE% E7% 90% 86% E8% AB% 96% E3% 81% A8% E5% AE% 9F% E8% A3% 85-% E6% 96% 8E% E8% 97% A4-% E5% BA% B7% E6% AF% 85 / dp / 4873117585)

rand simply outputs a random number within the range of 0 to + 1 (called a uniform distribution). randn outputs a random numerical value with a standard normal distribution (Gaussian distribution). The standard normal distribution (Gaussian distribution) is a normal distribution with a mean of 0 and a standard deviation of 1. That is, it outputs a random numerical value in the range where the average becomes 0. A value close to 0 is likely to be output, and the value is biased between -1 and +1.

Take a look at the output

6 You can't really tell the difference if you generate a number value. (If it is randn, -1.8 is also output.)

tmp.py


import numpy as np

print("-----rand-----")
#print(np.random.rand(2,3))
print(np.random.rand(2,3).flatten().sum()/6)
print("-----randn-----")
#print(np.random.randn(2,3))
print(np.random.randn(2,3).flatten().sum()/6)
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
[[ 0.93180533  0.57683396  0.61127319]
 [ 0.85445375  0.02708778  0.59547601]]
0.548895695538
-----randn-----
[[-0.26519086 -0.30503921  1.38052841]
 [ 0.78893855  1.17725959  1.80882169]]
0.31519809212

mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
[[ 0.60780629  0.20632273  0.29124602]
 [ 0.30784594  0.70772679  0.01821361]]
0.59935565743
-----randn-----
[[ 0.71569157 -0.52234049 -1.59936154]
 [-0.82040641 -1.40208476  0.89450808]]
0.669739423807

mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
[[ 0.490528    0.77907488  0.7239194 ]
 [ 0.85538536  0.92002825  0.67559426]]
0.495670703747
-----randn-----
[[-0.15695285 -0.34244922 -1.44827391]
 [-1.36062357 -0.49387177 -1.81863075]]
-0.431866047467

mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
[[ 0.84705902  0.58580408  0.31309435]
 [ 0.59569691  0.24460075  0.45989438]]
0.392091814221
-----randn-----
[[-0.35680625 -2.18199977  0.54243995]
 [-0.29317688 -0.43111914 -1.47586303]]
-0.0274650256491

However, when 1000 number values are generated, the difference becomes noticeable. randn converges to 0. The reason why the average of rand converges to 0.5 is that it is generated on average between 0 and +1. It does not tend to have a normal distribution with an average of 0.5.

tmp.py


import numpy as np

print("-----rand-----")
print(np.random.rand(1000).flatten().sum()/1000)
print("-----randn-----")
print(np.random.randn(1000).flatten().sum()/1000)
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.496111761085
-----randn-----
0.0156004004458
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.499698237009
-----randn-----
-0.00664557647709
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.494677814946
-----randn-----
0.0301994725782
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.515244265326
-----randn-----
-0.00605375728391
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.500510191675
-----randn-----
0.00764750739818

Recommended Posts

[python] Difference between rand and randn output
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between == and is in python
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
difference between statements (statements) and expressions (expressions) in Python
Difference between PHP and Python finally and exit
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[Python] Difference between class method and static method
[Python Iroha] Difference between List and Tuple
python input and output
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Difference between process and job
Difference between Ruby and Python in terms of variables
Difference between regression and classification
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference between ps a and ps -a
Difference between return and print-Python
Python module num2words Difference in behavior between English and Russian
List concatenation method in python, difference between list.extend () and β€œ+” operator
Memorandum (difference between csv.reader and csv.dictreader)
(Note) Difference between gateway and default gateway
Cooperation between python module and API
[Code] Module and Python version output
Difference between Numpy randint and Random randint
Differences between Python, stftime and strptime
Difference between sort and sorted (memorial)
Difference between SQLAlchemy flush () and commit ()
Difference in how to write if statement between ruby ​​and python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
Python / Numpy> Link> Difference between numpy.random and random.random> thread-safe or not
File open function in Python3 (difference between open and codecs.open and speed comparison)
Differences in authenticity between Python and JavaScript
[Xg boost] Difference between softmax and softprob
[Django ORM] Difference between values () and only ()
Differences in syntax between Python and Java
Difference between linear regression, Ridge regression and Lasso regression
speed difference between wsgi, Bottle and Flask
Differences in multithreading between Python and Jython
Differences between Ruby and Python (basic syntax)
Difference between numpy.ndarray and list (dimension, size)
Correspondence between Python built-in functions and Rust
Difference between ls -l and cat command
Exchange encrypted data between Python and C #
Difference and compatibility verification between keras and tf.keras # 1
[Introduction to Python] What is the difference between a list and a tuple?
What is the difference between `pip` and` conda`?
Summary of the differences between PHP and Python
Read JSON with Python and output as CSV
The answer of "1/2" is different between python2 and 3
Difference between using and import on shield language
[Python] Conversion memo between time data and numerical data