Skip to content

Similarity and distance

  • Lesson 6
  • 50 min
  • Module 2: Vectors and matrices
Lesson 6 of 18 · Essential Mathematics and Statistics for AISimilarity and distance

What you will be able to do

When a new ticket arrives, a support agent wants to see similar tickets that the team already solved. This lesson shows the two common ways to measure “similar” and when they disagree. It also shows why you must scale features in different units first.

NotebookOptional: check the numbers in a notebookColab · Kaggle · GitHub

The notebook is optional: this lesson works with a calculator. It has the lesson’s tables of cosine similarity and Euclidean distance, the feature-scaling example and the module practice in Python. Its checks say if your answer is right, without showing it. It uses only Python’s standard library, so it needs no install.

Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.

Before you startWhat to know and to have readyAbout 50 minutes

What you need to know

What you need to have ready (the notebook needs none of it)

  • A calculator with a square root key.
  • Your c03-notes.md file. You made it in the “Before you start” box of Mathematical reading skills. Add a heading: ### Similarity and distance. If you cannot find the file, make a new empty file with that name: this lesson does not use earlier answers.
  • About 15 minutes at the end for the module practice and the module check.

The notebook was tested in Jupyter with Python 3.14.6 (standard library only).

  • An assistant that answers from documents. Before a large language model answers a question, the system finds the paragraphs whose embeddings are most similar to the question. The name for this is retrieval-augmented generation (RAG). If the similarity search finds the wrong paragraphs, the answer is wrong too.
  • Recommendations. “Customers who looked at this also looked at…” often means: products whose vectors are close to this product’s vector.
  • Duplicate detection. A customer sends the same problem twice. Two tickets with almost identical embeddings are probably duplicates.
  • Similar past tickets. Larkfield wants to show an agent the three most similar solved tickets next to each new one, with the team that solved them.

All four need one number that says how similar two vectors are. There are two common choices.

A new ticket arrives. T-4200: “I want a refund, my parcel is late.” Priya counts two kinds of words, as in Lesson 2: refund words and delivery words. T-4200 = (2, 1). She compares it with four solved tickets.

Ticket Summary Refund words Delivery words
T-3907 A long complaint: refund for a late, damaged parcel 6 3
T-3912 Mostly about a late parcel, one mention of a refund 1 2
T-3915 A refund for a wrong item, delivered on time 4 1
T-3920 Only about tracking a parcel 0 4
  • New ticket T-4200 (2, 1)
  • Solved tickets
The new ticket T-4200 (orange) and four solved tickets (blue). T-3907 points in exactly the same direction as T-4200, but its tip is far away.
Text description of the chart

The horizontal axis is refund words and the vertical axis is delivery words, from 0 to 7. The new ticket T-4200 is a short orange arrow to (2, 1). T-3907 goes to (6, 3), on the same line as T-4200 but three times as long, so the two arrows overlap. T-3915 goes to (4, 1), a little lower than that line. T-3912 goes to (1, 2), steeper. T-3920 goes straight up to (0, 4). The tip closest to the tip of T-4200 is T-3912. The arrow with the smallest angle to T-4200 is T-3907, with an angle of 0.

The picture shows two ideas of “close”.

Euclidean distance is the length of the straight line from one tip to the other. It is the length of the difference vector. For T-4200 and T-3912, the difference is (2 − 1, 1 − 2) = (1, −1):

d(u,v)=∥u−v∥=(u1−v1)2+(u2−v2)2=12+(−1)2≈1.414d(\mathbf{u}, \mathbf{v}) = \lVert \mathbf{u} - \mathbf{v} \rVert = \sqrt{(u_1 - v_1)^2 + (u_2 - v_2)^2} = \sqrt{1^2 + (-1)^2} \approx 1.414

Read it as “subtract position by position, square, add, and take the square root”. A distance of 0 means the two vectors are equal. A larger distance means less similar. Distance has the unit of the features.

Cosine similarity looks only at the direction. It is the dot product divided by both lengths. That removes the length, so only the angle is left:

cos⁡(u,v)=u⋅v∥u∥ ∥v∥\cos(\mathbf{u}, \mathbf{v}) = \frac{\mathbf{u} \cdot \mathbf{v}}{\lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert}

Read it as “the dot product, divided by the length of u times the length of v”. It is a number from −1 to 1:

  • 1: the same direction (angle 0°), whatever the lengths.
  • 0: a right angle (90°). The vectors share nothing.
  • −1: opposite directions (180°).

Word counts are never negative, so for these tickets the cosine similarity is between 0 and 1. Embeddings can have negative values, so their cosine similarity can be negative.

