0Pricing
Learn AI with Python · Lesson

Linear Algebra with NumPy

np.dot, np.linalg.inv, np.linalg.eig, np.linalg.solve for systems of equations.

Linear Algebra with NumPy is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Linear Algebra in NumPy

NumPy provides matrix operations through the @ operator and the np.linalg module, the foundation of nearly every machine learning algorithm.

Matrix Multiplication with @

The @ operator performs true matrix multiplication. Inner dimensions must match: (m,k) @ (k,n) gives (m,n).

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)
# [[19 22]
#  [43 50]]

@ vs * (Important!)

* is element-wise, @ is matrix multiply. Confusing them is a classic bug.

print(A * B)   # element-wise: [[ 5 12]
               #               [21 32]]
print(A @ B)   # matrix product (different result)

np.dot

np.dot is the function form. For 2D arrays it equals @; for 1D arrays it computes the dot product (a scalar).

u = np.array([1, 2, 3])
v = np.array([4, 5, 6])
print(np.dot(u, v))   # 1*4 + 2*5 + 3*6 = 32

Matrix Inverse

np.linalg.inv(A) returns the inverse, the matrix that yields the identity when multiplied by A. Only square, non-singular matrices have one.

A = np.array([[4.0, 7.0], [2.0, 6.0]])
Ainv = np.linalg.inv(A)
print(np.round(A @ Ainv, 6))   # identity

Determinant

np.linalg.det(A) gives the determinant. A determinant of 0 means the matrix is singular and cannot be inverted.

print(np.linalg.det(A))   # 10.0  (nonzero -> invertible)

Solving Linear Systems

To solve A x = b, prefer np.linalg.solve(A, b) over computing the inverse. It is faster and more numerically stable.

A = np.array([[3.0, 1.0], [1.0, 2.0]])
b = np.array([9.0, 8.0])
x = np.linalg.solve(A, b)
print(x)   # [2. 3.]

Why solve over inv

Computing inv(A) @ b wastes work and amplifies rounding error. solve uses LU decomposition directly. Reach for solve in real code.

# Avoid:  x = np.linalg.inv(A) @ b
# Prefer: x = np.linalg.solve(A, b)

Eigenvalues and Eigenvectors

np.linalg.eig(A) returns eigenvalues and eigenvectors. These underpin PCA, stability analysis, and more.

A = np.array([[2.0, 0.0], [0.0, 3.0]])
vals, vecs = np.linalg.eig(A)
print(vals)   # [2. 3.]

Vector and Matrix Norms

np.linalg.norm measures magnitude. By default it is the Euclidean (L2) norm for vectors, used everywhere from distances to regularization.

v = np.array([3.0, 4.0])
print(np.linalg.norm(v))        # 5.0 (sqrt(9+16))
print(np.linalg.norm(v, 1))    # 7.0 (L1 norm)

Transpose

A.T transposes a matrix, swapping rows and columns. It appears constantly in formulas like the normal equations.

A = np.array([[1, 2, 3], [4, 5, 6]])
print(A.T)
# [[1 4]
#  [2 5]
#  [3 6]]

Quick Check

Test your linear algebra recall.

Recap

Linear algebra toolkit:

  • @ / np.dot for matrix and dot products (not *)
  • np.linalg.inv, np.linalg.det
  • np.linalg.solve(A, b) beats inverting
  • np.linalg.eig for eigen-decomposition
  • np.linalg.norm for magnitudes, A.T to transpose

Frequently asked questions

Is the “Linear Algebra with NumPy” lesson free?

Yes — the full text of “Linear Algebra with NumPy” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Linear Algebra with NumPy”?

np.dot, np.linalg.inv, np.linalg.eig, np.linalg.solve for systems of equations. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Linear Algebra with NumPy” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Array Creation and Properties
  2. Indexing, Slicing, and Fancy Indexing
  3. Broadcasting and Vectorized Operations
  4. Linear Algebra with NumPy
← Back to Learn AI with Python