See what vectors, matrices, and dot products actually mean — geometrically and in code.
the problem
Open any ML paper. Within the first page you'll see vectors, matrices, dot products, and transformations. Without linear algebra intuition, these are just symbols. With it, you can see what a neural network is actually doing — moving points around in space. You don't need to be a mathematician. You need to see what these operations mean geometrically, then code them yourself.
concept 1 — vectors are points and directions
A vector is coordinates in space
A vector is just a list of numbers — but those numbers mean something. They're coordinates. In AI, vectors represent everything:
word → 768 numbersimage → millions of pixelsuser → preference scores
concept 2 — matrices are transformations
A matrix moves points in space
A matrix transforms one vector into another. It can rotate, scale, stretch, or project. In AI, matrices are the model.
Neural net weights
Matrices that transform input into output at each layer
Attention scores
Matrices that decide what parts of the input to focus on
Embeddings
Matrices that map words and tokens into vector space
concept 3 — dot product measures similarity
The dot product is how similarity is computed
The dot product of two vectors tells you how similar they are — and it powers search, RAG, and recommendations.
1 / 3
the code
a = [1, 2, 3]
3.7417
magnitude |a|
a · b
32
dot product
cosine similarity
0.9746
nearly identical direction
vector.py
classVector:
def__init__(self, components):
self.components = list(components)
self.dim = len(self.components)
def__add__(self, other):
returnVector([a + b for a, b inzip(self.components, other.components)])
def__sub__(self, other):
returnVector([a - b for a, b inzip(self.components, other.components)])
defdot(self, other):
returnsum(a * b for a, b inzip(self.components, other.components))
defmagnitude(self):
returnsum(x**2for x inself.components) ** 0.5defnormalize(self):
mag = self.magnitude()
returnVector([x / mag for x inself.components])
defcosine_similarity(self, other):
returnself.dot(other) / (self.magnitude() * other.magnitude())
def__repr__(self):
returnf"Vector({self.components})"
a = Vector([1, 2, 3])
b = Vector([4, 5, 6])
print(f"a + b = {a + b}")
print(f"a · b = {a.dot(b)}")
print(f"|a| = {a.magnitude():.4f}")
print(f"cosine similarity = {a.cosine_similarity(b):.4f}")
output
a + b =Vector([5, 7, 9])
a · b =32
|a| =3.7417
cosine similarity =0.9746
method reference
.dot(other)
Σ aᵢ×bᵢ — measures alignment. High = similar direction.
.magnitude()
√Σ xᵢ² — the length of the vector in space.
.normalize()
Divide by magnitude. Result has length = 1.
.cosine_similarity()
dot/(|a|×|b|). Range −1 to 1. Powers search and RAG.
💡a · b = 32. Both vectors point in the same direction — large positive dot product. In a search engine, this means the query and document are highly relevant to each other.
in practice — how AI uses this
Real world: Spotify "Recommended Songs"Every song, user, and playlist is a vector. Dot product finds what you'll love next.
1
Your taste → a vector
Based on your listening history, Spotify encodes your taste as a high-dimensional vector. Lots of indie rock, late-night jazz, and fast BPM → specific coordinates in music space.
2
Every song is also a vector
Each of Spotify's 100M+ tracks is embedded into the same space using audio features, lyrics, and listening patterns. Similar songs cluster nearby.
3
Dot product finds your next song
Spotify computes the dot product (cosine similarity) between your taste vector and every song vector. The top matches become your "Discover Weekly" — computed fresh every Monday.
Cosine similarity: your taste vs candidate songs
Sufjan Stevens — "Death With Dignity"indie dream-pop · 68 BPM · night drive