Chapter_04



In [2]:
import numpy as np
data1 = [6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
arr1


Out[2]:
array([ 6. ,  7.5,  8. ,  0. ,  1. ])

In [3]:
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr2 = np.array(data2)
arr2


Out[3]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

In [4]:
print(arr2.ndim, arr2.shape)


2 (2, 4)

In [5]:
print(arr1.dtype, arr2.dtype)


float64 int32

In [6]:
np.zeros(10)


Out[6]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

In [7]:
np.zeros((3, 6))


Out[7]:
array([[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.]])

In [8]:
np.empty((2, 3, 2))


Out[8]:
array([[[  4.59481051e-322,   0.00000000e+000],
        [  0.00000000e+000,   0.00000000e+000],
        [  0.00000000e+000,   0.00000000e+000]],

       [[  0.00000000e+000,   0.00000000e+000],
        [  0.00000000e+000,   0.00000000e+000],
        [  0.00000000e+000,   0.00000000e+000]]])

In [10]:
np.arange(15)


Out[10]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

In [11]:
np.asarray(data2)


Out[11]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

In [12]:
np.asarray(arr2)


Out[12]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

In [13]:
np.ones_like(arr2)


Out[13]:
array([[1, 1, 1, 1],
       [1, 1, 1, 1]])

In [14]:
np.identity(3)


Out[14]:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

In [16]:
arr = np.array([1, 2, 3, 4, 5])
arr.dtype


Out[16]:
dtype('int32')

In [18]:
float_arr = arr.astype('float32')
float_arr.dtype


Out[18]:
dtype('float32')

In [19]:
float_arr[0] = 0
print(float_arr, '\n', arr)


[ 0.  2.  3.  4.  5.] 
 [1 2 3 4 5]

In [20]:
numeric_string = np.array(['1.25', '-9.6', '42'], dtype = np.string_)
numeric_string.astype(float)


Out[20]:
array([  1.25,  -9.6 ,  42.  ])

In [25]:
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr


Out[25]:
array([[1, 2, 3],
       [4, 5, 6]])

In [26]:
arr * arr


Out[26]:
array([[ 1,  4,  9],
       [16, 25, 36]])

In [27]:
arr - arr


Out[27]:
array([[0, 0, 0],
       [0, 0, 0]])

In [28]:
1 / arr


Out[28]:
array([[ 1.        ,  0.5       ,  0.33333333],
       [ 0.25      ,  0.2       ,  0.16666667]])

In [29]:
arr ** 0.5


Out[29]:
array([[ 1.        ,  1.41421356,  1.73205081],
       [ 2.        ,  2.23606798,  2.44948974]])

In [31]:
arr = np.arange(10)
arr


Out[31]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [32]:
arr[5]


Out[32]:
5

In [33]:
arr[5:8]


Out[33]:
array([5, 6, 7])

In [34]:
arr[5:8] = 12
arr


Out[34]:
array([ 0,  1,  2,  3,  4, 12, 12, 12,  8,  9])

In [35]:
arr_slice = arr[5:8]
arr_slice[1] = 12345
arr


Out[35]:
array([    0,     1,     2,     3,     4,    12, 12345,    12,     8,     9])

In [36]:
arr_slice[:] = 64
arr


Out[36]:
array([ 0,  1,  2,  3,  4, 64, 64, 64,  8,  9])

In [37]:
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr2d[2]


Out[37]:
array([7, 8, 9])

In [38]:
arr2d[0][2]


Out[38]:
3

In [39]:
arr2d[0, 2]


Out[39]:
3

In [40]:
arr2d


Out[40]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [41]:
arr2d[:2]


Out[41]:
array([[1, 2, 3],
       [4, 5, 6]])

In [42]:
arr2d[:2, 1:]


Out[42]:
array([[2, 3],
       [5, 6]])

In [43]:
arr2d[1, :2]


Out[43]:
array([4, 5])

In [45]:
arr2d[2, :1]


Out[45]:
array([7])

In [46]:
arr2d[:, :1]


Out[46]:
array([[1],
       [4],
       [7]])

In [47]:
arr2d[:2, 1:] = 0
arr2d


Out[47]:
array([[1, 0, 0],
       [4, 0, 0],
       [7, 8, 9]])

In [48]:
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(7, 4)

In [49]:
names


Out[49]:
array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], 
      dtype='<U4')

