Please enable JavaScript to use CodeHS

AI Explained

Vectors and embeddings

A vector is just a list of numbers — but lists of numbers can describe position, color, or meaning. Embeddings are vectors that place similar ideas close together in space.

AI ~12 min High school
The big idea

Vectors describe the world

A vector is a list of numbers. The list might represent a point in space, a color, or the meaning of a word. The magic is that the same tools work on all of them: you can add vectors, subtract them, and measure how far apart they are.

Core idea: if two things are similar, their vectors should be close. If they are different, their vectors should be far apart.

In AI, the vector for a word is called an embedding. The model learns the embedding from reading huge amounts of text. It discovers that words like “king” and “queen” end up close, while “king” and “apple” end up far apart.


Dot product

Measuring similarity

There are many ways to measure how close two vectors are. One of the most useful is the dot product: multiply the matching numbers and add them up.

vectors.py
cat     = [0.2, 0.8, 0.1]
kitten  = [0.3, 0.7, 0.2]
car     = [0.8, 0.1, 0.5]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

print(dot(cat, kitten))  # 0.64  (similar)
print(dot(cat, car))     # 0.23  (less similar)

If two vectors point in similar directions, their dot product is large. If they point in opposite directions, it is negative. The dot product is why a single neuron can act like a pattern detector: it takes the dot product of the input vector and its weight vector.


Interactive

Embeddings learn meaning

Below is a toy 2D embedding space. Each word is a dot. Click a word to see its nearest neighbors. Then try the vector arithmetic puzzle: king − man + woman should land near queen.

Click a word to highlight it and its nearest neighbors.

Result: —

Nearest neighbors for king

Select two words to compare.

Geometry

Vector arithmetic

Because embeddings are vectors, you can add and subtract them. The classic example is:

king − man + woman ≈ queen

This is not magic. The model learned that the direction from “man” to “woman” captures a gender relationship. Adding that direction to “king” shifts it toward the female royal. The same direction often works for other word pairs: “actor” to “actress,” “uncle” to “aunt,” and so on.

Why this matters: embeddings turn meaning into geometry. Relationships become directions, similarities become distances, and analogies become vector arithmetic.


Check yourself
?

Make an analogy

Using the widget, what words should you combine to get “paris” from “tokyo”? (Think: Tokyo is to Japan as Paris is to France.)

france − japan + tokyo should land near paris. The vector from “japan” to “tokyo” captures the “capital city” direction; adding it to “france” points toward “paris.”


Quick quiz

Three questions

1. What does the dot product measure between two vectors?

2. What is a word embedding?

3. Why does king − man + woman land near queen?

Keep learning

Embeddings give words a home in vector space. The next step is to see how a model turns a sequence of word vectors into a prediction for the next word.

Part of the AI Explained tutorial series.