1. Home
  2. Blog
  3. Artificial Intelligence

Artificial Intelligence

Cosine Similarity Explained: Formula, Worked Example and Python Code

Cosine similarity measures how closely two vectors point in the same direction, from -1 (opposite) to 1 (identical direction), ignoring their length. Learn the formula, work one example by hand, run it in Python and NumPy, and see why AI search and RAG depend on it.

8 min read
Cosine similarity explained with Python

Key takeaways

  • Cosine similarity = dot product ÷ (length of A × length of B). It compares direction, not size.
  • For [3, 4] and [6, 8] it is exactly 1.0, even though one vector is twice as long; perpendicular vectors give 0 and opposite ones give -1.
  • In our word-count test, cosine ranked a long, repetitive document second while Euclidean distance ranked it last.
  • With vectors normalised to length 1, cosine similarity equals a plain dot product, which is why vector databases often normalise first.

Every time a search engine finds documents “like” your query, or a chatbot retrieves the right paragraph from your files, there is a good chance cosine similarity is doing the comparison. It is one of the most useful ideas in machine learning, yet it needs only school maths. All numbers below came from code we ran on Python 3.13.6, with NumPy 2.5.4 for the vectorised version.

The cosine similarity formula

cos(A, B) = (A · B) / (‖A‖ × ‖B‖)

A · B = a1×b1 + a2×b2 + ... + an×bn      (dot product)
‖A‖   = √(a1² + a2² + ... + an²)          (length, or norm)

The result is the cosine of the angle between the two vectors. Two arrows pointing the same way have a 0° angle, and cos 0° = 1. Arrows at right angles have cos 90° = 0. Arrows pointing opposite ways have cos 180° = -1. Because the lengths are divided out, only the direction counts.

A worked example by hand

Take A = [3, 4] and B = [6, 8]:

  1. Dot product: 3×6 + 4×8 = 18 + 32 = 50.
  2. Length of A: √(9 + 16) = 5. Length of B: √(36 + 64) = 10.
  3. Cosine similarity: 50 ÷ (5 × 10) = 1.0.

B is just A doubled, so they point in exactly the same direction. Our code agreed: cos(A,B) = 1.000. The Euclidean distance between them, however, was 5.0, so a distance-based measure would call them quite different.

What the values mean

Pair Vectors Cosine Meaning
Same direction [3, 4] and [6, 8] 1.000 Identical direction
Perpendicular [3, 4] and [4, -3] 0.000 Unrelated
Opposite [3, 4] and [-3, -4] -1.000 Opposite direction

With word counts, which are never negative, scores stay between 0 and 1. With embeddings, negative values are possible, but in practice most related text scores well above 0. Cosine distance is simply 1 minus cosine similarity, used where a library expects “smaller is closer”.

Calculate it in pure Python

import math