Sometimes a vector has length 0, for example a ticket with no refund words and no delivery words. Then the cosine similarity is not defined. You cannot divide by 0, and a vector of length 0 has no direction. A search system must handle that case, for example by skipping empty tickets.

Worked example: similar past tickets for T-4200

Section titled “Worked example: similar past tickets for T-4200”

Priya calculates both measures for each solved ticket. The length of T-4200 is √(2² + 1²) = √5 ≈ 2.236.

Ticket Vector Dot product with (2, 1) Length Cosine similarity Angle Difference Euclidean distance
T-3907 (6, 3) 12 + 3 = 15 6.708 15 ÷ (2.236 × 6.708) = 1.000 0° (−4, −2) √20 ≈ 4.472
T-3915 (4, 1) 8 + 1 = 9 4.123 9 ÷ (2.236 × 4.123) = 0.976 12.5° (−2, 0) √4 = 2.000
T-3912 (1, 2) 2 + 2 = 4 2.236 4 ÷ (2.236 × 2.236) = 0.800 36.9° (1, −1) √2 ≈ 1.414
T-3920 (0, 4) 0 + 4 = 4 4.000 4 ÷ (2.236 × 4.000) = 0.447 63.4° (2, −3) √13 ≈ 3.606

The two measures give different rankings:

Rank By cosine similarity (highest first) By Euclidean distance (smallest first)
1 T-3907 (1.000) T-3912 (1.414)
2 T-3915 (0.976) T-3915 (2.000)
3 T-3912 (0.800) T-3920 (3.606)
4 T-3920 (0.447) T-3907 (4.472)

Grace reads the summaries. “T-3907 is the same problem as T-4200: a refund for a late parcel. The customer only wrote more.” Cosine similarity puts it first. Euclidean distance puts it last, because a longer ticket has larger counts, so its tip is far away.

This is the main difference:

  • Cosine similarity ignores length. Use it when the size of a vector is not the point, for example word counts of texts with different lengths, or text embeddings. Most text search uses it.
  • Euclidean distance includes length. Use it when the size matters, for example the amount of an order or the number of items. Use it only after you put the features on a similar scale (see below).

A useful fact. First scale every vector to length 1 (a unit vector, Lesson 2). Then the two measures give the same ranking. That is why many search systems store embeddings as unit vectors. Then the cosine similarity is the dot product.

Explore

Cosine similarity and Euclidean distance

u
(2, 1)
v
(6, 3)
Dot product u · v
15
Length of u
2.236
Length of v
6.708
Cosine similarity
1
Angle
0°
Euclidean distance
4.472

u · v = 2 × 6 + 1 × 3 = 12 + 3 = 15

u and v point in the same direction: cosine similarity 1, angle 0°.

0123456Delivery words0123456Refund wordsdistanceu (2, 1)v (6, 3)
0123456Delivery words0123456Refund wordsdistanceu (2, 1)v (6, 3)
Drag the tips of u and v. The dashed line is the Euclidean distance. The arc shows the angle, which decides the cosine similarity.
Text description of the explorer

The explorer shows two arrows from (0, 0), in a plot from 0 to 6 on both axes. At the start, u = (2, 1), like T-4200, and v = (6, 3), like T-3907. The readouts show the dot product 15 and the lengths 2.236 and 6.708. The cosine similarity is 1, the angle 0° and the Euclidean distance 4.472. A dashed line connects the two tips. Move v to (1, 2). Then the cosine similarity falls to 0.8, the angle is 36.9° and the distance falls to 1.414: closer tips, but a different direction. If you move u to (0, 0), u has length 0. Then the cosine similarity and the angle show “not defined”, and a message explains that a vector of length 0 has no direction. The distance still has a value.

Feature scaling: when one unit decides everything

Section titled “Feature scaling: when one unit decides everything”

Distance adds up the squared differences of all features. So a feature with large numbers can hide all the others. This happens when features have different units.

Priya tests a search with two features from Lesson 1: length and refund words. The new ticket is N. Two solved tickets are candidates.

Ticket Summary Length (hundreds of words) Refund words
N (new) A refund request 2 3
P A shorter refund request: the same problem 1 3
Q A question about a garden chair, no refund 2.2 0

With length in hundreds of words:

  • d(N, P) = √((2 − 1)² + (3 − 3)²) = √1 = 1.000
  • d(N, Q) = √((2 − 2.2)² + (3 − 0)²) = √(0.04 + 9) ≈ 3.007

P is closer. That is correct: P is the same kind of problem.