In [50]:
data


Out[50]:
array([[-1.02507637,  0.4954459 ,  0.48120621, -0.57415051],
       [-0.2525449 ,  0.60428499,  0.12993482,  0.09675807],
       [ 0.96470466, -0.072239  , -1.03371183,  0.3060512 ],
       [-2.46213906, -0.28323223,  1.65348566, -0.88040289],
       [ 0.74105795, -1.19107693, -0.30397068, -1.73768552],
       [-0.71400102, -0.11157372, -0.05273129, -1.31130646],
       [ 1.23977937, -0.12684053,  0.56747507,  0.38559187]])

In [51]:
names == 'Bob'


Out[51]:
array([ True, False, False,  True, False, False, False], dtype=bool)

In [52]:
data[names == 'Bob']


Out[52]:
array([[-1.02507637,  0.4954459 ,  0.48120621, -0.57415051],
       [-2.46213906, -0.28323223,  1.65348566, -0.88040289]])

In [53]:
data[names == 'Bob', 3]


Out[53]:
array([-0.57415051, -0.88040289])

In [54]:
names != 'Bob'


Out[54]:
array([False,  True,  True, False,  True,  True,  True], dtype=bool)

In [55]:
data[names != 'Bob']


Out[55]:
array([[-0.2525449 ,  0.60428499,  0.12993482,  0.09675807],
       [ 0.96470466, -0.072239  , -1.03371183,  0.3060512 ],
       [ 0.74105795, -1.19107693, -0.30397068, -1.73768552],
       [-0.71400102, -0.11157372, -0.05273129, -1.31130646],
       [ 1.23977937, -0.12684053,  0.56747507,  0.38559187]])

In [56]:
data[-(names == 'Bob')]


C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:1: DeprecationWarning: numpy boolean negative, the `-` operator, is deprecated, use the `~` operator or the logical_not function instead.
  if __name__ == '__main__':
Out[56]:
array([[-0.2525449 ,  0.60428499,  0.12993482,  0.09675807],
       [ 0.96470466, -0.072239  , -1.03371183,  0.3060512 ],
       [ 0.74105795, -1.19107693, -0.30397068, -1.73768552],
       [-0.71400102, -0.11157372, -0.05273129, -1.31130646],
       [ 1.23977937, -0.12684053,  0.56747507,  0.38559187]])

In [57]:
mask = (names == 'Bob') | (names == 'Will')
mask


Out[57]:
array([ True, False,  True,  True,  True, False, False], dtype=bool)

In [58]:
data[mask]


Out[58]:
array([[-1.02507637,  0.4954459 ,  0.48120621, -0.57415051],
       [ 0.96470466, -0.072239  , -1.03371183,  0.3060512 ],
       [-2.46213906, -0.28323223,  1.65348566, -0.88040289],
       [ 0.74105795, -1.19107693, -0.30397068, -1.73768552]])

In [59]:
data[data < 0] = 0
data


Out[59]:
array([[ 0.        ,  0.4954459 ,  0.48120621,  0.        ],
       [ 0.        ,  0.60428499,  0.12993482,  0.09675807],
       [ 0.96470466,  0.        ,  0.        ,  0.3060512 ],
       [ 0.        ,  0.        ,  1.65348566,  0.        ],
       [ 0.74105795,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ],
       [ 1.23977937,  0.        ,  0.56747507,  0.38559187]])

In [60]:
data[names != 'Joe'] = 7
data


Out[60]:
array([[ 7.        ,  7.        ,  7.        ,  7.        ],
       [ 0.        ,  0.60428499,  0.12993482,  0.09675807],
       [ 7.        ,  7.        ,  7.        ,  7.        ],
       [ 7.        ,  7.        ,  7.        ,  7.        ],
       [ 7.        ,  7.        ,  7.        ,  7.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ],
       [ 1.23977937,  0.        ,  0.56747507,  0.38559187]])

In [61]:
arr = np.empty((8, 4))
for i in range(8):
    arr[i] = i
arr


Out[61]:
array([[ 0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  2.],
       [ 3.,  3.,  3.,  3.],
       [ 4.,  4.,  4.,  4.],
       [ 5.,  5.,  5.,  5.],
       [ 6.,  6.,  6.,  6.],
       [ 7.,  7.,  7.,  7.]])