def cos(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    na = math.sqrt(sum(x * x for x in a))
    nb = math.sqrt(sum(y * y for y in b))
    return dot / (na * nb)

print(cos([3, 4], [6, 8]))     # 1.0

Ten lines, no libraries. It is perfect for learning and small jobs. Note what happens with a zero vector: cos([0, 0], [1, 2]) raised ZeroDivisionError: float division by zero in our run, because a vector with no length has no direction. Guard against it in real code.

Fast version with NumPy

import numpy as np

A, B = np.array([3, 4]), np.array([6, 8])
print(np.dot(A, B) / (np.linalg.norm(A) * np.linalg.norm(B)))   # 1.0

For many vectors at once, normalise each row and use one matrix multiplication. We created 10,000 random 384-dimension vectors (384 is a common embedding size) and compared all of them with a query vector:

Mn = M / np.linalg.norm(M, axis=1, keepdims=True)
scores = Mn @ (q / np.linalg.norm(q))
top = np.argsort(-scores)[:3]

The full comparison and ranking took 25.5 ms. The top hit was the query itself with a score of 1.0, and the next best random vectors scored only about 0.18, as you would expect from unrelated random data. scikit-learn offers the same calculation as sklearn.metrics.pairwise.cosine_similarity, which we did not run here.

Tip: normalise vectors once when you store them. After that, cosine similarity is just a dot product. In our 768-dimension test, the dot product of normalised vectors matched the full formula to six decimal places (0.885104 both ways).

A text example: word counts

Turn each document into counts over a small vocabulary: python, error, install, snake, bite, pip. Then compare each with the query “python pip install error”:

Doc Text Cosine Euclidean distance
d1 python install error pip 1.000 0.00
d4 python error error error install pip pip 0.904 2.24
d2 pip install python python 0.816 1.41
d3 snake bite python 0.289 2.24

Cosine ranked d4 second: it is about exactly the same topic, just longer and more repetitive. Euclidean distance tied it with d3, the snake document, because extra words increase distance whatever they mean. That is the core reason text search prefers cosine: document length should not decide relevance. Real systems use embeddings instead of raw counts, so “install failed” can match “setup error” even without shared words; see what are embeddings.

Cosine similarity vs Euclidean distance

Aspect Cosine similarity Euclidean distance
Measures Angle (direction) Straight-line gap
Affected by length No Yes
Range -1 to 1 0 to infinity
Best for Text, embeddings, recommendations Coordinates, sensor readings, clustering raw measurements

For vectors normalised to length 1, the two give the same ranking, so the choice matters most for raw, unnormalised data.

Why AI search and RAG use it

In retrieval-augmented generation, the pattern that lets a large language model answer from your own documents, every document chunk is turned into an embedding and stored. A question is embedded the same way, and the system returns the chunks with the highest cosine similarity. We simulated this with 768-dimension vectors: a vector made by adding random noise (half the signal’s strength) to another scored 0.885 against it, while an unrelated random vector scored 0.011. Related meanings sit close in direction; unrelated ones are near 0. Vector databases speed this up with approximate nearest-neighbour indexes; see our vector database guide and what is RAG. How you split documents first also matters, as our RAG chunking strategies test showed.

From score to angle

Because the score is a cosine, you can turn it back into an angle with math.degrees(math.acos(score)). For our text example, d4 at 0.904 sat at about 25.3° from the query, d2 at 0.816 at 35.3°, and the snake document at 0.289 at 73.2°. Angles make the intuition concrete: small angles mean the same topic, angles near 90° mean little in common.

How to choose a similarity threshold

Many systems keep only matches above a cut-off, such as 0.75. There is no universal right number, because each embedding model spreads its scores differently. A practical process:

  1. Collect 30 to 50 real queries and mark which documents are truly relevant.
  2. Compute cosine scores for all query and document pairs.
  3. Look at the score ranges of relevant and irrelevant pairs; they usually overlap a little.
  4. Pick the threshold that balances missed answers against wrong ones for your use case, then re-check it whenever you change models.

Often it is safer to return the top few matches by score and let the application or the language model decide, instead of trusting one fixed cut-off.

Pitfalls to avoid

  • Zero vectors: they cause division by zero; skip or handle them.
  • Mixing models: embeddings from different models live in different spaces, so their cosine scores are meaningless together.
  • Treating scores as percentages: 0.8 is not “80% similar”, and good thresholds differ by model. Calibrate on your own data.
  • Ignoring magnitude when it matters: if size carries meaning, such as purchase amounts, cosine throws that information away.
  • Integer overflow or precision: use floating point and normalise once, not on every query.

Interview-ready summary

“Cosine similarity measures the angle between two vectors: the dot product divided by the product of their lengths. It ranges from -1 to 1 and ignores magnitude, which makes it ideal for comparing text and embeddings of different lengths. Cosine distance is 1 minus the similarity, and for normalised vectors cosine similarity equals the dot product, which vector databases use for speed.” Back it up with the [3, 4] and [6, 8] example, which scores exactly 1.0 even though the vectors differ in length.

Final thoughts

Cosine similarity is the dot product of two vectors divided by their lengths, and it answers one question: do these point the same way? That makes it ideal for text and embeddings, where length is noise and direction is meaning. Learn it by hand once, use NumPy or your vector database in practice, normalise early, and calibrate thresholds on your own data.

Frequently asked questions

What is cosine similarity in simple terms?

It is a score from -1 to 1 that shows how closely two vectors point in the same direction. 1 means the same direction, 0 means unrelated and -1 means opposite.

What is the cosine similarity formula?

Cosine similarity equals the dot product of A and B divided by the product of their lengths: (A · B) / (‖A‖ × ‖B‖).

What is the difference between cosine similarity and cosine distance?

Cosine distance is 1 minus cosine similarity. Similarity is higher for closer vectors, while distance is lower for closer vectors.

Why is cosine similarity used for text and embeddings?

It ignores vector length, so a long document and a short one about the same topic still score as similar. Only the direction, which reflects meaning, counts.

How do I calculate cosine similarity in Python?

Use np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) with NumPy, sklearn.metrics.pairwise.cosine_similarity for batches, or a short pure Python function with math.sqrt.

