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.
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.
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
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.
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.
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