Python, from Zero, for AILesson 72 of 89

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

dtypes: what float16 costs, why int8 wraps round, and how to compare floats

In this lesson

  • 9 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 3 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
One hundred thousand embeddings of 1,536 dimensionsfloat641229float32614float16307int8, quantised154megabytesSame vectors, four dtypes, exact arithmetic: 153.6 million numbers times the bytes each takes. float32is the working standard for embeddings and costs nothing you can measure in a ranking; float16 halvesit again and keeps about three significant figures; int8 needs a scale factor and a reason.
Same vectors, four dtypes, exact arithmetic: 153.6 million numbers times the bytes each takes. float32 is the working standard for embeddings and costs nothing you can measure in a ranking; float16 halves it again and keeps about three significant figures; int8 needs a scale factor and a reason.

Figure

A little at a time

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

Step 1 of 9Free · no account needed
Lesson steps

Bytes per element

Every array has a dtype, and the dtype decides three things at once: how much memory each element takes, the largest value it can hold, and how many significant figures it keeps.

Python
np.zeros(1_000_000, dtype=np.float64).nbytes   # 8,000,000
np.zeros(1_000_000, dtype=np.float32).nbytes   # 4,000,000
np.zeros(1_000_000, dtype=np.float16).nbytes   # 2,000,000
np.zeros(1_000_000, dtype=np.int8).nbytes      # 1,000,000

A million float64 values is 8 MB. A model with 7 billion parameters stored as float16 is 14 GB; as float32, 28 GB; as int8 after quantisation, 7 GB. That arithmetic (parameters times bytes per parameter) is the single most useful calculation in deciding whether a model fits on a machine, and it is nothing more than nbytes.

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) ‘dtypes: what float16 costs, why int8 wraps round, and how to compare floats’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/dtypes-and-precision (Accessed: 19 November 2026).