NumPy#

NumPy is a fundamental library for computation in Python.

Additional resources:

import numpy as np

Python list#

Lets’s compare regular Python lists and NumPy arrays.

# A list is created with [.., ..]
myvals = [1.0, 2.0, 1.5]
myvals
[1.0, 2.0, 1.5]
type(myvals)
list

Numpy 1D array (vector)#

myvals_np = np.array([1.2, 3.0, 4.0])
myvals_np
array([1.2, 3. , 4. ])
type(myvals_np)
numpy.ndarray
myvals_np.dtype
dtype('float64')
myvals_np.sum()
np.float64(8.2)

Indexing#

x = np.array([1.0,1.5, 2.0, 5.3]) 
x
array([1. , 1.5, 2. , 5.3])
x[1]
np.float64(1.5)
x[-1]
np.float64(5.3)
x[1] = 2.0 # modify the second value in the array
x
array([1. , 2. , 2. , 5.3])

Slicing#

x[:2]
array([1., 2.])

Inline exercise

  1. Create an array x with three values: 1, 2, 3

  2. What is is the data type of x?

  3. Create a new array: y = x/2

  4. What is the data type of y?`

Math operations#

Python is a general purpose language not designed with numerical computing in mind.

However, NumPy is designed for numerical computing!

[1.2, 4.5] + [2.3, 4.3] # is this the result you expected??
[1.2, 4.5, 2.3, 4.3]
np.array([1.2, 4.5]) + np.array([2.3, 4.3]) 
array([3.5, 8.8])
np.array([1.2, 4.5]) * np.array([2.3, 4.3]) 
array([ 2.76, 19.35])

Note for Matlab users, all operators such as * are element wise

np.array([1.2, 4.5]) @ np.array([2.3, 4.3]) # in case you actually wanted to do a dot product
np.float64(22.11)
x = np.arange(5, 100, 5)
x
array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85,
       90, 95])
x.dtype # Integers!
dtype('int64')
x + 1 # add 1!
array([ 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86,
       91, 96])
x = x + 3.0 # add a float to some integers, can we do that?
x
array([ 8., 13., 18., 23., 28., 33., 38., 43., 48., 53., 58., 63., 68.,
       73., 78., 83., 88., 93., 98.])
x.dtype # but now it became floats!
dtype('float64')
xr = np.random.random(10)
xr
array([0.12457626, 0.58381729, 0.83895136, 0.72132442, 0.52873704,
       0.06572816, 0.41519268, 0.74489806, 0.11709301, 0.69287032])
xr.mean()
np.float64(0.48331885862720314)
xr.std()
np.float64(0.27376269806953873)
xr.max()
np.float64(0.8389513566036343)
xr - xr.mean()
array([-0.3587426 ,  0.10049843,  0.3556325 ,  0.23800556,  0.04541818,
       -0.41759069, -0.06812618,  0.2615792 , -0.36622585,  0.20955146])
xn = np.random.normal(loc=5.0, scale=2.0, size=100)
xn[30] = 99.0
mu = xn.mean()
sigma = xn.std()

xn[xn < mu - 3*sigma]
array([], dtype=float64)
xn[xn > mu + 3*sigma]
array([99.])

Missing values (delete values)#

NumPy has support for missing values.

y = np.random.random(10)
y
array([0.18469206, 0.64801549, 0.47538526, 0.31119547, 0.70028253,
       0.88717013, 0.83934785, 0.82073971, 0.93855086, 0.51869641])
y[5:] = np.nan
y
array([0.18469206, 0.64801549, 0.47538526, 0.31119547, 0.70028253,
              nan,        nan,        nan,        nan,        nan])
y.mean()
np.float64(nan)
np.nanmean(y)
np.float64(0.4639141601228823)
y * np.pi
array([0.58022721, 2.03580071, 1.49346683, 0.97764939, 2.20000245,
              nan,        nan,        nan,        nan,        nan])

Boolean indexing#

z = np.random.normal(loc=0.0, scale=3.0, size=10)

z_sorted = np.sort(z)
z_sorted
array([-3.59369688, -2.57322661, -2.22900507, -1.58618696, -1.49035142,
        0.16006616,  0.45573874,  1.19854153,  3.64797153,  5.54873307])
z<0.0
array([ True, False, False,  True,  True,  True, False,  True, False,
       False])
z_sorted<0.0
array([ True,  True,  True,  True,  True, False, False, False, False,
       False])
z_sorted[z_sorted<0.0]
array([-3.59369688, -2.57322661, -2.22900507, -1.58618696, -1.49035142])
z_sorted[z_sorted<0.0] = 0.0
z_sorted
array([0.        , 0.        , 0.        , 0.        , 0.        ,
       0.16006616, 0.45573874, 1.19854153, 3.64797153, 5.54873307])
np.where(z<0.0)
(array([0, 3, 4, 5, 7]),)
xn = np.random.normal(loc=5.0, scale=2.0, size=100)

xn[30] = 99.0 # outlier

median = np.median(xn)
sigma = xn.std()
sigma # sample std affected by outlier
np.float64(9.51168884660138)
xn[xn > median + 3*sigma] # but 1 abnormally high value
array([99.])
xn[xn > median + 3*sigma] = np.nan
np.nanstd(xn) # much closer to the true std==2.0
np.float64(1.8397030015690627)

2D arrays#

X = np.array([
              [0.0, 1.0, 2.0],
              [3.0, 4.0, 5.0]
])
    
X
array([[0., 1., 2.],
       [3., 4., 5.]])
X.shape
(2, 3)
nrows = X.shape[0]
nrows
2
ncols = X.shape[1]
ncols
3
X[0,0]
np.float64(0.0)
X[1,1]
np.float64(4.0)
X[-1,-1]
np.float64(5.0)
X[0,:]
array([0., 1., 2.])
X[0]
array([0., 1., 2.])
X.mean()
np.float64(2.5)
colmeans = X.mean(axis=0)
colmeans
array([1.5, 2.5, 3.5])
colmeans.shape
(3,)
rowmeans = X.mean(axis=1)
rowmeans
array([1., 4.])
X - colmeans
array([[-1.5, -1.5, -1.5],
       [ 1.5,  1.5,  1.5]])
# X - rowmeans    # executing this will fail

NumPy broadcasting (detailed explanation of how arrays can be used in expressions)

R = rowmeans[:, np.newaxis] # add a new dimension to create a 2D array
R
array([[1.],
       [4.]])
np.expand_dims(rowmeans, 1) # same result
array([[1.],
       [4.]])
X.shape
(2, 3)
R.shape
(2, 1)
X - R
array([[-1.,  0.,  1.],
       [-1.,  0.,  1.]])

Reshaping#

x = X.flatten()
x
array([0., 1., 2., 3., 4., 5.])
x.reshape(2,3)
array([[0., 1., 2.],
       [3., 4., 5.]])