Keep learning

Work with Stack Quora

Need help with testing, WordPress or SEO?

We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.

Keep reading

More guides on testing, AI & modern development

Enjoyed this article? Pick a topic and jump straight into our newest tutorials.

Artificial Intelligence

BM25 Explained: How Keyword Search Ranks Results, With Python Code and Hybrid Search

BM25 explained with a pure Python implementation we ran: the formula, IDF, term saturation, the k1 and b parameters, why…

Read article →
Software Development

HTTP Methods Explained: GET, POST, PUT, PATCH, DELETE, Tested on a Live Server

HTTP methods explained with a small server we ran: GET, POST, PUT, PATCH, DELETE, HEAD and OPTIONS, safe vs idempotent,…

Read article →
DevOps

Git Bisect Explained: Find the Commit That Broke Your Code, Manually or Automatically

Git bisect explained with a tested repository: find the one bad commit among 41 in 5 test runs, automate it…

Read article →
Test Automation

pytest Markers Explained: Custom Marks, skip, skipif, xfail and -m Filters

pytest markers explained with a tested suite: register custom marks, run only smoke tests with -m, combine filters with and,…

Read article →
Software Testing

Positive vs Negative Testing: Differences, 24 Real Test Cases and a Checklist

Positive vs negative testing explained with a real signup form: 6 positive and 18 negative test cases run in pytest,…

Read article →
Databases & SQL

SQL ORDER BY Multiple Columns: ASC, DESC, NULLS LAST, Case and Custom Order

SQL ORDER BY with multiple columns explained with tested queries: mixed ASC and DESC, tie-breakers, NULLS FIRST and LAST, case-insensitive…

Read article →
Databases & SQL

SQL Running Total: SUM OVER, Per-Group Totals, Moving Sums and the RANGE Trap

How to calculate a SQL running total with SUM() OVER: per-group totals, daily cumulative sums, moving sums, percent of total,…

Read article →
Software Development

JavaScript Sort Array: Numbers, Objects, Strings and Why 10 Comes Before 5

How to sort an array in JavaScript correctly: numbers with a compare function, objects by one or more fields, strings…

Read article →
Software Development

Python String Methods: Cheat Sheet, Real Examples and the strip() Trap

Python string methods with tested examples: split, join, strip, replace, find, startswith, is-methods, casefold, partition and translate, plus the strip()…

Read article →
Software Development

Python zip() Explained: Loop Two Lists at Once, Unzip, strict=True and Traps

Python zip() pairs items from several lists in one loop. Tested examples for dicts, unzipping with *, strict=True, zip_longest, sliding…

Read article →
Artificial Intelligence

Cosine Similarity Explained: Formula, Worked Example and Python Code

Cosine similarity explained simply: the formula, a worked example by hand, pure Python and NumPy code, cosine vs Euclidean distance…

Read article →
Software Development

UTF-8 Encoding Explained: How Text Becomes Bytes and How to Fix Garbled Text

UTF-8 encoding explained with byte-level examples we ran in Python: how characters become 1 to 4 bytes, why café turns…

Read article →
DevOps

Git Squash Commits: 4 Easy Ways, Tested Step by Step (rebase -i, reset, merge)

Git squash commits explained with 4 tested methods: interactive rebase, reset –soft, merge –squash and fixup with autosquash. Before-and-after logs,…

Read article →
Test Automation

pytest parametrize Explained: 7 Patterns with Tested Examples

pytest parametrize explained with 7 tested patterns: basic cases, readable ids, stacked decorators, pytest.param with xfail and skip, indirect fixtures…

Read article →
Software Testing

Retesting vs Regression Testing: 8 Key Differences with a Real Bug Fix Example

Retesting vs regression testing explained with a real bug fix run in pytest: retesting confirmed the fix, the regression run…

Read article →
Databases & SQL

Delete Duplicate Rows in SQL: 5 Safe Methods, Tested Step by Step

How to delete duplicate rows in SQL safely: find them, preview them, then remove them with ROW_NUMBER, MIN(id), EXISTS, rowid…

Read article →
Databases & SQL

SQL RANK vs DENSE_RANK vs ROW_NUMBER: Differences with Tested Examples

SQL RANK vs DENSE_RANK vs ROW_NUMBER in one table: how each handles ties, top-N per group, the second-highest salary trap…

