%matplotlib inline
/usr/local/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.') /usr/local/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
from __future__ import division
from numpy.random import randn
import numpy as np
np.set_printoptions(precision = 4, suppress = True)
data = randn(2, 3)
data
array([[-1.1288, 0.1897, 0.7566], [-0.4347, -0.5984, -0.6985]])
data * 10
array([[-11.2878, 1.8974, 7.5657], [ -4.3468, -5.9838, -6.9849]])
data + data
array([[-2.2576, 0.3795, 1.5131], [-0.8694, -1.1968, -1.397 ]])
data.shape
(2, 3)
data.dtype
dtype('float64')
data1 = [6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
arr1
array([ 6. , 7.5, 8. , 0. , 1. ])
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr2 = np.array(data2)
arr2
array([[1, 2, 3, 4], [5, 6, 7, 8]])
arr2.ndim
2
arr2.shape
(2, 4)
arr1.dtype
dtype('float64')
arr2.dtype
dtype('int64')
np.zeros(10)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros((3, 6))
array([[ 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0.]])
np.ones(10)
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.ones((2, 3))
array([[ 1., 1., 1.], [ 1., 1., 1.]])
np.empty((2, 3, 2))
array([[[ 3.1050e+231, 1.7306e-077], [ 2.9644e-323, 0.0000e+000], [ 0.0000e+000, 0.0000e+000]], [[ 0.0000e+000, 0.0000e+000], [ 0.0000e+000, 0.0000e+000], [ 0.0000e+000, 8.3440e-309]]])
np.arange(15)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
arr3 = np.array([1, 2, 3], dtype = np.float64)
arr3.dtype
dtype('float64')
arr4 = np.array([1, 2, 3], dtype = np.int64)
arr4.dtype
dtype('int64')
arr5 = np.array([1, 2, 3, 4, 5])
arr5.dtype
dtype('int64')
arr_float = arr5.astype(np.float64)
arr_float.dtype
dtype('float64')
arr6 = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
arr6.dtype
dtype('float64')
arr6.astype(np.int32)
array([ 3, -1, -2, 0, 12, 10], dtype=int32)
numeric_strings = np.array(['1.25', '-9.6', '42'], dtype = np.string_)
numeric_strings.astype(float)
array([ 1.25, -9.6 , 42. ])
int_array = np.arange(10)
calibers = np.array([0.22, 0.270, .357, .380, .44, .50], dtype = np.float64)
int_array.astype(calibers.dtype)
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
empty_unit32 = np.empty(8, dtype = 'u4')
empty_unit32
array([ 0, 1879048192, 3537412978, 805308408, 6, 0, 0, 393216], dtype=uint32)
arr7 = np.array([[1, 2, 3], [4, 5, 6]])
arr7
array([[1, 2, 3], [4, 5, 6]])
arr7 + arr7
array([[ 2, 4, 6], [ 8, 10, 12]])
arr7 * arr7
array([[ 1, 4, 9], [16, 25, 36]])
1/arr7
array([[ 1. , 0.5 , 0.3333], [ 0.25 , 0.2 , 0.1667]])
arr7**0.5
array([[ 1. , 1.4142, 1.7321], [ 2. , 2.2361, 2.4495]])
arr8 = np.arange(10)
arr8
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr8[5]
5
arr8[5:8]
array([5, 6, 7])
arr8[5:8] = 12
arr8
array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
array_slice = arr8[5:8]
array_slice
array([12, 12, 12])
array_slice[1] = 12345
array_slice
array([ 12, 12345, 12])
array_slice[:] = 64
array_slice
array([64, 64, 64])
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr2d[2]
array([7, 8, 9])
arr2d[0][2]
3
arr2d[0, 2]
3
arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
arr3d
array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]]])
arr3d[0]
array([[1, 2, 3], [4, 5, 6]])
arr3d[0, 1]
array([4, 5, 6])
arr3d[0][1]
array([4, 5, 6])
arr3d[0][1][2]
6
old_values = arr3d[0].copy()
arr3d[0] = 42
arr3d
array([[[42, 42, 42], [42, 42, 42]], [[ 7, 8, 9], [10, 11, 12]]])
arr3d[0] = old_values
arr3d
array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]]])
arr3d[1, 0]
array([7, 8, 9])
arr8
array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])
arr8[1:6]
array([ 1, 2, 3, 4, 64])
arr2d
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr2d[:2]
array([[1, 2, 3], [4, 5, 6]])
arr2d[:2, 1:]
array([[2, 3], [5, 6]])
arr2d[1, :2]
array([4, 5])
arr2d[2, :1]
array([7])
arr2d[:, :1]
array([[1], [4], [7]])
arr2d[:2, 1:] = 0
arr2d
array([[1, 0, 0], [4, 0, 0], [7, 8, 9]])
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
names
array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], dtype='<U4')
data = randn(7, 4)
data
array([[ 1.5667, 1.332 , -1.1835, -0.45 ], [-0.1619, 1.067 , -0.691 , -0.6781], [ 1.9966, 1.0252, 1.5692, -0.6976], [ 1.931 , 0.801 , -0.4198, 0.2899], [ 0.0727, -0.0716, -0.3509, 1.3726], [-1.3476, -2.0312, 0.4368, -0.1413], [-0.3111, -0.5803, -0.8319, -0.2031]])
names == 'Bob'
array([ True, False, False, True, False, False, False], dtype=bool)
data[names == 'Bob']
array([[ 1.5667, 1.332 , -1.1835, -0.45 ], [ 1.931 , 0.801 , -0.4198, 0.2899]])
data[names == 'Bob', 3:]
array([[-0.45 ], [ 0.2899]])
data[names == 'Bob', 2]
array([-1.1835, -0.4198])
names != 'Bob'
array([False, True, True, False, True, True, True], dtype=bool)
data[names != 'Bob']
array([[-0.1619, 1.067 , -0.691 , -0.6781], [ 1.9966, 1.0252, 1.5692, -0.6976], [ 0.0727, -0.0716, -0.3509, 1.3726], [-1.3476, -2.0312, 0.4368, -0.1413], [-0.3111, -0.5803, -0.8319, -0.2031]])
data[~(names == 'Bob')]
array([[-0.1619, 1.067 , -0.691 , -0.6781], [ 1.9966, 1.0252, 1.5692, -0.6976], [ 0.0727, -0.0716, -0.3509, 1.3726], [-1.3476, -2.0312, 0.4368, -0.1413], [-0.3111, -0.5803, -0.8319, -0.2031]])
mask = (names == 'Bob') | (names == 'Will')
mask
array([ True, False, True, True, True, False, False], dtype=bool)
data[mask]
array([[ 1.5667, 1.332 , -1.1835, -0.45 ], [ 1.9966, 1.0252, 1.5692, -0.6976], [ 1.931 , 0.801 , -0.4198, 0.2899], [ 0.0727, -0.0716, -0.3509, 1.3726]])
data[data < 0] = 0
data
array([[ 1.5667, 1.332 , 0. , 0. ], [ 0. , 1.067 , 0. , 0. ], [ 1.9966, 1.0252, 1.5692, 0. ], [ 1.931 , 0.801 , 0. , 0.2899], [ 0.0727, 0. , 0. , 1.3726], [ 0. , 0. , 0.4368, 0. ], [ 0. , 0. , 0. , 0. ]])
data[names != 'Joe'] = 7
data
array([[ 7. , 7. , 7. , 7. ], [ 0. , 1.067 , 0. , 0. ], [ 7. , 7. , 7. , 7. ], [ 7. , 7. , 7. , 7. ], [ 7. , 7. , 7. , 7. ], [ 0. , 0. , 0.4368, 0. ], [ 0. , 0. , 0. , 0. ]])
arr9 = np.empty((8, 4))
arr9
array([[ 3.1050e+231, 3.1050e+231, 2.2318e-314, 2.4703e-323], [ 0.0000e+000, 0.0000e+000, 0.0000e+000, 0.0000e+000], [ 0.0000e+000, 0.0000e+000, 0.0000e+000, 0.0000e+000], [ 0.0000e+000, 0.0000e+000, 0.0000e+000, 0.0000e+000], [ 0.0000e+000, 0.0000e+000, 0.0000e+000, 0.0000e+000], [ 0.0000e+000, 0.0000e+000, -3.9843e-048, 0.0000e+000], [ 0.0000e+000, -7.5927e-076, 0.0000e+000, 1.9469e-308], [ 0.0000e+000, 3.9525e-323, 2.2318e-314, 2.4703e-323]])
for i in range(8):
arr9[i] = i
arr9
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.]])
arr9[[4, 3, 0, 6]]
array([[ 4., 4., 4., 4.], [ 3., 3., 3., 3.], [ 0., 0., 0., 0.], [ 6., 6., 6., 6.]])
arr9[[-3, -5, -7]]
array([[ 5., 5., 5., 5.], [ 3., 3., 3., 3.], [ 1., 1., 1., 1.]])
arr10 = np.arange(32).reshape((8, 4))
arr10
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]])
arr10[[1, 5, 7, 2], [0, 3, 1, 2]]
array([ 4, 23, 29, 10])
arr10[[1, 5, 7, 3]][:, [0, 3, 1, 2]]
array([[ 4, 7, 5, 6], [20, 23, 21, 22], [28, 31, 29, 30], [12, 15, 13, 14]])
arr10[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])]
array([[ 4, 7, 5, 6], [20, 23, 21, 22], [28, 31, 29, 30], [ 8, 11, 9, 10]])
arr11 = np.arange(15).reshape((3, 5))
arr11
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
arr11.T
array([[ 0, 5, 10], [ 1, 6, 11], [ 2, 7, 12], [ 3, 8, 13], [ 4, 9, 14]])
arr12 = np.random.randn(6, 3)
arr12
array([[-1.7875, -0.1715, 0.0374], [ 1.9915, -0.8422, 0.9459], [ 0.0683, -1.3781, 0.7558], [-1.8136, 0.3604, -0.7077], [ 1.632 , -0.3238, -0.3254], [ 3.1159, 1.0252, -1.7004]])
np.dot(arr12, arr12.T)
array([[ 3.226 , -3.3801, 0.1426, 3.1535, -2.8738, -5.8091], [ -3.3801, 5.5703, 2.0115, -4.5847, 3.2152, 3.7336], [ 0.1426, 2.0115, 2.475 , -1.1553, 0.3117, -2.4853], [ 3.1535, -4.5847, -1.1553, 3.9197, -2.8462, -4.0781], [ -2.8738, 3.2152, 0.3117, -2.8462, 2.8741, 5.3063], [ -5.8091, 3.7336, -2.4853, -4.0781, 5.3063, 13.6511]])
np.dot(arr12.T, arr12)
array([[ 22.8272, 0.5475, -2.6773], [ 0.5475, 3.9235, -3.7375], [ -2.6773, -3.7375, 4.9654]])
arr13 = np.arange(16).reshape((2, 2, 4))
arr13
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7]], [[ 8, 9, 10, 11], [12, 13, 14, 15]]])
arr13.transpose((1, 0, 2))
array([[[ 0, 1, 2, 3], [ 8, 9, 10, 11]], [[ 4, 5, 6, 7], [12, 13, 14, 15]]])
arr13.swapaxes(1, 2)
array([[[ 0, 4], [ 1, 5], [ 2, 6], [ 3, 7]], [[ 8, 12], [ 9, 13], [10, 14], [11, 15]]])
arr14 = np.arange(10)
arr14
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.sqrt(arr14)
array([ 0. , 1. , 1.4142, 1.7321, 2. , 2.2361, 2.4495, 2.6458, 2.8284, 3. ])
np.exp(arr14)
array([ 1. , 2.7183, 7.3891, 20.0855, 54.5982, 148.4132, 403.4288, 1096.6332, 2980.958 , 8103.0839])
x = randn(8)
x
array([-1.1566, 0.1062, 0.1054, 0.0743, 0.2924, -0.4461, -1.7097, -0.3379])
y = randn(8)
y
array([ 0.3624, 0.257 , 0.2404, 0.5182, 1.3033, 1.1561, 0.0866, 0.6654])
np.maximum(x, y)
array([ 0.3624, 0.257 , 0.2404, 0.5182, 1.3033, 1.1561, 0.0866, 0.6654])
arr15 = randn(7) * 5
arr15
array([-0.0813, 1.8273, -1.6541, 0.5038, -0.6916, 4.2674, -2.447 ])
np.modf(arr15)
(array([-0.0813, 0.8273, -0.6541, 0.5038, -0.6916, 0.2674, -0.447 ]), array([-0., 1., -1., 0., -0., 4., -2.]))
points = np.arange(-5, 5, 0.01)
xs, ys = np.meshgrid(points, points)
xs
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]])
from matplotlib.pyplot import imshow, title
import matplotlib.pyplot as plt
z = np.sqrt(xs ** 2 + ys ** 2)
z
array([[ 7.0711, 7.064 , 7.0569, ..., 7.0499, 7.0569, 7.064 ], [ 7.064 , 7.0569, 7.0499, ..., 7.0428, 7.0499, 7.0569], [ 7.0569, 7.0499, 7.0428, ..., 7.0357, 7.0428, 7.0499], ..., [ 7.0499, 7.0428, 7.0357, ..., 7.0286, 7.0357, 7.0428], [ 7.0569, 7.0499, 7.0428, ..., 7.0357, 7.0428, 7.0499], [ 7.064 , 7.0569, 7.0499, ..., 7.0428, 7.0499, 7.0569]])
plt.imshow(z, cmap = plt.cm.gray)
plt.colorbar()
plt.title('Image plot of $\sqrt{x^2 + y^2}$ for a grid of values')
<matplotlib.text.Text at 0x118a56630>
xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])
cond = np.array([True, False, True, True, False])
result = [(x if c else y)
for x, y, c in zip(xarr, yarr, cond)]
result
[1.1000000000000001, 2.2000000000000002, 1.3, 1.3999999999999999, 2.5]
result = np.where(cond, xarr, yarr)
result
array([ 1.1, 2.2, 1.3, 1.4, 2.5])
arr16 = randn(4, 4)
arr16
array([[ 0.6851, -0.6378, -0.8524, 1.3891], [ 0.1806, 1.2007, 0.5101, -0.2702], [-0.717 , 0.783 , 0.1876, 0.8501], [ 0.0737, -0.3917, 0.6096, 1.2823]])
np.where(arr16 > 0, 2, -2)
array([[ 2, -2, -2, 2], [ 2, 2, 2, -2], [-2, 2, 2, 2], [ 2, -2, 2, 2]])
np.where(arr16 > 0, 2, arr16)
array([[ 2. , -0.6378, -0.8524, 2. ], [ 2. , 2. , 2. , -0.2702], [-0.717 , 2. , 2. , 2. ], [ 2. , -0.3917, 2. , 2. ]])
arr17 = np.random.randn(4, 5)
arr17
array([[ 1.6533, 0.1569, 1.1953, 0.5076, -0.6969], [ 0.3001, 1.3571, 0.7924, 0.5225, 0.777 ], [ 0.326 , -0.4121, -0.4198, -0.44 , 1.7303], [ 1.4647, 0.1069, 0.0008, -0.1721, 0.0581]])
arr17.mean()
0.44040416796585308
np.mean(arr17)
0.44040416796585308
arr17.sum()
8.8080833593170613
arr17.mean(axis = 1) # row
array([ 0.5632, 0.7498, 0.1569, 0.2917])
arr17.mean(axis = 0) # column
array([ 0.9361, 0.3022, 0.3922, 0.1045, 0.4671])
arr18 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr18
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr18.cumsum(0) # sum of column
array([[ 1, 2, 3], [ 5, 7, 9], [12, 15, 18]])
arr18.cumprod(1) # prod of row
array([[ 1, 2, 6], [ 4, 20, 120], [ 7, 56, 504]])
arr19 = randn(100)
(arr19 > 0).sum()
50
bools = np.array([False, False, True, False])
bools.any()
True
bools.all()
False
arr20 = randn(8)
arr20
array([-0.5483, 1.4955, 0.5092, -0.8928, -1.7595, 1.3327, 0.4815, -1.1086])
arr20.sort()
arr20
array([-1.7595, -1.1086, -0.8928, -0.5483, 0.4815, 0.5092, 1.3327, 1.4955])
arr21 = randn(5, 3)
arr21
array([[ 0.1766, -2.2486, 1.2854], [ 0.3819, 0.1207, 0.5651], [ 0.9679, 0.9795, 0.8815], [ 0.2147, -1.4361, 0.7716], [-0.2916, 0.5463, -0.827 ]])
arr21.sort(1)
arr21
array([[-2.2486, 0.1766, 1.2854], [ 0.1207, 0.3819, 0.5651], [ 0.8815, 0.9679, 0.9795], [-1.4361, 0.2147, 0.7716], [-0.827 , -0.2916, 0.5463]])
large_array = randn(1000)
large_array.sort()
large_array[int(0.05 * len(large_array))]
-1.6186837940856837
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
np.unique(names)
array(['Bob', 'Joe', 'Will'], dtype='<U4')
ints = np.array([3, 2, 3, 2, 2, 1, 1, 4, 4])
np.unique(ints)
array([1, 2, 3, 4])
sorted(set(names))
['Bob', 'Joe', 'Will']
sorted(set(ints))
[1, 2, 3, 4]
values = np.array([6, 0, 0, 3, 2, 5, 6])
np.in1d(values, [2, 3, 6])
array([ True, False, False, True, True, False, True], dtype=bool)
arr22 = np.arange(10)
arr22
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.save('some_array', arr22)
np.load('some_array.npy')
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.savez('array_achieve.npz', a = arr22, b = arr22)
arch = np.load('array_achieve.npz')
arch['b']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
!rm some_array.npy
!rm array_achieve.npz
x = np.array([[1., 2., 3.], [4., 5., 6.]])
x
array([[ 1., 2., 3.], [ 4., 5., 6.]])
y = np.array([[6., 23.], [-1, 7], [8, 9]])
y
array([[ 6., 23.], [ -1., 7.], [ 8., 9.]])
x.dot(y)
array([[ 28., 64.], [ 67., 181.]])
np.dot(x, np.ones(3))
array([ 6., 15.])
np.random.seed(12345)
from numpy.linalg import inv, qr
X = randn(5, 5)
X
array([[-0.2047, 0.4789, -0.5194, -0.5557, 1.9658], [ 1.3934, 0.0929, 0.2817, 0.769 , 1.2464], [ 1.0072, -1.2962, 0.275 , 0.2289, 1.3529], [ 0.8864, -2.0016, -0.3718, 1.669 , -0.4386], [-0.5397, 0.477 , 3.2489, -1.0212, -0.5771]])
mat = X.T.dot(X)
mat
array([[ 4.075 , -3.3059, -1.3073, 3.4466, 2.6197], [ -3.3059, 6.1523, 1.7149, -4.3193, -0.0938], [ -1.3073, 1.7149, 11.1187, -3.3702, -2.0097], [ 3.4466, -4.3193, -3.3702, 4.7812, 0.0331], [ 2.6197, -0.0938, -2.0097, 0.0331, 7.7736]])
inv(mat)
array([[ 3.0361, -0.1808, -0.6878, -2.8285, -1.1911], [-0.1808, 0.5035, 0.1215, 0.6702, 0.0956], [-0.6878, 0.1215, 0.2904, 0.8081, 0.3049], [-2.8285, 0.6702, 0.8081, 3.4152, 1.1557], [-1.1911, 0.0956, 0.3049, 1.1557, 0.6051]])
mat.dot(inv(mat))
array([[ 1., -0., -0., 0., -0.], [ 0., 1., 0., -0., 0.], [-0., -0., 1., 0., -0.], [ 0., -0., -0., 1., 0.], [-0., -0., 0., 0., 1.]])
q, r = qr(mat)
q
array([[-0.5883, -0.2619, -0.2013, -0.3233, -0.6635], [ 0.4772, -0.6609, 0.1734, -0.5501, 0.0532], [ 0.1887, -0.0806, -0.9635, -0.0266, 0.1698], [-0.4975, 0.1618, 0.0179, -0.5581, 0.6438], [-0.3782, -0.6797, 0.0276, 0.5298, 0.3371]])
r
array([[ -6.9271, 7.389 , 6.1227, -7.1163, -4.9215], [ 0. , -3.9735, -0.8671, 2.9747, -5.7402], [ 0. , 0. , -10.2681, 1.8909, 1.6079], [ 0. , 0. , 0. , -1.2996, 3.3577], [ 0. , 0. , 0. , 0. , 0.5571]])
samples = np.random.normal(size = (4, 4))
samples
array([[ 0.1241, 0.3026, 0.5238, 0.0009], [ 1.3438, -0.7135, -0.8312, -2.3702], [-1.8608, -0.8608, 0.5601, -1.2659], [ 0.1198, -1.0635, 0.3329, -2.3594]])
from random import normalvariate
N = 1000000
%timeit samples = [normalvariate(0, 1) for _ in range(N)]
1 loop, best of 3: 876 ms per loop
%timeit np.random.normal(size = N)
10 loops, best of 3: 34.3 ms per loop
import random
position = 0
walk = [position]
steps = 1000
for i in range(steps):
step = 1 if random.randint(0, 1) else -1
position += step
walk.append(position)
np.random.seed(123445)
nsteps = 1000
draws = np.random.randint(0, 2, size = nsteps)
steps = np.where(draws > 0, 1, -1)
walk = steps.cumsum()
walk.min()
-25
walk.max()
13
(np.abs(walk) >= 10).argmax()
27
nwalks = 5000
nsteps = 1000
draws = np.random.randint(0, 2, size = (nwalks, nsteps))
steps = np.where(draws > 0, 1, -1)
walks = steps.cumsum(1)
walks
array([[ -1, -2, -1, ..., 38, 39, 38], [ -1, -2, -1, ..., 50, 51, 50], [ -1, 0, -1, ..., -8, -7, -8], ..., [ -1, -2, -3, ..., -100, -99, -98], [ -1, 0, -1, ..., -16, -17, -16], [ 1, 0, -1, ..., -10, -11, -12]])
walks.max()
119
walks.min()
-118
hits30 = (np.abs(walks) >= 30).any(1)
hits30
array([ True, True, False, ..., True, False, False], dtype=bool)
hits30.sum()
3407
crossing_times = (np.abs(walks[hits30]) >= 30).argmax(1)
crossing_times.mean()
503.94511300264162
steps = np.random.normal(loc=0, scale=0.25,
size=(nwalks, nsteps))
steps
array([[ 0.0576, 0.4903, -0.0204, ..., 0.036 , 0.016 , 0.5973], [-0.2707, -0.0213, 0.0457, ..., 0.0279, -0.3356, 0.2751], [ 0.4423, -0.17 , 0.3218, ..., -0.1858, -0.2958, 0.0574], ..., [-0.4958, 0.2782, 0.0975, ..., 0.2677, 0.1195, 0.1042], [-0.1086, 0.2598, -0.0773, ..., 0.0493, 0.217 , -0.2588], [ 0.0867, 0.1986, 0.2159, ..., 0.2175, -0.1233, 0.028 ]])