In [62]:
arr[[4, 3, 0, 6]]


Out[62]:
array([[ 4.,  4.,  4.,  4.],
       [ 3.,  3.,  3.,  3.],
       [ 0.,  0.,  0.,  0.],
       [ 6.,  6.,  6.,  6.]])

In [63]:
arr[[-3, -5, -7]]


Out[63]:
array([[ 5.,  5.,  5.,  5.],
       [ 3.,  3.,  3.,  3.],
       [ 1.,  1.,  1.,  1.]])

In [65]:
arr = np.arange(32).reshape((8, 4))
arr


Out[65]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31]])

In [66]:
arr[[1, 5, 7, 2], [0, 3, 1, 2]]


Out[66]:
array([ 4, 23, 29, 10])

In [67]:
arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]


Out[67]:
array([[ 4,  7,  5,  6],
       [20, 23, 21, 22],
       [28, 31, 29, 30],
       [ 8, 11,  9, 10]])

In [68]:
arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])]


Out[68]:
array([[ 4,  7,  5,  6],
       [20, 23, 21, 22],
       [28, 31, 29, 30],
       [ 8, 11,  9, 10]])

In [70]:
arr = np.arange(15).reshape((5, 3))
arr


Out[70]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])

In [71]:
np.dot(arr.T, arr)


Out[71]:
array([[270, 300, 330],
       [300, 335, 370],
       [330, 370, 410]])

In [73]:
arr = np.arange(16).reshape((2, 2, 4))
arr


Out[73]:
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])

In [75]:
arr.transpose((1, 0, 2))


Out[75]:
array([[[ 0,  1,  2,  3],
        [ 8,  9, 10, 11]],

       [[ 4,  5,  6,  7],
        [12, 13, 14, 15]]])

In [76]:
arr = np.arange(10)
np.sqrt(arr)


Out[76]:
array([ 0.        ,  1.        ,  1.41421356,  1.73205081,  2.        ,
        2.23606798,  2.44948974,  2.64575131,  2.82842712,  3.        ])

In [77]:
np.exp(arr)


Out[77]:
array([  1.00000000e+00,   2.71828183e+00,   7.38905610e+00,
         2.00855369e+01,   5.45981500e+01,   1.48413159e+02,
         4.03428793e+02,   1.09663316e+03,   2.98095799e+03,
         8.10308393e+03])

In [78]:
x = np.random.rand(8)
y = np.random.rand(8)
x


Out[78]:
array([ 0.58688864,  0.79713185,  0.51880263,  0.04174489,  0.17906691,
        0.10172631,  0.24571787,  0.78487748])

In [79]:
y


Out[79]:
array([ 0.74709456,  0.81212746,  0.35239972,  0.0117329 ,  0.7445411 ,
        0.56914803,  0.44465982,  0.12246176])

In [80]:
np.maximum(x, y)


Out[80]:
array([ 0.74709456,  0.81212746,  0.51880263,  0.04174489,  0.7445411 ,
        0.56914803,  0.44465982,  0.78487748])

In [81]:
arr = np.random.rand(7) * 5
arr


Out[81]:
array([ 1.62942281,  3.55597796,  0.91382494,  3.35444082,  0.33686222,
        3.22943479,  1.67379681])

In [82]:
np.modf(arr)


Out[82]:
(array([ 0.62942281,  0.55597796,  0.91382494,  0.35444082,  0.33686222,
         0.22943479,  0.67379681]), array([ 1.,  3.,  0.,  3.,  0.,  3.,  1.]))

In [83]:
points = np.arange(-5, 5, 0.01)
xs, ys = np.meshgrid(points, points)
xs


Out[83]:
array([[-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       ..., 
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99]])

In [84]:
ys


Out[84]:
array([[-5.  , -5.  , -5.  , ..., -5.  , -5.  , -5.  ],
       [-4.99, -4.99, -4.99, ..., -4.99, -4.99, -4.99],
       [-4.98, -4.98, -4.98, ..., -4.98, -4.98, -4.98],
       ..., 
       [ 4.97,  4.97,  4.97, ...,  4.97,  4.97,  4.97],
       [ 4.98,  4.98,  4.98, ...,  4.98,  4.98,  4.98],
       [ 4.99,  4.99,  4.99, ...,  4.99,  4.99,  4.99]])

