@BruceWang
2018-01-03T18:27:56.000000Z
字数 1045
阅读 1395
NumpyPandasMatplot
import numpy as np
samples = np.random.normal(size=(4,4))
samples
array([[-0.00825586, -1.40577361, -0.08983249, 1.56990696],
[-1.00962914, -0.85160605, 0.98410487, -1.4069253 ],
[ 0.9055031 , 0.025083 , 0.58990583, 1.97081072],
[ 0.24138361, -0.1793512 , -0.76812824, -0.32014383]])
相对的,python内建的random模块一次只能生成一个样本
from random import normalvariate
N = 1000000
%timeit sample = [normalvariate(0,1) for _ in range(N)]
1 loop, best of 3: 798 ms per loop
%timeit np.random.normal(size=N)
10 loops, best of 3: 30.8 ms per loop
之所以称之为伪随机数,是因为随机数生成算法是根据seed来生成的。也就是说,只要seed设置一样,每次生成的随机数是相同的:
np.random.seed(1234)
np.random.randn(3,3)
np.random.randn(3,4)
array([[ -2.24268495e+00, 1.15003572e+00, 9.91946022e-01,
9.53324128e-01],
[ -2.02125482e+00, -3.34077366e-01, 2.11836468e-03,
4.05453412e-01],
[ 2.89091941e-01, 1.32115819e+00, -1.54690555e+00,
-2.02646325e-01]])
rng = np.random.RandomState(1234)
rng.randn(10)
array([ 0.47143516, -1.19097569, 1.43270697, -0.3126519 , -0.72058873,
0.88716294, 0.85958841, -0.6365235 , 0.01569637, -2.24268495])