NumPy-CheatSheet
ipynb10 kBPython · 33 cellsExtra material of IN400
Numpy CheatSheet- By Hadi Hijazi
import numpy as npCreating Matrices with Numpy
# Creating Matrix From Nested Lists
M = np.array([
[1, 2, 3],
[4, 5, 6]
])
# Check dimensions
print(M.shape) # (2, 3)Special Matrices Functions
Identity Matrix
M = np.identity(3) # 3x3 identity matrix
print(M)Zeros Matrix
M = np.zeros((2, 3)) # 2x3 matrix of zeros
print(M)Ones Matrix
M = np.ones((2, 3)) # 2x3 matrix of ones
print(M)Random Matrix
M = np.random.rand(2, 3) # 2x3 matrix of random numbers
print(M)Matrix Operations
Let us create two matrices and :
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
# 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.
Scalar & Matrix Multiplication
Scalar Multiplication
# Multiplying every element by 2
result = 2 * A
print("result = ", result)Useful for scaling features or applying learning rates in ML algorithms.
Matrix Multiplication
# 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: . The inner dimensions must match: .
Transpose & Inverse Operations
Matrix Transpose
# Matrix Transpose
A_transosed = A.T # or np.transpose(A)
print("A_transosed = ", A_transosed)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
# 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.
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).
rank_A = np.linalg.matrix_rank(A)
print(rank_A)Eigen Values & Eigen Vectors
Eigenvectoes () are special vectors that when transformed by matrix A, only change is scale (not direction), by a factor of the eigenvalue .
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.
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:", eigenvectors)