In [85]:
import matplotlib.pyplot as plt
z = np.sqrt(xs ** 2 + ys ** 2)
z


Out[85]:
array([[ 7.07106781,  7.06400028,  7.05693985, ...,  7.04988652,
         7.05693985,  7.06400028],
       [ 7.06400028,  7.05692568,  7.04985815, ...,  7.04279774,
         7.04985815,  7.05692568],
       [ 7.05693985,  7.04985815,  7.04278354, ...,  7.03571603,
         7.04278354,  7.04985815],
       ..., 
       [ 7.04988652,  7.04279774,  7.03571603, ...,  7.0286414 ,
         7.03571603,  7.04279774],
       [ 7.05693985,  7.04985815,  7.04278354, ...,  7.03571603,
         7.04278354,  7.04985815],
       [ 7.06400028,  7.05692568,  7.04985815, ...,  7.04279774,
         7.04985815,  7.05692568]])

In [94]:
im = plt.imshow(z, cmap = plt.cm.gray)
plt.colorbar(im)
plt.title('Image plot of $\sqrt{x^2 + y^2}$ for a grid of values')
plt.show()



In [96]:
x = np.arange(1.1, 1.6, 0.1)
y = np.arange(2.1, 2.6, 0.1)
cond = np.array([True, False, True, True, False])
results = [(x if c else y) for x, y, c in zip(x, y, cond)]
results


Out[96]:
[1.1000000000000001,
 2.2000000000000002,
 1.3000000000000003,
 1.4000000000000004,
 2.5000000000000004]

In [97]:
np.where(cond, x, y)


Out[97]:
array([ 1.1,  2.2,  1.3,  1.4,  2.5])

In [100]:
arr = np.random.randn(4, 4)
arr


Out[100]:
array([[-0.06816602,  1.67322443,  2.04080301,  0.158113  ],
       [ 0.76445377,  1.64652579,  1.5852583 ,  0.22501969],
       [-1.20454604, -0.15219593,  0.97212465, -0.04184555],
       [ 0.94095142, -0.08079224,  0.17258112, -1.12260666]])

In [101]:
arr = np.where(arr > 0, 2, -2)
arr


Out[101]:
array([[-2,  2,  2,  2],
       [ 2,  2,  2,  2],
       [-2, -2,  2, -2],
       [ 2, -2,  2, -2]])

In [102]:
arr = np.random.randn(5, 4)
arr.mean()


Out[102]:
0.14356080981279939

In [103]:
np.mean(arr)


Out[103]:
0.14356080981279939

In [104]:
arr.sum()


Out[104]:
2.8712161962559879

In [105]:
arr


Out[105]:
array([[-0.16634514,  0.96288864,  0.34584897,  0.72711133],
       [ 0.40054813,  0.78953209, -0.13187233, -0.52535842],
       [ 1.62799037, -0.03725593,  0.7542657 ,  0.52100022],
       [ 0.58173638, -0.11504556,  0.62555739, -1.64303679],
       [-0.75754755, -0.52643384, -0.07722036, -0.48514707]])

In [106]:
arr.mean(axis = 1)


Out[106]:
array([ 0.46737595,  0.13321237,  0.71650009, -0.13769715, -0.46158721])

In [107]:
arr.mean(axis = 0)


Out[107]:
array([ 0.33727644,  0.21473708,  0.30331587, -0.28108615])

In [108]:
np.cumsum(arr, axis = 0)


Out[108]:
array([[-0.16634514,  0.96288864,  0.34584897,  0.72711133],
       [ 0.23420299,  1.75242072,  0.21397664,  0.20175291],
       [ 1.86219335,  1.71516479,  0.96824234,  0.72275313],
       [ 2.44392973,  1.60011923,  1.59379973, -0.92028366],
       [ 1.68638218,  1.07368539,  1.51657937, -1.40543073]])

In [109]:
np.unique(names)


Out[109]:
array(['Bob', 'Joe', 'Will'], 
      dtype='<U4')

In [110]:
ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])
np.unique(ints)


Out[110]:
array([1, 2, 3, 4])

In [111]:
values = np.array([6, 0, 0, 3, 2, 5, 6])
np.in1d(values, [2, 3, 6])


