# 🧮 Cheatsheet: Mathematics for Machine Learning 1: Linear Algebra Foundations

By Better Wise

> This cheatsheet is premium content. Sign in and purchase the course to view the full content.

One-page lookup for vectors, matrices, projections, eigen and SVD, with every operation in NumPy.

```python
import numpy as np
```

---

## Module 1: Vectors, Norms and Similarity

### 1.1 Vectors as Arrows, Points, and NumPy Arrays

A vector is the same object seen three ways: an **arrow** (direction + length), a **point** (coordinates), and a **1D array** (data).

| View | Meaning | NumPy |
| --- | --- | --- |
| Arrow | Direction and magnitude from origin | `v = np.array([3, 4])` |
| Point | A location in space | `v[0], v[1]` are coordinates |
| Array | Ordered list of numbers | `v.shape  # (2,)` |

```python
v = np.array([3, 4])
u = np.array([1, 2])
v + u            # elementwise add (tip-to-tail)
v - u            # difference vector
2 * v            # scalar scaling (stretch)
np.linalg.norm(v)  # length
```

- **Tip-to-tail**: `v + u` places `u` at the tip of `v`.
- **Scalar multiple** `c * v`: same direction if `c > 0`, flipped if `c < 0`, zero if `c = 0`.
- **Dimension** = `v.shape[0]`. Two vectors add only if dimensions match.

### 1.2 Norms and Distances

Norm = "size" of a vector. Distance = norm of the difference.

| Norm | Formula | NumPy | Shape it measures |
| --- | --- | --- | --- |
| L2 (Euclidean) | $\sqrt{\sum v_i^2}$ | `np.linalg.norm(v)` | Straight-line length |
| L1 (Manhattan) | $\sum \lvert v_i \rvert$ | `np.linalg.norm(v, 1)` | Grid / city-block |
| L-infinity | $\max \lvert v_i \rvert$ | `np.linalg.norm(v, np.inf)` | Largest component |

$$
\|v\|_2 = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}
$$

| Distance | Formula | NumPy |
| --- | --- | --- |
| Euclidean | $\|u - v\|_2$ | `np.linalg.norm(u - v)` |
| Manhattan | $\|u - v\|_1$ | `np.linalg.norm(u - v, 1)` |

**Unit vector** (length 1): `v / np.linalg.norm(v)`. Normalizing puts every vector on the unit circle/sphere.

### 1.3 Dot Product and Cosine Similarity

$$
u \cdot v = \sum_i u_i v_i = \|u\|\,\|v\|\cos\theta
$$

```python
np.dot(u, v)      # or u @ v
```
