Help us understand the problem. What is going on with this article?

[python]randとrandnの出力の違い

More than 1 year has passed since last update.

参考:ゼロから作るDeep Learning(p98 4.3.1)

randはシンプルに0~+1の範囲内でランダムな数値を出力(一様分布と言われる)する。
randnは標準正規分布(ガウス分布)でランダムな数値を出力する。
標準正規分布(ガウス分布)は、平均0, 標準偏差1の正規分布である。
つまり、平均が0になる範囲のランダムな数値を出力する。0に近い数値が出力されやすく、値が-1~+1の間に偏っている。

出力を見てみる

6個数値を生成するぐらいだと差があまりわからない。
(randnだと-1.8とかも出力される。)

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

しかし、1000個数値を生成すると差が顕著に出てくる。randnのほうは0に収束している。
randの平均が0.5に収束しているのは0~+1の間を平均的に生成するようにしているからだろう。平均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
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away