[关闭]
@BruceWang 2018-01-03T19:09:56.000000Z 字数 2891 阅读 1692

Numpy pandas matplot 笔记 for 随机漫步

NumpyPandasMatplot

这个例子让我了解一个在实际任务中如何利用数组操作,首先是一个最简单的随机漫步,从零开始,纯python方法 1000

  1. import random
  2. position = 0
  3. walk = [position]
  4. steps = 1000
  5. for i in range(steps):
  6. step = 1 if random.randint(0,1) else -1
  7. position += step
  8. walk.append([position])
  1. import matplotlib.pyplot as plt
  2. %matplotlib inline
  1. walk[:10]
[0, [-1], [-2], [-1], [-2], [-1], [-2], [-1], [-2], [-1]]

随机漫步其实就是一个简单的累加。而用np.random能更快:

  1. import numpy as np
  1. nsteps = 1000
  2. draws = np.random.randint(0,2,size=nsteps)
  3. steps = np.where(draws>0, 1, -1)
  4. print(steps)
[-1 -1 -1  1 -1  1 -1  1  1  1 -1  1 -1  1  1 -1 -1  1 -1 -1 -1  1 -1  1 -1
 -1 -1  1  1  1  1  1 -1  1 -1 -1  1 -1 -1 -1  1 -1  1  1 -1  1 -1 -1 -1  1
  1 -1  1  1  1  1 -1  1 -1  1  1  1 -1 -1  1  1 -1  1  1  1  1 -1  1  1 -1
  1 -1 -1 -1 -1  1  1 -1  1 -1 -1  1  1 -1 -1  1 -1 -1  1 -1  1  1 -1 -1  1
  1 -1  1 -1  1  1 -1  1  1  1 -1 -1  1  1 -1 -1  1  1  1  1 -1  1 -1 -1 -1
  1  1  1 -1  1 -1 -1 -1  1 -1  1  1  1 -1 -1  1  1 -1  1 -1 -1  1  1  1  1
  1  1 -1 -1  1  1  1  1  1 -1 -1  1  1 -1 -1  1 -1 -1  1  1 -1  1  1 -1  1
  1  1 -1 -1 -1  1 -1 -1 -1  1  1 -1  1 -1  1 -1 -1 -1 -1  1  1  1  1  1 -1
 -1 -1  1  1  1  1 -1  1  1  1 -1  1  1  1 -1 -1  1  1  1 -1  1 -1  1  1  1
 -1  1 -1 -1  1  1  1  1  1 -1  1  1  1 -1  1  1  1 -1  1 -1 -1  1  1  1 -1
 -1 -1  1  1  1 -1 -1  1  1  1 -1 -1  1 -1 -1  1 -1  1  1 -1 -1 -1 -1 -1  1
  1  1 -1  1 -1  1  1  1  1  1  1 -1 -1  1  1  1 -1 -1  1 -1 -1  1  1  1 -1
 -1  1  1  1  1  1  1 -1 -1 -1  1 -1 -1 -1 -1  1 -1 -1  1 -1  1 -1  1  1  1
 -1  1 -1  1  1  1  1 -1 -1  1  1 -1 -1  1 -1 -1  1  1  1  1 -1  1 -1 -1  1
  1  1  1 -1 -1  1 -1  1 -1 -1 -1 -1  1 -1  1  1 -1 -1  1  1 -1 -1 -1 -1 -1
  1  1  1 -1 -1 -1 -1 -1 -1 -1  1  1  1  1 -1 -1  1  1 -1  1  1  1 -1 -1  1
  1  1  1 -1 -1 -1 -1 -1 -1 -1 -1  1  1  1  1  1  1  1  1 -1  1 -1 -1  1  1
 -1 -1 -1 -1 -1  1 -1 -1 -1  1  1  1  1  1 -1  1 -1  1 -1  1 -1 -1  1  1  1
 -1  1 -1 -1 -1  1 -1 -1  1  1 -1 -1 -1  1 -1  1 -1  1  1 -1 -1  1 -1  1  1
 -1  1  1 -1  1 -1  1  1  1  1  1  1  1  1 -1 -1 -1  1 -1  1 -1 -1  1 -1 -1
  1  1  1 -1 -1  1  1 -1 -1 -1 -1  1  1 -1  1 -1 -1 -1  1 -1 -1  1 -1  1  1
 -1  1 -1 -1  1  1  1 -1 -1 -1 -1 -1  1  1 -1 -1  1  1  1 -1  1 -1  1 -1  1
  1 -1 -1 -1 -1 -1 -1  1  1 -1  1 -1 -1  1  1  1 -1 -1 -1 -1 -1 -1  1  1 -1
 -1 -1 -1 -1  1 -1 -1 -1  1 -1  1  1  1  1 -1 -1 -1  1  1  1  1 -1  1 -1  1
 -1  1 -1 -1 -1 -1  1 -1  1  1 -1 -1  1 -1 -1 -1  1 -1 -1  1  1 -1  1 -1 -1
  1 -1 -1  1  1  1 -1 -1  1  1  1 -1  1  1  1  1  1 -1  1 -1 -1  1 -1 -1 -1
  1 -1 -1  1 -1  1  1 -1 -1  1  1  1  1 -1 -1  1  1 -1 -1 -1  1 -1  1  1  1
  1  1  1 -1  1  1 -1  1  1 -1 -1 -1 -1 -1 -1  1 -1 -1 -1  1  1  1  1  1  1
  1 -1 -1  1 -1  1  1  1 -1 -1 -1  1 -1 -1 -1  1  1 -1 -1  1 -1  1 -1 -1  1
  1 -1 -1 -1  1 -1 -1 -1  1 -1 -1  1 -1 -1 -1  1  1 -1 -1  1 -1 -1 -1 -1  1
  1 -1  1  1 -1  1 -1  1 -1  1  1  1 -1  1  1  1 -1  1  1 -1 -1  1 -1 -1  1
  1  1 -1  1  1  1  1 -1  1 -1  1 -1  1 -1 -1 -1  1 -1  1  1  1 -1 -1  1  1
  1  1 -1  1  1 -1  1  1 -1 -1 -1  1  1  1 -1  1  1 -1 -1  1  1 -1  1  1  1
 -1  1 -1  1  1  1 -1  1 -1 -1  1 -1 -1 -1  1 -1 -1  1  1 -1 -1 -1 -1  1 -1
  1  1 -1 -1 -1 -1  1 -1  1 -1 -1 -1  1  1 -1  1 -1 -1  1  1  1 -1  1  1 -1
  1  1 -1  1 -1 -1 -1 -1  1 -1  1  1 -1 -1  1 -1 -1 -1  1  1  1  1 -1  1  1
  1 -1  1 -1 -1 -1 -1 -1  1  1  1 -1 -1  1  1 -1  1 -1  1 -1 -1  1 -1  1  1
  1  1  1  1  1 -1 -1 -1 -1 -1  1  1  1 -1 -1  1  1  1 -1  1  1 -1 -1 -1  1
 -1 -1  1 -1 -1 -1  1  1  1 -1  1 -1  1  1 -1  1 -1 -1 -1 -1 -1  1  1 -1  1
  1  1 -1  1 -1  1 -1 -1  1  1 -1  1 -1  1  1  1  1  1  1  1 -1 -1 -1 -1 -1]
  1. walk = steps.cumsum()
  1. walk.min()
