Python, from Zero, for AILesson 74 of 89

Module 8 · Numbers in bulk: NumPy, pandas and the vector under every model

Dot products and cosine similarity: one vector against a hundred thousand in a single line

In this lesson

  • 9 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 5 of 10 in this chapter

This chapter’s goalLoad a messy CSV into pandas, clean its missing values and types, group and join it into the table a question needs, convert it to a NumPy array of the right shape and dtype, compute cosine similarity between one vector and a hundred thousand in a single vectorised expression, plot the result to a file, and explain from memory layout why the array version runs a hundred times faster than the loop it replaced.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:43Not available
Full audiobookNot availableNot available
Scoring one query against every document02500500010000400000Documents in the indexMilliseconds per query—— Python loop, np.dot per row– – M_unit @ q, one lineBoth lines are straight: the arithmetic is rows times dimensions either way. What differs is theconstant. The loop pays Python’s per-row overhead a hundred thousand times; the matrix product handsthe whole block to a compiled routine once. Brute force stays honest to a few hundred thousand rows,and an index earns its complexity after that.
Both lines are straight: the arithmetic is rows times dimensions either way. What differs is the constant. The loop pays Python's per-row overhead a hundred thousand times; the matrix product hands the whole block to a compiled routine once. Brute force stays honest to a few hundred thousand rows, and an index earns its complexity after that.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

An embedding is a row of floats

Ask an embedding model for a vector and you get back a list of numbers: 384 for a small open model, 1,536 or 3,072 for the hosted ones. What the numbers mean is the business of the machine learning course; here they are a (768,) float32 array, and a corpus of a hundred thousand documents is a (100000, 768) matrix. Everything below is arithmetic on that matrix.

Python
q = np.asarray(embed("how do I reset my password"), dtype=np.float32)   # (768,)
M = np.load("embeddings.npy")                                            # (100000, 768)

np.save/np.load write the raw array with a small header: the fastest way to persist one, and much smaller than JSON.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Dot products and cosine similarity: one vector against a hundred thousand in a single line’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/vectors-and-cosine-similarity (Accessed: 21 November 2026).