Now Mei, the data engineer, changes the pipeline to count length in words (200, 100 and 220). Nothing else changes.

  • d(N, P) = √(100² + 0²) = 100.000
  • d(N, Q) = √(20² + 3²) = √409 ≈ 20.224

Now Q is closer. The search suggests a garden chair question for a refund request. The refund words still differ by 3, but a difference of 3 is tiny next to a difference of 100 in length. Cosine similarity does not help either: in words, N has cosine similarity 0.99989 with both P and Q. Both vectors point almost along the length axis, so the measure cannot tell them apart.

Explore

One feature in large units

a
(2, 3)
b
(3, 1)
Dot product a · b
9
Length of a
3.606
Length of b
3.162
Cosine similarity
0.789
Angle
37.9°
Euclidean distance
2.236

a · b = 2 × 3 + 3 × 1 = 6 + 3 = 9

The angle is less than 90°, so the dot product is positive. 20% of the squared distance comes from length, 80% from refund words.

012345Refund words012345Length (hundreds of words)distance37.9°a (2, 3)b (3, 1)
012345Refund words012345Length (hundreds of words)distance37.9°a (2, 3)b (3, 1)
Where the squared distance comes fromLength 20%Refund words 80%
Where the squared distance comes fromLength 20%Refund words 80%
Drag the tips, then switch the length unit to words. The bar shows how much of the squared distance comes from each feature.
Text description of the explorer

The explorer shows two tickets as arrows: a = (2, 3) and b = (3, 1). The horizontal axis is length in hundreds of words. The vertical axis is refund words. At the start, the Euclidean distance is 2.236 and the cosine similarity is 0.789. A bar below the plot shows the share of the squared distance from each feature: length 20%, refund words 80%. When you switch on “Count length in words (× 100)”, the values become a = (200, 3) and b = (300, 1). The distance becomes 100.02, and length gives 99.96% of the squared distance. The cosine similarity becomes 0.99993. A second picture shows the two arrows at their true scale. Both are almost flat, because the length numbers are now 100 times larger. The plot itself keeps the original units, so you can still drag the tips.

The fix: standardise each feature. For each feature, subtract its mean and divide by its standard deviation (SD), both measured on past tickets:

z=value−meanSDz = \frac{\text{value} - \text{mean}}{\text{SD}}

Read it as “how many SDs above or below the mean the value is”. After this step, every feature has mean 0 and SD 1. A difference of one SD then counts the same in every feature, whatever the unit.

Suppose that on last month’s tickets, length has mean 200 words and SD 100 words. Refund words have mean 1.5 and SD 1.5.

Ticket Length in words → z Refund words → z Standardised vector
N (200 − 200) ÷ 100 = 0 (3 − 1.5) ÷ 1.5 = 1 (0, 1)
P (100 − 200) ÷ 100 = −1 (3 − 1.5) ÷ 1.5 = 1 (−1, 1)
Q (220 − 200) ÷ 100 = 0.2 (0 − 1.5) ÷ 1.5 = −1 (0.2, −1)

Now d(N, P) = √1 = 1.000 and d(N, Q) = √(0.04 + 4) ≈ 2.010. P is closer again. If you count length in hundreds of words (mean 2, SD 1), you get exactly the same z values. After standardising, the unit no longer matters.

In scikit-learn, this step is StandardScaler. Calculate the mean and the SD on the past tickets only, and use the same numbers for every new ticket. Embeddings from one embedding model do not need this step: all their positions already have similar sizes. Hand-made features with units, such as words, minutes and counts, do.

A similarity score is easy to misread. Three cautions:

  • It is not a probability. A cosine similarity of 0.93 does not mean “93% sure that this is the same problem”. It measures an angle between two vectors. There is no event and no chance in it.
  • Its scale depends on the model. With some embedding models, unrelated texts still get a cosine similarity of 0.7 or more. With others, related texts get 0.4. “0.8 is high” is true only for one model and one kind of text.
  • Similar is not correct. In RAG, the most similar paragraph can be an old version of a policy. Similarity finds related text. It does not check if the text is true or current.

To decide which similarity is “high enough”, test it. Take pairs of tickets that Grace labelled as “same problem” and “different problem”, and look at their scores. Module 3 gives you the tools to describe how often such a rule is wrong.

One more solved ticket, T-3925, has the vector (3, 4): 3 refund words, 4 delivery words. Compare it with T-4200 = (2, 1).

  1. Calculate the dot product of (2, 1) and (3, 4).
  2. Calculate the length of (3, 4).
  3. Divide the dot product by 2.236 × the length. This is the cosine similarity.
  4. Calculate the difference vector (2 − 3, 1 − 4).
  5. Calculate the Euclidean distance from the difference vector.
  6. Write where T-3925 goes in each ranking table above.
