Numpy

11 minute read

Numpy is python’s linear algebra library.
To install it use the following command in command prompt:

pip install numpy

In anaconda distribution:

conda install numpy

Creating numpy arrays

import numpy as np
lst = [1,2,3]
np.array(lst)
    array([1, 2, 3])
lst = [[1,2,3],[4,5,6]]
np.array(lst)
    array([[1, 2, 3],
           [4, 5, 6]])

Built in methods

#arange(start,stop,step)
np.arange(0,10,2)
    array([0, 2, 4, 6, 8])
np.arange(0,10)
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#ones(shape tuple)
np.ones((3,3))
    array([[1., 1., 1.],
           [1., 1., 1.],
           [1., 1., 1.]])
#linspace(start,stop,number of evenly spaced numbers)
np.linspace(0,10,3)
    array([ 0.,  5., 10.])
np.linspace(0,10,10)
    array([ 0.        ,  1.11111111,  2.22222222,  3.33333333,  4.44444444,
            5.55555556,  6.66666667,  7.77777778,  8.88888889, 10.        ])
#4x4 Identity matrix
np.eye(4,4)
    array([[1., 0., 0., 0.],
           [0., 1., 0., 0.],
           [0., 0., 1., 0.],
           [0., 0., 0., 1.]])
arr = np.arange(0,25)
#Get the shape of array
arr.shape
    (25,)
#reshaping an array
arr.reshape(5,5)
    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]])
arr
    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])
arr.max()
    24
arr.min()
    0
arr.mean()
    12.0
arr.std()
    7.211102550927978
arr.var()
    52.0
#returns the index of maximum value
arr.argmax()
    24
#returns the index of minimum value
arr.argmin()
    0

Generating random arrays

#Generating random numbers from uniform distribution
np.random.rand(2,2)
    array([[0.48353285, 0.57472306],
           [0.24742253, 0.40119928]])
np.random.rand(5,5)
    array([[0.71716514, 0.89992773, 0.49219231, 0.03280521, 0.25671464],
           [0.74452436, 0.34535279, 0.34108729, 0.69509882, 0.38784086],
           [0.17217316, 0.97134788, 0.90350981, 0.57780894, 0.50682227],
           [0.27788103, 0.22697458, 0.34995732, 0.42797256, 0.95632152],
           [0.68261078, 0.3899289 , 0.06753088, 0.74181803, 0.95712323]])
#Generating random numbers from standard normal distribution
np.random.randn(2,2)
    array([[0.71758331, 1.28048375],
           [1.5364017 , 0.77660221]])
#randint(low,high,size tuple)
np.random.randint(0,100,(2,2))
    array([[20, 18],
           [73, 48]])

Numpy indexing and selection

arr = np.random.randint(0,100,(5,5))
arr
    array([[28, 73, 47, 91, 95],
           [15, 82, 81,  1, 81],
           [ 7,  5, 67, 57,  1],
           [60, 49,  1, 41, 53],
           [60, 19, 74, 37,  0]])
#Getting first number
arr[0,0]
    28
#Getting first two rows
arr[0:2,0:]
    array([[28, 73, 47, 91, 95],
           [15, 82, 81,  1, 81]])
#Getting last two columns from last two rows
arr[3:,3:]
    array([[41, 53],
           [37,  0]])
#Getting the 2nd row and the last row
arr[[1,-1]]
    array([[15, 82, 81,  1, 81],
           [60, 19, 74, 37,  0]])
#Getting numbers greater than 50 from our array
arr[arr>50]
    array([73, 91, 95, 82, 81, 81, 67, 57, 60, 53, 60, 74])

Broadcasting

arr = np.arange(0,10)
arr
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[0:5] = 100
arr
    array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9])

Copy of array

arr = np.arange(0,10)
arr
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr_copy = arr.copy()
arr_copy
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Airthmetic operations

arr = np.arange(0,10)
arr
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr + arr
    array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
arr - arr
    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
arr * arr
    array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])
arr / arr
    array([nan,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
arr/3
    array([0.        , 0.33333333, 0.66666667, 1.        , 1.33333333,
           1.66666667, 2.        , 2.33333333, 2.66666667, 3.        ])
3/arr
    array([       inf, 3.        , 1.5       , 1.        , 0.75      ,
           0.6       , 0.5       , 0.42857143, 0.375     , 0.33333333])
arr ** 2
    array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81], dtype=int32)
#Taking square root
np.sqrt(arr)
    array([0.        , 1.        , 1.41421356, 1.73205081, 2.        ,
           2.23606798, 2.44948974, 2.64575131, 2.82842712, 3.        ])
np.log()
    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])

Leave a comment