NumPy

In this lesson we will learn the basics of numerical analysis using the NumPy package.

NumPy basics

import numpy as np
# Set seed for reproducibility
np.random.seed(seed=1234)
# Scalars
x = np.array(6) # scalar
print ("x: ", x)
# Number of dimensions
print ("x ndim: ", x.ndim)
# Dimensions
print ("x shape:", x.shape)
# Size of elements
print ("x size: ", x.size)
# Data type
print ("x dtype: ", x.dtype)
x:  6
x ndim:  0
x shape: ()
x size:  1
x dtype:  int64
# 1-D Array
x = np.array([1.3 , 2.2 , 1.7])
print ("x: ", x)
print ("x ndim: ", x.ndim)
print ("x shape:", x.shape)
print ("x size: ", x.size)
print ("x dtype: ", x.dtype) # notice the float datatype
x:  [1.3 2.2 1.7]
x ndim:  1
x shape: (3,)
x size:  3
x dtype:  float64
# 3-D array (matrix)
x = np.array([[[1,2,3], [4,5,6], [7,8,9]]])
print ("x:\n", x)
print ("x ndim: ", x.ndim)
print ("x shape:", x.shape)
print ("x size: ", x.size)
print ("x dtype: ", x.dtype)
x:
 [[[1 2 3]
  [4 5 6]
  [7 8 9]]]
x ndim:  3
x shape: (1, 3, 3)
x size:  9
x dtype:  int64
# Functions
print ("np.zeros((2,2)):\n", np.zeros((2,2)))
print ("np.ones((2,2)):\n", np.ones((2,2)))
print ("np.eye((2)):\n", np.eye((2)))
print ("np.random.random((2,2)):\n", np.random.random((2,2)))
np.zeros((2,2)):
 [[0. 0.]
 [0. 0.]]
np.ones((2,2)):
 [[1. 1.]
 [1. 1.]]
np.eye((2)):
 [[1. 0.]
 [0. 1.]]
np.random.random((2,2)):
 [[0.77997581 0.27259261]
 [0.27646426 0.80187218]]

Indexing

# Indexing
x = np.array([1, 2, 3])
print ("x[0]: ", x[0])
x[0] = 0
print ("x: ", x)
x[0]:  1
x:  [0 2 3]
# Slicing
x = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print (x)
print(x.ndim)
print ("x column 1: ", x[:, 1]) 
print ("x row 0: ", x[0, :].shape) 
print ("x rows 0,1,2 & cols 1,2: \n", x[:3, 1:3]) 
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
2
x column 1:  [ 2  6 10]
x row 0:  (4,)
x rows 0,1,2 & cols 1,2: 
 [[ 2  3]
 [ 6  7]
 [10 11]]
np.arange(5)
array([0, 1, 2, 3, 4])
# Integer array indexing
print (x)
rows_to_get = np.arange(len(x))
print ("rows_to_get: ", rows_to_get)
cols_to_get = np.array([0, 2, 1])
print ("cols_to_get: ", cols_to_get)
print ("indexed values: ", x[rows_to_get, cols_to_get])
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
rows_to_get:  [0 1 2]
cols_to_get:  [0 2 1]
indexed values:  [ 1  7 10]
# Boolean array indexing
x = np.array([[1,2], [3, 4], [5, 6]])
print ("x:\n", x)
print ("x > 2:\n", x > 2)
print ("x[x > 2]:\n", x[x > 2])
x:
 [[1 2]
 [3 4]
 [5 6]]
x > 2:
 [[False False]
 [ True  True]
 [ True  True]]
x[x > 2]:
 [3 4 5 6]

Array math

# Basic math
x = np.array([[1,2], [3,4]], dtype=np.float64)
y = np.array([[1,2], [3,4]], dtype=np.float64)
print ("x + y:\n", np.add(x, y)) # or x + y
print ("x - y:\n", np.subtract(x, y)) # or x - y
print ("x * y:\n", np.multiply(x, y)) # or x * y
x + y:
 [[2. 4.]
 [6. 8.]]
x - y:
 [[0. 0.]
 [0. 0.]]
x * y:
 [[ 1.  4.]
 [ 9. 16.]]

# Dot product
a = np.array([[1,2,3], [4,5,6]], dtype=np.float64) # we can specify dtype
b = np.array([[7,8], [9,10], [11, 12]], dtype=np.float64)
print (a.dot(b))
[[ 58.  64.]
 [139. 154.]]
# Sum across a dimension
x = np.array([[1,2],[3,4]])
print (x)
print ("sum all: ", np.sum(x)) # adds all elements
print ("sum by col: ", np.sum(x, axis=0)) # add numbers in each column
print ("sum by row: ", np.sum(x, axis=1)) # add numbers in each row
[[1 2]
 [3 4]]
sum all:  10
sum by col:  [4 6]
sum by row:  [3 7]
# Transposing
print ("x:\n", x)
print ("x.T:\n", x.T)
x:
 [[1 2]
 [3 4]]
x.T:
 [[1 3]
 [2 4]]

Advanced

# Tile
x = np.array([[1,2], [3,4]])
y = np.array([5, 6])
addent = np.tile(y, (len(x), 1))
print ("addent: \n", addent)
z = x + addent
print ("z:\n", z)
addent: 
 [[5 6]
 [5 6]]
z:
 [[ 6  8]
 [ 8 10]]
# Broadcasting
x = np.array([[1,2], [3,4]])
y = np.array([5, 6])
z = x + y
print ("z:\n", z)
z:
 [[ 6  8]
 [ 8 10]]
# Reshaping
x = np.array([[1,2], [3,4], [5,6]])
print (x)
print ("x.shape: ", x.shape)
y = np.reshape(x, (2, 3))
print ("y.shape: ", y.shape)
print ("y: \n", y)
[[1 2]
 [3 4]
 [5 6]]
x.shape:  (3, 2)
y.shape:  (2, 3)
y: 
 [[1 2 3]
 [4 5 6]]
# Removing dimensions
x = np.array([[[1,2,1]],[[2,2,3]]])
print ("x.shape: ", x.shape)
y = np.squeeze(x, 1) # squeeze dim 1
print ("y.shape: ", y.shape) 
print ("y: \n", y)
x.shape:  (2, 1, 3)
y.shape:  (2, 3)
y: 
 [[1 2 1]
 [2 2 3]]
# Adding dimensions
x = np.array([[1,2,1],[2,2,3]])
print ("x.shape: ", x.shape)
y = np.expand_dims(x, 1) # expand dim 1
print ("y.shape: ", y.shape) 
print ("y: \n", y)
x.shape:  (2, 3)
y.shape:  (2, 1, 3)
y: 
 [[[1 2 1]]

 [[2 2 3]]]

Additional resources

You don't have to memorize anything here and we will be taking a closer look at NumPy in the later lessons. If you are curious about more checkout the NumPy reference manual.