NumPy-CheatSheet

ipynb10 kBPython · 33 cellsExtra material of IN400

Numpy CheatSheet- By Hadi Hijazi

In [ ]
import numpy as np

Creating Matrices with Numpy

In [ ]
# Creating Matrix From Nested Lists
M = np.array([
    [1, 2, 3],
    [4, 5, 6]
  ])

# Check dimensions
print(M.shape) # (2, 3)
[123456]\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}

Special Matrices Functions

Identity Matrix
In [ ]
M = np.identity(3) # 3x3 identity matrix
print(M)
[100010001]\begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}
Zeros Matrix
In [ ]
M = np.zeros((2, 3)) # 2x3 matrix of zeros
print(M)
[000000]\begin{bmatrix} 0 & 0 & 0 \\ 0 & 0 & 0 \end{bmatrix}
Ones Matrix
In [ ]
M = np.ones((2, 3)) # 2x3 matrix of ones
print(M)
[111111]\begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix}
Random Matrix
In [ ]
M = np.random.rand(2, 3) # 2x3 matrix of random numbers
print(M)
[0.10.20.30.40.50.6]\begin{bmatrix} 0.1 & 0.2 & 0.3 \\ 0.4 & 0.5 & 0.6 \end{bmatrix}

Matrix Operations

Let us create two matrices AA and BB:

In [ ]
A = np.array([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
])

B = np.array([
  [1, 0, 2],
  [3, 2, 4],
  [5, 6, 0]
])

Matrix Addition & Substraction

In [ ]
# Adding matrices
C = A + B
print("C = ", C)

# Substracting matrices
D = A - B
print("D = ", D)

# Broadcasting with scalars
E = A + 5 # Adds 5 to reach element
print("E = ", E)

Matrix addition and substraction are element-wise operations that require the matrices to have the same dimensions.

[123456789]+[102324560]=[225771012149]\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} + \begin{bmatrix} 1 & 0 & 2 \\ 3 & 2 & 4 \\ 5 & 6 & 0 \end{bmatrix} = \begin{bmatrix} 2 & 2 & 5 \\ 7 & 7 & 10 \\ 12 & 14 & 9 \end{bmatrix}

Scalar & Matrix Multiplication

Scalar Multiplication
In [ ]
# Multiplying every element by 2
result = 2 * A
print("result = ", result)
[24681012]\begin{bmatrix} 2 & 4 & 6 \\ 8 & 10 & 12 \end{bmatrix}

Useful for scaling features or applying learning rates in ML algorithms.

Matrix Multiplication
In [ ]
# Matrix multiplication
result = np.dot(A, B) # Classic method
result = A @ B # Modern syntax (Python 3.5+)
print("result = ", result)

Matrix multiplication is not commutative: A@B≠B@AA@B \neq B@A. The inner dimensions must match: (m∗n)∗(n∗p)=(m∗p)(m * n) * (n * p) = (m * p).

Transpose & Inverse Operations

Matrix Transpose
In [ ]
# Matrix Transpose
A_transosed = A.T # or np.transpose(A)
print("A_transosed = ", A_transosed)
[102324567]→[1477258369]\begin{bmatrix} 1 & 0 & 2 \\ 3 & 2 & 4 \\ 5 & 6 & 7 \end{bmatrix} \rightarrow \begin{bmatrix} 1 & 4 & 77 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{bmatrix}

Tranposing flips a matrix along its diagonal, converting rows into columns and columns into rows. In Machine Learning, transposes are frequently used in backpropagation when calculating the gradient of the loss function with respect to the weights of a neural network.

Matrix Inverse
In [ ]
# Compute Inverse of A (if possible)
B_inv = np.linalg.inv(B)
print(B_inv)

# Verify A x A^(-1) = I (Identity Matrix)
result = B @ B_inv
print(result)

A matrix is invertible only if its determinant is non-zero. The inverse is used for solving linear systems and certain optimization problems.

Determinant & Rank

Determinant

The determinant of a square matrix is a scalar value that can be computed from the elements of a square matrix and encodes certain properties of the matrix. It is denoted as det(A) or |A|. A zero determinant indicates that the matrix is singular (non-invertible) and cannot be inverted.

In [ ]
det_A = np.linalg.det(A)
print(det_A)
Matrix Rank

The rank of a matrix is the maximum number of linearly independent rows or columns. It is denoted as rank(A).

In [ ]
rank_A = np.linalg.matrix_rank(A)
print(rank_A)

Eigen Values & Eigen Vectors

Av=λvA \mathbf{v} = \lambda \mathbf{v}

Eigenvectoes (v\mathbf{v}) are special vectors that when transformed by matrix A, only change is scale (not direction), by a factor of the eigenvalue (λ)(\lambda).

In machine learning, eigenvalues and eigenvectors are used in Principal Component Analysis (PCA) for dimensionality reduction, where eigenvectors correspond to the directions of maximum variance in the data, and eigenvalues indicate the amount of variance captured by each principal component.

In [ ]
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:", eigenvectors)