Vector Operations in Python
Vectors are the language of AI. Word embeddings, image features, and user preferences are all represented as vectors. The three operations you will use most — magnitude, dot product, and cosine similarity — are all single-line calls in NumPy.
Magnitude (The L2 Norm)
The magnitude (or Euclidean norm) of a vector $\mathbf{v}$ is $\|\mathbf{v}\| = \sqrt{\sum v_i^2}$. It measures the "length" of the vector in space. In AI, it's used to measure the size of gradients, distances between points, and for normalizing vectors.
Computing Norms
The Dot Product
The dot product $\mathbf{a} \cdot \mathbf{b} = \sum a_i b_i$ measures how much two vectors point in the same direction. It is positive if they align, zero if perpendicular, negative if opposite. Every neuron in a neural network computes a dot product between its inputs and its weights.
Dot Product in NumPy
Cosine Similarity
Cosine similarity removes the effect of magnitude and measures only the angle between two vectors: $$\cos(\theta) = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{a}\| \|\mathbf{b}\|}$$
It returns a value between -1 (completely opposite) and 1 (identical direction). It is the standard metric for comparing text embeddings — two sentences with similar meaning will have a cosine similarity close to 1.