Numpy Linear Algebra Cheatsheet
See linalg library docs
Vectors
import numpy as np
# create a vector
x = np.array([1,2,3])
y = np.array([4,5,6])
# scalar operations
x2 = x * 2
x_plus_one = x + 1
# elementwise operations
p = x + y
q = x - y
r = x / y
# dot product - linear combination of the vectors
dp = np.dot(y,x)
# Hadamard product - elementwise multiplication
hp = x * y
Matrices
import numpy as np
# create a matrix
X = np.array([
[2,3,4],
[1,2,3]
])
Y = np.array([
[0,1,0],
[1,0,0],
])
#see its shape
X.shape
# scalar operations
X * 2
X + 2
# elementwise operations
X + Y
# Transpose
X.T
# multiply
np.dot(X,Y)
# create an identity matrix:
np.eye(2) # 2 x 2
# invert a matrix
np.linalg.inv(X)