-5
  1. walk.max()
41

一次模拟多个随机漫步

  1. nwalks = 5000
  2. nsteps = 1000
  3. draws = np.random.randint(0,2, size=(nwalks, nsteps))
  4. steps = np.where(draws>0, 1,-1)
  5. walks = steps.cumsum(1)
  1. walks
array([[  1,   0,  -1, ...,  -6,  -7,  -8],
       [  1,   0,   1, ...,   6,   7,   6],
       [ -1,  -2,  -3, ...,   2,   3,   2],
       ..., 
       [  1,   0,   1, ...,  54,  53,  54],
       [  1,   0,   1, ..., -28, -29, -30],
       [  1,   2,   3, ..., -32, -31, -30]], dtype=int32)
  1. walks.max()
116
  1. walks.min()
-124

上面的step只是像翻硬币一样二选一,我们也可以用一个概率函数来生成:

  1. steps = np.random.normal(loc=0, scale=0.25, size=(nwalks, nsteps))
  1. steps
array([[-0.16399234, -0.0267885 ,  0.05142689, ..., -0.37457757,
         0.03966075,  0.39067126],
       [ 0.12018613, -0.08792517, -0.05285084, ...,  0.26438388,
        -0.21433596,  0.33423605],
       [-0.06996542, -0.17732628, -0.41869773, ..., -0.00390321,
        -0.12775795,  0.16524036],
       ..., 
       [-0.18181599, -0.37014665, -0.01165527, ..., -0.11092812,
         0.05949846,  0.09946047],
       [-0.36011367, -0.12865467, -0.31651241, ..., -0.02512507,
        -0.00548912,  0.30151192],
       [ 0.32561003,  0.28333045, -0.28733556, ...,  0.14309592,
        -0.31387699,  0.16887155]])
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注