Out[111]:
array([ True, False, False,  True,  True, False,  True], dtype=bool)

In [112]:
arr = np.arange(10)
np.save('some_array', arr)

In [113]:
np.load('some_array.npy')


Out[113]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [114]:
x = np.arange(6).reshape((2, 3))
y = np.arange(6, 12).reshape((3, 2))

In [115]:
x


Out[115]:
array([[0, 1, 2],
       [3, 4, 5]])

In [116]:
y


Out[116]:
array([[ 6,  7],
       [ 8,  9],
       [10, 11]])

In [117]:
np.dot(x, y)


Out[117]:
array([[ 28,  31],
       [100, 112]])

In [118]:
from numpy.linalg import inv, qr
x = np.random.randn(5, 5)
mat = np.dot(x.T, x)
inv(mat)


Out[118]:
array([[ 0.42245466,  0.15108592,  0.11289992,  0.15204209,  0.17084306],
       [ 0.15108592,  0.20832004,  0.10617203, -0.05017431,  0.08583619],
       [ 0.11289992,  0.10617203,  0.25050413, -0.07453367,  0.08870614],
       [ 0.15204209, -0.05017431, -0.07453367,  0.42835999,  0.13848224],
       [ 0.17084306,  0.08583619,  0.08870614,  0.13848224,  0.31600837]])

In [119]:
np.dot(mat, inv(mat))


Out[119]:
array([[  1.00000000e+00,   1.31838984e-16,  -1.38777878e-17,
          0.00000000e+00,  -5.55111512e-17],
       [ -2.77555756e-17,   1.00000000e+00,  -8.32667268e-17,
         -1.94289029e-16,   5.55111512e-17],
       [ -5.55111512e-17,   5.55111512e-17,   1.00000000e+00,
          1.11022302e-16,   5.55111512e-17],
       [ -2.22044605e-16,  -8.32667268e-17,  -2.77555756e-17,
          1.00000000e+00,  -1.11022302e-16],
       [  0.00000000e+00,   0.00000000e+00,   1.66533454e-16,
          0.00000000e+00,   1.00000000e+00]])

In [123]:
np.dot(mat, inv(mat)).astype('int64')


Out[123]:
array([[1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1]], dtype=int64)

In [128]:
nsteps = 1000
draws = np.random.randint(-1, 2, size = nsteps)
steps = np.where(draws > 0, 1, -1)
walk = steps.cumsum()
walk