Read article →
Software Development

JavaScript Optional Chaining (?.) Explained: 8 Examples, ?? Defaults and Traps

JavaScript optional chaining stops “Cannot read properties of undefined” errors. 8 tested examples for properties, arrays and methods, ?? vs…

Read article →
Artificial Intelligence

RAG Chunking Strategies Explained: Fixed, Overlap, Sentence, Recursive and Headings, Measured

RAG chunking strategies compared on one document with tiktoken 0.14: fixed-size, overlap, sentence packing, recursive splitting and heading-based chunks, measuring…

Read article →
DevOps

Git Stash Explained: Save, Apply, Pop and Untracked Files, Tested

Git stash explained with commands we ran on Git 2.56: push with a message, untracked files, list and show, apply…

Read article →
Software Testing

Pairwise Testing Explained: How All-Pairs Cuts 216 Tests to 13, Tested

Pairwise testing explained with a generator we ran in Python 3.13: a checkout with 216 combinations covered by 13 tests,…

Read article →
Test Automation

Test Doubles Explained: Dummy, Stub, Spy, Mock and Fake, with Tested Python Code

Test doubles explained with one Python example run in pytest 9.2: dummy, stub, spy, mock and fake, when to use…

Read article →
Databases & SQL

SQL COALESCE Explained: Defaults, NULLIF, IFNULL vs ISNULL and Traps, Tested

SQL COALESCE explained with queries we ran in SQLite 3.50: first non-null values, empty strings, NULL in maths, NULLIF for…

Read article →
Databases & SQL

SQL Self Join Explained: Employee-Manager, Pairs and Row Comparisons, Tested

SQL self join explained with queries we ran in SQLite 3.50: employee and manager, earning more than the manager, unique…

Read article →
Artificial Intelligence

LLM Context Window Explained: Tokens, Limits and How to Fit More, Measured

The LLM context window explained with token counts we measured using tiktoken: words vs tokens for prose, code and Hindi,…

Read article →
DevOps

Git Cherry-Pick Explained: Copy the Commit You Need, Safely, Tested

Git cherry-pick explained with commands we ran in Git 2.56: copying a hotfix to a release branch, new hashes and…

Read article →
Test Automation

Snapshot Testing Explained: How It Works, When It Helps and When It Hurts

Snapshot testing explained with a pytest and syrupy demo we ran: creating snapshots, reading the diff after a code change,…

Read article →
Software Testing

Mutation Testing Explained: Why 100% Coverage Still Missed Our Bug

Mutation testing explained with a small mutation tool we built and ran: 14 mutants, a test suite with 100% line…

Read article →
Artificial Intelligence

Prompt Injection Explained: How AI Apps Get Tricked and How to Defend Them

Prompt injection explained for builders: direct and indirect attacks, why keyword filters fail (our test blocked 1 of 4), and…

Read article →
DevOps

DNS Records Explained: A, AAAA, CNAME, MX, TXT and TTL With Real Lookups

DNS records explained with lookups we ran: how resolution works, A and AAAA, CNAME rules, MX and null MX, TXT…

Read article →
DevOps

Feature Flags Explained: Safe Releases, Gradual Rollouts and Flag Hygiene

Feature flags explained with a rollout simulation we ran: flag types, percentage rollouts with stable hashing, kill switches, allow lists,…

Read article →
Test Automation

Test Data Generation With Python Faker: Realistic, Repeatable and Safe Data

Test data generation with Python Faker, run and measured: seeding for repeatable data, locales, unique values, domain-shaped records, edge cases…

Read article →
Software Testing

Shift-Left Testing Explained: Find Bugs Earlier Without Slowing the Team

Shift-left testing explained: what it means, why early defects are cheaper, practices from requirement reviews to CI checks, the tester's…

Read article →
Artificial Intelligence

LLM Structured Output Explained: Getting Reliable JSON From AI Models

LLM structured output explained with validation we ran in Pydantic: JSON mode and schemas, constrained decoding, the ways model output…

Read article →
Test Automation

API Contract Testing Explained: Catch Breaking Changes Before They Ship

API contract testing explained with a schema check we ran: what a contract is, consumer-driven contracts, Pact, OpenAPI, which changes…

Read article →
Software Testing

Risk-Based Testing Explained: Score Risks, Build a Matrix and Test What Matters

Risk-based testing explained step by step: identifying risks, scoring likelihood and impact, building a risk matrix, mapping risk to test…

Read article →