5.17 intro to numpy and arrays

Arrays


In [1]:
my_list = [1,2,3] # list

In [2]:
my_list


Out[2]:
[1, 2, 3]

In [3]:
import numpy as np

In [4]:
np.array(my_list)


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

In [5]:
my_lol = [[1,2,3],[4,5,6],[7,8,9]] # list of lists

In [7]:
np.array(my_lol) # cast lol as 2d array. 2d == 2 levels of brackets


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

In [10]:
np.arange(0,10,2) # (arange == 'array range')


Out[10]:
array([0, 2, 4, 6, 8])

In [20]:
np.zeros(3) # 1d matrix of zeros


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

In [21]:
np.zeros((5,5)) # 2d matrix of zeros, size defined by tuple


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

In [15]:
np.ones((2,4))


Out[15]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])

In [18]:
np.linspace(0,5,10) # 0-5 in ten equal steps. one-dimensional because only one bracket.


Out[18]:
array([ 0.        ,  0.55555556,  1.11111111,  1.66666667,  2.22222222,
        2.77777778,  3.33333333,  3.88888889,  4.44444444,  5.        ])

Identity Matrix


In [25]:
np.eye(6) # identity matrix, square array with diagonal streak of ones; useful in linear algebra


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

Random numbers


In [30]:
np.random.rand(8) # random, evenly distributed values between 0 and 1


Out[30]:
array([ 0.94281769,  0.07352679,  0.67758591,  0.90855607,  0.62023528,
        0.0184851 ,  0.25607745,  0.47223303])

In [29]:
np.random.rand(5,5) # note that this does not take a tuple


Out[29]:
array([[ 0.21296376,  0.34214548,  0.06525983,  0.50073381,  0.00949134],
       [ 0.02751488,  0.74050259,  0.68150845,  0.38250846,  0.53251133],
       [ 0.01213005,  0.20244446,  0.86817604,  0.97044167,  0.37523837],
       [ 0.78798428,  0.51939935,  0.05645766,  0.74499824,  0.7892101 ],
       [ 0.39495495,  0.57984132,  0.27374584,  0.53635004,  0.20940844]])

In [32]:
np.random.randn(5,5) # random, normally-distributed values, distribution centered on 0


Out[32]:
array([[-1.91203663, -0.52787125,  1.36753625, -0.90693989,  0.38105558],
       [ 0.56568902, -1.28475963, -0.53472699,  0.91658542,  0.3049844 ],
       [ 0.27162795, -0.40690948,  1.30689999,  1.4499117 ,  0.39435586],
       [-0.01567394,  0.90361041, -0.94111586, -0.74511823,  1.95083454],
       [ 1.3815438 ,  0.77528303, -1.30183163,  0.13992134, -0.43431779]])

In [60]:
np.random.randint(100) # I think, in py2.7, low is 0 by default, high is input.


Out[60]:
87

In [45]:
np.random.randint(3,9,(3,3)) # random value between low (inclusive) and high (exclusive). eg: 3 <= value < 9


Out[45]:
array([[7, 3, 6],
       [6, 4, 4],
       [4, 3, 8]])

Useful attributes and methods of arrays


In [61]:
arr = np.arange(25)

In [62]:
ranarr = np.random.randint(0,50,10)

In [74]:
arr = arr.reshape(5,5) 
arr


Out[74]:
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]])

In [64]:
ranarr.max()


Out[64]:
49

In [67]:
ranarr.min()


Out[67]:
10

In [68]:
ranarr.argmax()


Out[68]:
4

In [70]:
ranarr.argmin()


Out[70]:
8

In [75]:
arr.shape


Out[75]:
(5, 5)

In [72]:
ranarr.shape


Out[72]:
(10,)

In [77]:
arr.dtype


Out[77]:
dtype('int64')

In [78]:
ranarr.dtype


Out[78]:
dtype('int64')

In [80]:
from numpy.random import randint

In [81]:
randint(2,13)


Out[81]:
8

In [ ]: