NumPy#
Overview#
In this class, you will be writing algorithms with linear algebra operations. Use NumPy to do this efficiently. The NumPy package in Python is a wrapper for a parallelized, internally optimized implementation of common matrix operations in C++. Vectorizing your code in NumPy will improve your code’s speed, performance, and elegance.
Code Examples#
Use the following section as documentation for common NumPy operations that you will use in this class.
Create a Matrix in NumPy#
import numpy as np
# Create the array:
# [[1, 2, 3]
# [4, 5, 6]]
a = np.array([[1, 2, 3], [4, 5, 6]])
Calculate the Shape of a Matrix in NumPy#
Consider the same array as in the example above. The shape of a
can be calculated with a.shape
, which returns (2, 3)
.
Size of a Matrix in NumPy#
The size of a matrix represents the total number of elements in the array. For the same array a
, the size can be calculated with a.size
, which returns 6
because the matrix has 2 rows and 3 columns.
Example: Shape and Size of a Matrix#
import numpy as np
# Define the matrix
a = np.array([[1, 2, 3], [4, 5, 6]])
# Get the shape and size
shape = a.shape # Output: (2, 3)
size = a.size # Output: 6
print(f"Shape: {shape}")
print(f"Size: {size}")
Converting Row Vector to Column Vector#
import numpy as np
A = np.array([1, 2, 3, 4])
B = A.reshape(-1, 1)
# A = [1, 2, 3, 4]
# B = [[1],
# [2],
# [3],
# [4]]
Matrix Multiplication#
We can use either the infix (@
) or the function (np.matmul
) operator for matrix multiplication. The infix operator is often preferred for its simplicity.
Infix:
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[1, 2], [3, 4], [5, 6]])
C = A @ B
# C = [[22, 28],
# [49, 64]]
Function (np.matmul
):
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[1, 2], [3, 4], [5, 6]])
C = np.matmul(A, B)
# C = [[22, 28],
# [49, 64]]
Remember that the order of matrix multiplication matters: the number of columns of the first matrix must match the number of rows of the second matrix.
Element-wise Multiplication#
Not to be confused with matrix multiplication, np.multiply
(or *
operator) multiplies two arrays element-wise. The arrays must have the same dimensions, or broadcasting rules must apply.
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[1, 2], [3, 4]])
C = A * B # Can also use np.multiply(A, B)
# C = [[1, 4],
# [9, 16]]
You can also multiply an array by a scalar:
import numpy as np
A = np.array([[1, 2], [3, 4]])
A *= 3
# A = [[3, 6],
# [9, 12]]
Element-wise Division#
Similarly to np.multiply
, np.divide
(or /
operator) divides two arrays element-wise.
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[1, 2], [3, 4]])
C = A / B # Can also use np.divide(A, B)
# C = [[1., 1.],
# [1., 1.]]
You can also divide an array by a scalar:
import numpy as np
A = np.array([[1, 2], [3, 4]])
A = A / 3
# A = [[0.33333333, 0.66666667],
# [1.0, 1.33333333]]
Stacking Matrices#
NumPy allows you to stack arrays vertically or horizontally using np.vstack
and np.hstack
.
Vertical Stacking:
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8, 9]])
C = np.vstack((A, B))
# C = [[1, 2, 3],
# [4, 5, 6],
# [7, 8, 9]]
Horizontal Stacking:
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
C = np.hstack((A, B))
# C = [[1, 2, 5, 6],
# [3, 4, 7, 8]]
Vectorization and Broadcasting#
Vectorization allows you to apply operations on entire arrays without using explicit loops, making your code more concise and faster.
Broadcasting automatically expands arrays of different shapes to make their dimensions compatible for element-wise operations. For more details, refer to the NumPy broadcasting rules.
Broadcasting Rules (Simplified):
If arrays have a different number of dimensions, the shape of the smaller array is padded with ones on the left.
Arrays are compatible for broadcasting if, in all dimensions, their sizes are either equal or one of them is 1.
Broadcasting proceeds from the trailing dimensions (i.e., starting from the last dimension).
Example of Vectorization:
import numpy as np
A = np.array([1, 2, 3, 4, 5])
B = A ** 2 # Squaring each element
# B = [1, 4, 9, 16, 25]
Example of Broadcasting:
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([1, 0, 1])
C = A + B # B is broadcasted to match A's shape
# C = [[2, 2, 4],
# [5, 5, 7]]
Broadcasting simplifies arithmetic operations between arrays of different shapes and is a fundamental feature for efficient numerical computations in NumPy.
NumPy Documentation#
It is often useful to refer to the official NumPy documentation when unsure about which functions to use. For NumPy beginners, the beginner guide is quite helpful, and the user guide is a great resource for documentation about specific NumPy functions.
Understanding Function Details#
The documentation will detail the function’s parameters, return types, and possible errors. For example, the dot
function takes two NumPy array parameters and returns their dot product. It may throw a ValueError
if the dimensions of the input arrays are incompatible.
The documentation may also include:
Links to related functions: Useful if you need variations of a function.
Notes: Additional explanations or special cases.
Examples: Practical code snippets that demonstrate how to use the function.
By exploring these resources, you can efficiently learn and implement NumPy functions in your code.