Show the expected result
  1. 2 × 3 + 1 × 4 = 6 + 4 = 10.
  2. √(9 + 16) = √25 = 5.
  3. 10 ÷ (2.236 × 5) = 10 ÷ 11.18 ≈ 0.894.
  4. (−1, −3).
  5. √(1 + 9) = √10 ≈ 3.162.
  6. By cosine similarity, T-3925 is third: between T-3915 (0.976) and T-3912 (0.800). By distance, it is also third: between T-3915 (2.000) and T-3920 (3.606). The order is T-3912, T-3915, T-3925, T-3920, T-3907.

Check: a cosine similarity is never above 1 and never below −1. If your result is outside that range, check the lengths.

Run Python in your browser

Optional: check the table with code

This is optional. The code calculates the cosine similarity and the Euclidean distance between T-4200 and the four solved tickets. Add T-3925 = [3, 4] and check your answer from the guided practice. In professional code, scikit-learn has cosine_similarity and numpy has np.linalg.norm for the length.

Independent variation: scale every vector to length 1

Section titled “Independent variation: scale every vector to length 1”

Change one condition: Priya first divides every vector by its own length, so all vectors have length 1.

  1. Write T-4200 = (2, 1) as a unit vector.
  2. Calculate the Euclidean distance between the unit vectors of T-4200 and T-3907.
  3. Calculate the Euclidean distance between the unit vectors of T-4200 and T-3912.
  4. Which of the two is closer now? Compare with the cosine ranking.
Show the answer
  1. (2, 1) ÷ 2.236 = (0.894, 0.447).
  2. T-3907 ÷ 6.708 = (0.894, 0.447): the same unit vector. The distance is 0.
  3. T-3912 = (1, 2) ÷ 2.236 = (0.447, 0.894). The difference is (0.447, −0.447), so the distance is √(0.2 + 0.2) = √0.4 ≈ 0.632.
  4. T-3907 is now closer, as in the cosine ranking. For unit vectors, distance and cosine similarity always agree: the squared distance equals 2 − 2 × cosine similarity. For T-3912: 2 − 2 × 0.8 = 0.4.

Common mistake

One feature in large units decides the search

Symptom: the similar-ticket search shows past tickets that have about the same length as the new ticket but a different topic. Agents stop using it. Nobody changed the search code.

Cause: a pipeline change measures length in words instead of hundreds of words. The other features are small counts. The squared differences in length are now thousands of times larger, so length alone decides the Euclidean distance. In the explorer above, length gives 99.96% of the squared distance after the change.

Fix: standardise every feature with the mean and SD from past tickets before you measure distance. Then test the search with a few pairs that you know: a ticket and its known duplicate must be closer than two unrelated tickets. Run this test again after every change to the pipeline.

Check your understanding

Two tickets have cosine similarity 1, but Euclidean distance 4.47. Explain how both can be true, and which measure you would use to find tickets about the same problem.

Show the answer

Cosine similarity 1 means the vectors point in exactly the same direction: the same mix of words. The distance is large because one vector is much longer: one ticket has more words. For the same problem, the mix matters more than the length, so cosine similarity is the better choice here. Euclidean distance would rank the long ticket last.

Check your understanding

Predict: Larkfield adds a feature 'order value in cost points', with values from 0 to 5,000, next to word counts from 0 to 10. Without scaling, what happens to a Euclidean similar-ticket search? What do you do?

Show the answer

The order value has much larger numbers, so its squared differences are much larger. It decides almost the whole distance. The search would find tickets with a similar order value, whatever their topic. Standardise each feature first: subtract the mean and divide by the SD, both from past tickets. Then test the search on known pairs.

A new ticket, T-4300, has three counts: refund words, delivery words and account words (“password”, “login”, “email”). T-4300 = u = (2, 1, 2). Priya compares it with one solved ticket, P1 = (1, 2, 2). She writes every intermediate value.

Step Calculation Result
Products, position by position 2 × 1, 1 × 2, 2 × 2 2, 2, 4
Dot product u · P1 2 + 2 + 4 8
Length of u √(4 + 1 + 4) = √9 3
Length of P1 √(1 + 4 + 4) = √9 3
Cosine similarity 8 ÷ (3 × 3) 0.889
Difference u − P1 (2 − 1, 1 − 2, 2 − 2) (1, −1, 0)
Squared differences 1, 1, 0 sum 2
Euclidean distance √2 1.414

