Skip to main content
GanitML

Vectors and similarity

Turn things into lists of numbers, measure how far two of them point the same way, and build a tiny search engine with cosine similarity.

FreeAbout 15 min

Step 1 of 8

Movies as arrows

Suppose you score every movie from 0 to 5 on two things: how much action it has and how much comedy. A car-chase thriller might score (4,2)(4, 2); a romantic comedy (1,3)(1, 3). Each movie is now a pair of numbers, which you can draw as an arrow from the origin.

A search works the same way. Ask for "mostly action, a bit funny" and your request becomes an arrow too, say q=(2,1)\mathbf{q} = (2, 1). The movies to show you are the ones whose arrows point in nearly the same direction as q\mathbf{q}.

A vector x∈Rn\mathbf{x} \in \mathbb{R}^n is an ordered list of nn real numbers x1,x2,…,xnx_1, x_2, \dots, x_n, its components. Vectors are columns; to save space we often write them sideways, as in x=(x1,x2)\mathbf{x} = (x_1, x_2).

Real systems use the same idea with hundreds of components instead of two. A list of numbers that describes a word, a picture or a movie is called an embedding.

Drag the tips of the movies b\mathbf{b} and c\mathbf{c}. Which one points closer to the direction of q\mathbf{q}?