Python Central HubSign in

Data Analytics with Python / Phase 2: Numerical Computing NumPy

Linear Algebra with NumPy

Learn dot products, matrix multiplication, determinants, inverses, eigenvalues, and solving linear systems using numpy.linalg.

Why linear algebra is useful

Linear algebra appears in:

  • Regression
  • Optimization
  • Dimensionality reduction
  • Correlation and covariance

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

Dot product

Python
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.matmul (equivalently np.dot for 2D arrays).

Python
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

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

Determinant and inverse

Python
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 = b directly with np.linalg.solve is faster and more numerically stable than computing inv(A) @ b.

Python
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

Python
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)

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

Check your understanding

quizCheck yourself
  1. What does A * B do for two 2D arrays?

    Show answer

    B โ€” Element-wise multiplication โ€” The * operator is element-wise. Matrix multiplication needs @ or np.matmul.

  2. What does np.dot(np.array([1, 2, 3]), np.array([4, 5, 6])) return?

    Show answer

    C โ€” 32 โ€” The dot product is 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32.

  3. To solve A x = b, which approach does the lesson recommend?

    Show answer

    B โ€” np.linalg.solve(A, b) โ€” np.linalg.solve is faster and more numerically stable than computing the inverse, which can be unstable.

  4. What does np.linalg.norm(np.array([3, 4])) return?

    Show answer

    C โ€” 5.0 โ€” The norm is the vector length, the square root of 3 squared plus 4 squared, which is 5.

Next

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

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Dot Product

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Matrix Multiplication with @

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Solve a Linear System

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did