Interpretation: the two tickets point in a similar direction (0.889 is close to 1), and their tips are close. Both measures say “similar”. The cosine similarity of 0.889 is not an 89% chance of anything.

Two more solved tickets: P2 = (4, 0, 3) and P3 = (0, 3, 4). In c03-notes.md, add a heading: #### Module practice: Vectors and matrices. Then:

  1. Copy Priya’s table for P2. Fill in every row.
  2. Make the same table for P3.
  3. Rank P1, P2 and P3 by cosine similarity, highest first.
  4. Rank them by Euclidean distance, smallest first.
  5. Write which ticket is “most similar” by each measure.
  6. Write in one or two sentences which measure you would use for word counts, and why.
Show the model answer
Step P2 = (4, 0, 3) P3 = (0, 3, 4)
Products 8, 0, 6 0, 3, 8
Dot product with u 14 11
Length of u 3 3
Length of the ticket √(16 + 0 + 9) = √25 = 5 √(0 + 9 + 16) = √25 = 5
Cosine similarity 14 ÷ 15 ≈ 0.933 11 ÷ 15 ≈ 0.733
Difference (−2, 1, −1) (2, −2, −2)
Squared differences 4, 1, 1 → sum 6 4, 4, 4 → sum 12
Euclidean distance √6 ≈ 2.449 √12 ≈ 3.464
  • By cosine similarity: P2 (0.933), P1 (0.889), P3 (0.733).
  • By Euclidean distance: P1 (1.414), P2 (2.449), P3 (3.464).
  • Most similar: P2 by cosine similarity, P1 by distance. The measures disagree because P2 is longer (length 5) than u and P1 (length 3).
  • For word counts, use cosine similarity. It compares the mix of words and ignores how long the ticket is. A longer message about the same problem should still count as similar.

Review: mark your answer as demonstrated if every value matches to 2 decimal places, both rankings are correct, and the explanation mentions length.

Check your understanding

Retrieval: what is the shape of a matrix with one row for each of 1,200 tickets and one column for each of 4 features? What is the dimension of one row?

Show the answer

The shape is 1,200 × 4: rows first, then columns. One row is the feature vector of one ticket, so its dimension is 4.

Check your understanding

Explain or predict: you have embeddings for 1,200 past tickets in a matrix E with shape 1,200 × 384, and one new embedding q with shape 384 × 1. Which product gives one dot product for each past ticket, E × q or q × E? What shape does it have? Does the other one exist?

Show the answer

E × q: the inner numbers are 384 and 384, so it exists. The result has the outer numbers, 1,200 × 1: one dot product for each past ticket. q × E has inner numbers 1 and 1,200. They are different, so q × E does not exist. If the embeddings are unit vectors, the 1,200 dot products are the cosine similarities, and the largest ones are the most similar past tickets.

Check your understanding

Completion check: Omar, the product manager, sees 'similarity 0.93' next to a suggested past ticket and says: 'So the AI is 93% sure that this is the same problem.' (a) Is the comparison of a 1,200 × 384 matrix with a 384 × 1 vector shape-compatible? (b) What does 0.93 mean, and what would you tell Omar?

Show the answer

(a) Yes. The inner numbers are both 384. The result has shape 1,200 × 1: one score for each past ticket.

(b) 0.93 is a cosine similarity: the two embeddings point in almost the same direction. It is a geometric measure, not a probability. There is no “93% sure” in it. Its meaning also depends on the embedding model, because some models give high scores even to unrelated texts. Tell Omar: “0.93 means the texts are close in the model’s vector space. To know how often a 0.93 match is really the same problem, we must test scores on pairs that Grace has labelled.”

Review: the answer is demonstrated if (a) uses the inner numbers and gives the result shape. Part (b) must say that the score is not a probability, and name a way to find out what a score means in practice.

Recap

In this module, you learned how an AI system stores data and compares it.

  • Represent: one observation is a vector of feature values, many observations are a matrix with shape rows × columns. Images are matrices of pixels. Text becomes an embedding: a long vector whose positions have no names.
  • Calculate: add and scale vectors position by position. The dot product multiplies and adds: it is the score of a linear model. The length is the square root of the sum of squares. Matrix multiplication is many dot products: (n × k) × (k × m) → (n × m).
  • Compare: Euclidean distance measures how far apart two tips are and includes length. Cosine similarity measures direction only, from −1 to 1, and is not defined for a vector of length 0. Standardise features in different units first.
  • Interpret: a similarity score is not a probability. Test what a score means on labelled pairs before you trust it.

Next, Module 3 starts with chance: how to describe events that are uncertain, with counts instead of formulas.

Next: Events and uncertainty