In [2]:
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
arr = np.arange(10)
In [4]:
arr**2
Out[4]:
In [5]:
arr[5:8]
Out[5]:
In [6]:
arr[5:8] = 12
In [7]:
arr
Out[7]:
In [8]:
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [10]:
arr2d[2,0]
Out[10]:
In [19]:
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
In [21]:
data = np.random.randn(7, 4)
print data
data[names == 'Bob'] = 0
print data
In [16]:
Out[16]:
In [17]:
names == 'Bob'
Out[17]:
In [4]:
points = np.arange(-5, 5, 0.01)
xs, ys = np.meshgrid(points, points)
In [5]:
ys
Out[5]:
In [6]:
xs
Out[6]:
In [7]:
z = np.sqrt(xs ** 2 + ys ** 2)
In [8]:
plt.imshow(z, cmap=plt.cm.gray); plt.colorbar()
Out[8]:
In [9]:
nsteps = 1000
draws = np.random.randint(0, 2, size=nsteps)
In [14]:
print draws
In [13]:
np.random.randint(0, 20)
Out[13]:
In [15]:
steps = np.where(draws > 0, 1, -1)
print steps
In [16]:
walk = steps.cumsum()
print walk
In [17]:
plt.plot(walk)
Out[17]:
In [36]:
#随机游走模型
nwalks = 5000
nsteps = 1000
draws = np.random.randint(0, 2, size=(nwalks, nsteps))
steps = np.where(draws > 0, 1, -1)
walks = steps.cumsum(1)
hits30 = (np.abs(walks) >= 30).any(1)
crossing_times = (np.abs(walks[hits30]) >= 30).argmax(1)
crossing_times.mean()
Out[36]:
In [ ]: