Module 2: Math Foundations — Linear Algebra
Lesson 2.5 of 40

Lab: Linear Algebra in NumPy

30 min 📚 Beginner 💻 Hands-On Lab
Section 1

From Theory to Code

Lessons 2.1 through 2.4 covered vectors, matrices, dot products, norms, multiplication, and eigenvalues on paper. This lab puts every one of those ideas into actual running Python code using NumPy — the library that every other tool in this course track (pandas, scikit-learn, PyTorch) is ultimately built on top of.

Open a Python environment (Jupyter notebook, Google Colab, or any local setup) and follow along — typing the code yourself, not just reading it, is genuinely worth the extra few minutes here.

Section 2

Creating Vectors and Matrices

import numpy as np

# A vector — just a 1D array
house = np.array([1800, 3, 2, 15])
print(house.shape)   # (4,) — matches what Lesson 2.1 predicted

# A matrix — 100 houses, 4 features each (using random data here)
houses = np.random.rand(100, 4)
print(houses.shape)  # (100, 4)

Notice that .shape returns exactly the (rows, columns) format from Lesson 2.1's table. Get comfortable checking .shape constantly — it's the single most useful debugging habit in all of NumPy/pandas/PyTorch work, and you'll be doing it in every lab from here forward.

Section 3

Dot Products and Norms in Code

a = np.array([2, 3, 1])
b = np.array([4, 0, 5])

# Dot product — matches Lesson 2.2's worked example exactly
dot_result = np.dot(a, b)
print(dot_result)  # 13

# L2 norm — vector length, from Lesson 2.3
v = np.array([3, 4])
length = np.linalg.norm(v)
print(length)  # 5.0
Run this yourself and confirm the dot product really does equal 13, matching Lesson 2.2's hand-calculated example exactly. This kind of sanity check — verifying code output against a hand calculation you trust — is a genuinely valuable habit for catching bugs early, before they hide inside a more complex model.
Section 4

Matrix Multiplication and the Shape Rule, Live

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

result = A @ b   # @ is Python's matrix multiplication operator
print(result)  # [17 39] — matches Lesson 2.3's worked example

# Now let's deliberately break the shape rule to see the actual error:
bad_a = np.random.rand(3, 4)
bad_b = np.random.rand(3, 4)
# bad_a @ bad_b  -->  raises: ValueError: matmul: Input operand 1 has a
# mismatch in its core dimension... (size 3 is different from 4)

That error message is exactly the "shape mismatch" Lesson 2.3 warned about. Uncomment the last line in your own environment and read the real error — the more times you see this specific message early and understand exactly why it happened, the less time you'll lose to it later when a model's shapes don't line up in a more complex pipeline.

Section 5

Eigenvalues, One Line

A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)

print("Eigenvalues:", eigenvalues)
print("Eigenvectors:", eigenvectors)

This is genuinely all the eigenvalue computation you'll ever write by hand in this course — one function call. What matters, per Lesson 2.4, is recognizing this function name later when it appears inside PCA's implementation in Tier 2.

Section 6

Lab Exercise

Before moving to Module 3, try this on your own:

# 1. Create a vector representing a house: [sqft, bedrooms, bathrooms, age]
# 2. Create a "weights" vector of the same length, with made-up numbers
# 3. Compute their dot product — this is exactly what a trained linear
#    regression model does to produce one prediction (Module 7 will
#    show you how those weights actually get learned, rather than made up)
# 4. Compute the L2 norm of your house vector
# 5. Try creating two matrices with incompatible shapes and multiplying
#    them — read the actual error message NumPy gives you
⚠️
There's no quiz scoring this exercise — it's meant to build hands-on fluency before the math gets layered with calculus in Module 3. If any line above didn't run the way you expected, that's worth pausing on now rather than continuing forward with a shaky foundation.
✅ Quick Check — Lesson 2.5
1. What NumPy operator is used for matrix multiplication?
2. What does checking an array's .shape attribute help you do?
3. Which NumPy function computes eigenvalues and eigenvectors?
🎉 Module 2 complete! Next up, Module 3: Calculus & Optimization — the math behind how models actually learn.
🗒 Cheat Sheet 📝 Worksheet