Numpy Tutorial

## import numpy
import numpy as np
my_lst = [1, 2, 3]
my_lst
[1, 2, 3]
np.array(my_lst)
array([1, 2, 3])
my_matrix = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
my_matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
np.array(my_matrix)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Built-ins

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

Zeros and ones

np.zeros(3)
array([0., 0., 0.])
np.zeros((3, 3))
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
np.ones(3)
array([1., 1., 1.])
np.ones((3, 3))
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

linspace

np.linspace(0, 10, 3)
array([ 0.,  5., 10.])
np.linspace(0,5,20)
array([0.        , 0.26315789, 0.52631579, 0.78947368, 1.05263158,
       1.31578947, 1.57894737, 1.84210526, 2.10526316, 2.36842105,
       2.63157895, 2.89473684, 3.15789474, 3.42105263, 3.68421053,
       3.94736842, 4.21052632, 4.47368421, 4.73684211, 5.        ])

eye

np.eye(4)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])

Random

rand
np.random.rand(2)
array([0.40238536, 0.06656609])
np.random.rand(5, 5)
array([[0.12254725, 0.348197  , 0.24849521, 0.47882429, 0.22543093],
       [0.97439097, 0.85104882, 0.21975901, 0.64904836, 0.15185813],
       [0.39898   , 0.87820818, 0.04461097, 0.26572141, 0.20420263],
       [0.18067849, 0.62212104, 0.9233101 , 0.91842698, 0.01005861],
       [0.22570714, 0.26014876, 0.72662577, 0.86396834, 0.53274278]])
randn
np.random.randn(2)
array([-0.90371241, -0.79366858])
np.random.randn(5, 5)
array([[ 1.44888877,  0.74329783, -1.38152915, -2.22003905, -2.11033026],
       [ 0.67312385, -0.01857065, -0.70392401, -0.6683721 , -0.51541829],
       [-0.10372972, -0.72184981, -0.19980393, -0.1513105 ,  0.4171869 ],
       [ 0.36287574,  0.92669748,  0.25979809, -0.16400741, -0.80226408],
       [-1.14886478,  1.21948235, -0.0321742 , -0.58590923,  0.3878124 ]])
randint
np.random.randint(1,100)
10
np.random.randint(1,100,10)
array([ 2, 74, 83, 63, 56, 22, 62, 31, 50, 58])

seed

np.random.seed(42)
np.random.rand(4)
array([0.37454012, 0.95071431, 0.73199394, 0.59865848])
np.random.seed(42)
np.random.rand(4)
array([0.37454012, 0.95071431, 0.73199394, 0.59865848])

Array Attributes and Methods

arr = np.arange(25)
ranarr = np.random.randint(0,50,10)
ranarr
array([38, 18, 22, 10, 10, 23, 35, 39, 23,  2])
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])

Reshape

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]])

max, min, argmax, argmin

ranarr
array([38, 18, 22, 10, 10, 23, 35, 39, 23,  2])
ranarr.max()
39
ranarr.min()
2
ranarr.argmax()
7
ranarr.argmin()
9

Shape

arr.shape
(25,)
# Notice the two sets of brackets
arr.reshape(1,25)
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.reshape(1,25).shape
(1, 25)
arr.reshape(25,1)
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.reshape(25,1).shape
(25, 1)

dtype

arr.dtype
dtype('int32')
arr2 = np.array([1.2, 3.4, 5.6])
arr2.dtype
dtype('float64')

Bracket Indexing and Selection

#Get a value at an index
arr[8]
8
#Get values in a range
arr[1:5]
array([1, 2, 3, 4])
#Get values in a range
arr[0:5]
array([0, 1, 2, 3, 4])

Broadcasting

#Setting a value with index range (Broadcasting)
arr[0:5]=100

#Show
arr
array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10,  11,  12,
        13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24])
# Reset array, we'll see why I had to reset in  a moment
arr = np.arange(0,11)

#Show
arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
#Important notes on Slices
slice_of_arr = arr[0:6]

#Show slice
slice_of_arr
array([0, 1, 2, 3, 4, 5])
#Change Slice
slice_of_arr[:]=99

#Show Slice again
slice_of_arr
array([99, 99, 99, 99, 99, 99])
arr
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])
#To get a copy, need to be explicit
arr_copy = arr.copy()

arr_copy
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

Indexing a 2D array (matrices)

arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))

#Show
arr_2d
array([[ 5, 10, 15],
       [20, 25, 30],
       [35, 40, 45]])
#Indexing row
arr_2d[1]
array([20, 25, 30])
#Indexing 2nc column
arr_2d[:,1]
array([10, 25, 40])
# Format is arr_2d[row][col] or arr_2d[row,col]

# Getting individual element value
arr_2d[1][0]
20
# 2D array slicing

#Shape (2,2) from top right corner
arr_2d[:2,1:]
array([[10, 15],
       [25, 30]])
#Shape bottom row
arr_2d[2]
array([35, 40, 45])
#Shape bottom row
arr_2d[2,:]
array([35, 40, 45])

Conditional Selection

arr = np.arange(1,11)
arr
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
arr > 4
array([False, False, False, False,  True,  True,  True,  True,  True,
        True])
bool_arr = arr>4
bool_arr
array([False, False, False, False,  True,  True,  True,  True,  True,
        True])
arr[bool_arr]
array([ 5,  6,  7,  8,  9, 10])
arr[arr>2]
array([ 3,  4,  5,  6,  7,  8,  9, 10])

Arithmetic

arr = np.arange(0, 11)
arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
arr + arr
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20])
arr * arr
array([  0,   1,   4,   9,  16,  25,  36,  49,  64,  81, 100])
arr - arr
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
# This will raise a Warning on division by zero, but not an error!
# It just fills the spot with nan
arr/arr
C:\Users\srmetlakunta\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in true_divide
  """Entry point for launching an IPython kernel.
array([nan,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
# Also a warning (but not an error) relating to infinity
1/arr
C:\Users\srmetlakunta\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in true_divide
  
array([       inf, 1.        , 0.5       , 0.33333333, 0.25      ,
       0.2       , 0.16666667, 0.14285714, 0.125     , 0.11111111,
       0.1       ])
arr**3
array([   0,    1,    8,   27,   64,  125,  216,  343,  512,  729, 1000],
      dtype=int32)

Universal Array Functions

# Taking Square Roots
np.sqrt(arr)
array([0.        , 1.        , 1.41421356, 1.73205081, 2.        ,
       2.23606798, 2.44948974, 2.64575131, 2.82842712, 3.        ,
       3.16227766])
# Calculating exponential (e^)
np.exp(arr)
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, 2.20264658e+04])
# Trigonometric Functions like sine
np.sin(arr)
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849,
       -0.54402111])
# Taking the Natural Logarithm
np.log(arr)
C:\Users\srmetlakunta\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in log
  
array([      -inf, 0.        , 0.69314718, 1.09861229, 1.38629436,
       1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458,
       2.30258509])

Axis Logic

arr_2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
arr_2d
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
arr_2d.sum(axis=0) #(columnwise)
array([15, 18, 21, 24])
arr_2d.sum(axis=1) #(rowwise)
array([10, 26, 42])