← Lesson
BitWithBite
AI & Machine Learning · Quick Reference

Lab: Linear Algebra in NumPy Cheat Sheet

AI & Machine Learning
In one line: 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 Pyth...

Key Ideas

1From 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 ru...
2Creating 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 house...
3Dot 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 ...
4Eigenvalues, One Line. A = np.array([[4, 2], [1, 3]]) eigenvalues, eigenvectors = np.linalg.eig(A) print("Eigenvalues:", eigenvalues) print("Eigenvectors:", eigenvectors) This is genuinely a...
5Lab Exercise. Before moving to Module 3, try this on your own:

Code Examples

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) pr...
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(...
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:...