Out[128]:
array([   1,    2,    1,    2,    1,    2,    1,    0,   -1,   -2,   -3,
         -2,   -3,   -2,   -1,   -2,   -1,   -2,   -3,   -2,   -1,    0,
         -1,   -2,   -3,   -4,   -5,   -6,   -7,   -8,   -9,  -10,   -9,
        -10,  -11,  -12,  -13,  -14,  -13,  -12,  -13,  -14,  -13,  -14,
        -13,  -14,  -15,  -16,  -17,  -18,  -19,  -18,  -19,  -20,  -19,
        -18,  -19,  -18,  -17,  -16,  -17,  -16,  -15,  -14,  -13,  -14,
        -15,  -16,  -15,  -14,  -15,  -16,  -15,  -14,  -15,  -16,  -17,
        -16,  -15,  -14,  -15,  -16,  -17,  -18,  -17,  -18,  -17,  -18,
        -17,  -18,  -19,  -18,  -19,  -18,  -17,  -18,  -19,  -20,  -21,
        -22,  -23,  -22,  -23,  -22,  -21,  -20,  -19,  -18,  -17,  -18,
        -19,  -20,  -19,  -20,  -21,  -22,  -21,  -20,  -19,  -20,  -21,
        -22,  -21,  -22,  -21,  -20,  -21,  -20,  -21,  -22,  -21,  -20,
        -19,  -20,  -21,  -22,  -23,  -24,  -25,  -26,  -25,  -26,  -27,
        -28,  -29,  -28,  -27,  -28,  -29,  -30,  -31,  -30,  -31,  -32,
        -31,  -32,  -31,  -32,  -33,  -34,  -33,  -34,  -35,  -36,  -37,
        -38,  -37,  -36,  -37,  -38,  -37,  -38,  -39,  -38,  -37,  -38,
        -39,  -40,  -39,  -40,  -41,  -42,  -41,  -40,  -41,  -40,  -41,
        -42,  -43,  -44,  -45,  -46,  -45,  -44,  -45,  -44,  -43,  -44,
        -45,  -46,  -45,  -44,  -43,  -44,  -43,  -42,  -41,  -40,  -41,
        -42,  -43,  -42,  -41,  -42,  -43,  -42,  -43,  -44,  -43,  -44,
        -45,  -46,  -47,  -48,  -49,  -50,  -51,  -52,  -53,  -52,  -53,
        -54,  -55,  -56,  -57,  -56,  -55,  -56,  -57,  -56,  -55,  -56,
        -55,  -56,  -57,  -58,  -57,  -58,  -57,  -56,  -57,  -56,  -57,
        -56,  -57,  -58,  -59,  -58,  -57,  -58,  -59,  -58,  -57,  -58,
        -59,  -60,  -61,  -62,  -61,  -62,  -63,  -64,  -65,  -66,  -67,
        -66,  -67,  -68,  -69,  -68,  -67,  -68,  -67,  -68,  -67,  -68,
        -67,  -66,  -67,  -68,  -69,  -70,  -69,  -70,  -71,  -70,  -71,
        -72,  -71,  -72,  -71,  -70,  -71,  -70,  -69,  -70,  -69,  -70,
        -71,  -72,  -73,  -74,  -73,  -74,  -75,  -76,  -75,  -74,  -75,
        -76,  -77,  -78,  -77,  -78,  -79,  -78,  -77,  -78,  -79,  -80,
        -79,  -80,  -81,  -82,  -81,  -82,  -81,  -82,  -81,  -82,  -83,
        -84,  -85,  -84,  -85,  -86,  -87,  -88,  -87,  -88,  -89,  -88,
        -89,  -90,  -91,  -90,  -91,  -90,  -91,  -92,  -91,  -92,  -93,
        -92,  -91,  -92,  -91,  -90,  -91,  -90,  -91,  -90,  -89,  -90,
        -89,  -90,  -91,  -92,  -93,  -92,  -93,  -94,  -93,  -94,  -95,
        -96,  -97,  -98,  -99, -100, -101, -100, -101, -100, -101, -100,
        -99, -100, -101, -102, -103, -102, -103, -102, -101, -100,  -99,
       -100, -101, -102, -103, -104, -105, -104, -103, -102, -103, -104,
       -105, -104, -105, -104, -103, -104, -105, -106, -107, -108, -109,
       -108, -109, -110, -111, -112, -113, -114, -115, -116, -115, -114,
       -113, -112, -113, -114, -113, -112, -113, -114, -115, -116, -115,
       -116, -117, -118, -119, -118, -119, -118, -117, -118, -119, -120,
       -121, -122, -123, -122, -123, -124, -125, -124, -125, -126, -127,
       -126, -125, -126, -127, -128, -129, -128, -129, -128, -127, -128,
       -129, -128, -129, -128, -127, -126, -125, -126, -127, -128, -129,
       -130, -131, -132, -133, -134, -135, -136, -137, -136, -135, -134,
       -133, -134, -133, -134, -135, -134, -135, -136, -137, -136, -137,
       -138, -139, -138, -137, -138, -137, -138, -139, -138, -137, -138,
       -139, -140, -139, -138, -137, -136, -135, -136, -137, -136, -137,
       -136, -137, -138, -139, -140, -141, -142, -143, -144, -143, -142,
       -143, -144, -143, -144, -145, -146, -145, -146, -147, -146, -145,
       -146, -147, -148, -147, -148, -149, -150, -151, -152, -151, -152,
       -153, -154, -153, -154, -155, -156, -155, -154, -155, -156, -157,
       -158, -159, -158, -159, -160, -161, -162, -163, -164, -165, -166,
       -165, -166, -165, -164, -165, -164, -165, -166, -167, -166, -167,
       -168, -167, -166, -167, -166, -167, -168, -167, -166, -167, -166,
       -167, -166, -167, -168, -169, -170, -171, -172, -173, -174, -175,
       -174, -175, -174, -173, -172, -173, -172, -173, -174, -175, -174,
       -175, -176, -177, -178, -179, -180, -181, -180, -181, -180, -181,
       -180, -181, -182, -181, -182, -183, -184, -183, -182, -183, -182,
       -183, -184, -183, -184, -185, -186, -187, -188, -187, -186, -185,
       -186, -187, -188, -189, -190, -191, -190, -191, -192, -193, -194,
       -195, -196, -195, -196, -197, -196, -195, -196, -197, -198, -199,
       -200, -199, -198, -199, -200, -201, -200, -201, -202, -203, -204,
       -203, -204, -205, -206, -207, -208, -209, -210, -211, -212, -213,
       -214, -215, -214, -215, -216, -217, -216, -217, -218, -219, -220,
       -219, -220, -219, -218, -219, -220, -221, -222, -223, -224, -225,
       -226, -227, -228, -227, -226, -225, -226, -225, -226, -227, -228,
       -229, -228, -227, -228, -227, -226, -225, -226, -225, -224, -223,
       -224, -225, -226, -225, -226, -225, -226, -225, -226, -225, -224,
       -225, -226, -227, -228, -229, -230, -229, -230, -229, -228, -229,
       -230, -231, -232, -233, -234, -235, -236, -237, -238, -239, -240,
       -241, -240, -239, -240, -241, -242, -241, -242, -241, -242, -241,
       -242, -241, -240, -241, -242, -243, -244, -245, -246, -245, -244,
       -245, -244, -243, -244, -245, -246, -245, -246, -247, -248, -249,
       -250, -249, -250, -251, -252, -251, -250, -251, -252, -253, -252,
       -251, -250, -251, -252, -253, -252, -251, -252, -253, -254, -255,
       -256, -255, -256, -257, -256, -255, -256, -257, -258, -259, -260,
       -261, -262, -261, -262, -263, -264, -263, -264, -263, -262, -261,
       -262, -261, -262, -263, -264, -265, -266, -267, -268, -269, -270,
       -271, -270, -271, -272, -273, -274, -275, -276, -277, -278, -279,
       -280, -281, -280, -281, -282, -281, -282, -281, -280, -281, -282,
       -283, -284, -285, -286, -287, -288, -287, -288, -289, -288, -289,
       -290, -291, -292, -293, -294, -295, -296, -297, -296, -295, -296,
       -297, -298, -299, -298, -299, -298, -299, -300, -301, -300, -301,
       -300, -301, -302, -303, -304, -305, -306, -307, -308, -307, -308,
       -307, -308, -307, -308, -309, -310, -311, -312, -311, -312, -311,
       -310, -311, -310, -311, -312, -313, -314, -313, -312, -313, -314,
       -315, -316, -317, -318, -319, -320, -321, -322, -323, -324, -325,
       -326, -325, -326, -325, -326, -327, -328, -329, -330, -331, -332,
       -331, -330, -331, -332, -333, -334, -335, -336, -337, -338], dtype=int32)

