ContentsThe library

Vectors, Distance and Similarity

Dividing the Lengths Out

Last timeOne Number From Two Vectors

Divide the dot product by both lengths and what is left is the cosine of the angle, a similarity between minus one and one that ignores how long either vector is.

The dot product answered a question about direction and quietly included the

lengths in the answer. The fix is to divide them out, and the result is the

single most used similarity measure in practice.

FIG 1Cosine similarity
the dot product, which already contains the two lengths and the cosine multiplied together
the two lengths, divided out so that only the cosine is left
Nothing is being approximated. The geometric form of the dot product was the two lengths times the cosine, so dividing by the lengths returns the cosine exactly, and the whole operation is a rearrangement of a fact from the previous lesson.

What the division buys

Multiply any vector by three. Its dot product with everything triples, and its

length triples too, so the ratio does not move at all. That is the property

people are usually after: a long document and a short one about the same subject

should count as similar, and under this measure they do.

FIG 2The same direction at three lengths, scored against one query
steplength of the stored vectordot productcosinewhat happened
110.80.8The original.
221.60.8The same direction, twice as long. The dot product has doubled and the cosine has not moved.
30.50.40.8Half as long. Same story in the other direction.
3 steps
The three rows are the same item recorded with different intensities. Under the dot product they are three different results; under the cosine they are one, which is either exactly what you wanted or exactly what you lost.

Reading the number

The value always lies between minus one and one, and it is tempting to read it

as a percentage of similarity. Resist that.

In a real collection of learned vectors the scores do not use the available

range. Unrelated items commonly score well above zero, sometimes above a half,

because the vectors occupy a narrow cone rather than spreading out over all

directions. A score of 0.8 may be excellent in one collection and unremarkable in

another.

The lesson stops here

3 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01The Same List, Read Two Ways
  2. 02Straight Lines and City Blocksopening only
  3. 03Multiply, Add, and Get an Angleopening only
  4. 04Dividing the Lengths Outyou are here
  5. 05When Distance and Angle Become the Same Questionopening only
  6. 06Everything Is Far Away and Nothing Is Nearbyopening only
  7. 07The Cost of Asking What Is Nearbyopening only
  8. 08Which Question Are You Actually Askingopening only

Read alongside