[关闭]
@BruceWang 2018-01-03T18:27:56.000000Z 字数 1045 阅读 1395

Numpy pandas matplot 笔记for 随机数组

NumpyPandasMatplot

numpy.random 提供了生成随机数的函数,我们可以选择用normal得到一个4x4的,符合标准正态分布的数组

  1. import numpy as np
  2. samples = np.random.normal(size=(4,4))
  3. 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模块一次只能生成一个样本

  1. from random import normalvariate
  2. N = 1000000
  1. %timeit sample = [normalvariate(0,1) for _ in range(N)]
1 loop, best of 3: 798 ms per loop
  1. %timeit np.random.normal(size=N)
10 loops, best of 3: 30.8 ms per loop

之所以称之为伪随机数,是因为随机数生成算法是根据seed来生成的。也就是说,只要seed设置一样,每次生成的随机数是相同的:

  1. np.random.seed(1234)
  2. np.random.randn(3,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]])
  1. rng = np.random.RandomState(1234)
  1. rng.randn(10)
array([ 0.47143516, -1.19097569,  1.43270697, -0.3126519 , -0.72058873,
        0.88716294,  0.85958841, -0.6365235 ,  0.01569637, -2.24268495])
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注