Skip to content

Linear Algebra with NumPy

Why linear algebra is useful

Linear algebra appears in:

  • Regression
  • Optimization
  • Dimensionality reduction
  • Correlation and covariance

NumPy provides numpy.linalgnumpy.linalg for common operations like decompositions, inverses, and determinants.

Dot product

dot
import numpy as np
 
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
 
print(np.dot(a, b))  # 1*4 + 2*5 + 3*6
dot
import numpy as np
 
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
 
print(np.dot(a, b))  # 1*4 + 2*5 + 3*6

Matrix multiplication

** on two 2D arrays is element-wise. Matrix multiplication needs @@ or np.matmulnp.matmul (equivalently np.dotnp.dot for 2D arrays).

matmul
import numpy as np
 
A = np.array([[1, 2], [3, 4]])
B = np.array([[10, 20], [30, 40]])
 
print(A @ B)
print(np.matmul(A, B))
matmul
import numpy as np
 
A = np.array([[1, 2], [3, 4]])
B = np.array([[10, 20], [30, 40]])
 
print(A @ B)
print(np.matmul(A, B))
diagram Linear algebra toolbox mermaid
numpy.linalg covers the standard matrix operations you need for regression, PCA, and solving systems.

Transpose

transpose
import numpy as np
 
A = np.array([[1, 2], [3, 4]])
print(A.T)
transpose
import numpy as np
 
A = np.array([[1, 2], [3, 4]])
print(A.T)

Determinant and inverse

det-inv
import numpy as np
 
A = np.array([[1, 2], [3, 4]])
 
det = np.linalg.det(A)
inv = np.linalg.inv(A)
 
print("det:", det)
print("inv:\n", inv)
det-inv
import numpy as np
 
A = np.array([[1, 2], [3, 4]])
 
det = np.linalg.det(A)
inv = np.linalg.inv(A)
 
print("det:", det)
print("inv:\n", inv)

Solve a system of equations

Solving A x = bA x = b directly with np.linalg.solvenp.linalg.solve is faster and more numerically stable than computing inv(A) @ binv(A) @ b.

solve
import numpy as np
 
A = np.array([[2, 1], [1, 3]])
b = np.array([8, 13])
 
x = np.linalg.solve(A, b)
print(x)
solve
import numpy as np
 
A = np.array([[2, 1], [1, 3]])
b = np.array([8, 13])
 
x = np.linalg.solve(A, b)
print(x)

Eigenvalues and eigenvectors

eig
import numpy as np
 
A = np.array([[2, 0], [0, 3]])
vals, vecs = np.linalg.eig(A)
 
print("eigenvalues:", vals)
print("eigenvectors:\n", vecs)
eig
import numpy as np
 
A = np.array([[2, 0], [0, 3]])
vals, vecs = np.linalg.eig(A)
 
print("eigenvalues:", vals)
print("eigenvectors:\n", vecs)

Norms (vector length)

norm
import numpy as np
 
v = np.array([3, 4])
print(np.linalg.norm(v))  # 5
norm
import numpy as np
 
v = np.array([3, 4])
print(np.linalg.norm(v))  # 5

Next

Continue to: Statistical Functions in NumPy for mean/median/std/percentiles and basic descriptive analytics.

🧪 Try It Yourself

Exercise 1 – Dot Product

Exercise 2 – Matrix Multiplication with @

Exercise 3 – Solve a Linear System

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did