In [129]:
walk.min()


Out[129]:
-338

In [130]:
walk.max()


Out[130]:
2

In [131]:
(np.abs(walk) >= 10).argmax()


Out[131]:
31

In [133]:
nwalks = 5000
nsteps = 1000
draws = np.random.randint(0, 2, size = (nwalks, nsteps))
steps = np.where(draws > 0, 1, -1)
walks = steps.cumsum(axis = 1)
walks


Out[133]:
array([[  1,   2,   1, ..., -14, -13, -12],
       [ -1,   0,  -1, ...,  -6,  -7,  -8],
       [ -1,   0,  -1, ...,  -2,  -1,  -2],
       ..., 
       [  1,   2,   1, ...,  28,  27,  26],
       [  1,   0,   1, ...,  10,   9,  10],
       [ -1,  -2,  -1, ..., -42, -41, -42]], dtype=int32)

In [134]:
walks.max()


Out[134]:
124

In [135]:
walks.min()


Out[135]:
-116

In [136]:
hits30 = (np.abs(walks) >= 30).any(axis = 1)
hits30


Out[136]:
array([False, False,  True, ...,  True,  True,  True], dtype=bool)

In [137]:
hits30.sum()


Out[137]:
3409

In [138]:
crossing_times = (np.abs(walks[hits30]) >= 30).argmax(axis = 1)
crossing_times


Out[138]:
array([395, 771, 317, ..., 615, 109, 413], dtype=int64)

In [139]:
crossing_times.mean()


Out[139]:
505.98151950718687

In [ ]: