Skip to content

Preface

How this book works and how to get the most from it

About this book

This is a first book on data science, the practice of learning useful things from data, using the Python programming language. It follows a piece of work from its beginning to its end: asking a question that data can answer; loading, cleaning and reshaping the data; exploring it with summaries and charts; reasoning about uncertainty with probability and statistics; building models that predict numbers and categories or find groups; judging honestly how good those models are; and communicating the result responsibly. The last chapter puts everything together in a complete project.

The book teaches the four libraries on which most data work in Python rests: NumPy for numerical arrays, pandas for tables, matplotlib for plots, and scikit-learn for machine learning, with a little of SciPy for statistics. But it is not a manual of those libraries. Each tool is introduced to answer a question, and the emphasis throughout is on the reasoning: why a step is needed, what can go wrong, and how to tell whether a result can be trusted.

Who it is for

The book is written for beginners: students in a first university course in data science, analytics or statistics with computing, and working people from any field who want to analyse data themselves. It assumes:

  • school mathematics: algebra, graphs, percentages, and the idea of an average. The few formulas used beyond this (a sum written with \(\sum\), a square root, a logarithm) are explained where they appear;
  • a little programming, in Python or another language: knowing what a variable, a loop and a function are. Chapter 2 reviews the parts of Python that data work needs, so a reader who has programmed a little in any language can start there.

No previous knowledge of statistics or machine learning is needed.

How the book is organised

Chapters Part What you learn
1 The workflow what data science is, the seven stages of a project, kinds of data
2–3 Python foundations Python for data work; NumPy arrays and vectorised computing
4–5 Working with tables pandas: loading, selecting, cleaning, reshaping, grouping, joining
6 Looking at data visualisation with matplotlib and exploratory data analysis
7 Reasoning with uncertainty probability, distributions, sampling, confidence intervals, hypothesis tests
8–11 Models regression; classification; evaluation and cross-validation; clustering and PCA
12 Special kinds of data text and time series
13 Responsibility ethics, privacy, fairness and reproducibility
14 Putting it together a complete project from raw data to report

Table P.1. The plan of the book.

The chapters are meant to be read in order: each builds on the ones before. Readers who already know Python can skim Chapter 2; readers who know some statistics can move quickly through Chapter 7. Chapters 12 and 13 can be read in either order.

How each chapter is built

Every chapter has the same parts:

  • Learning outcomes at the top, saying what you should be able to do at the end.
  • Numbered sections that build from intuition to precision, with worked examples and code.
  • Boxes: a key idea box picks out the one thing in a section not to forget; note boxes add detail; caution boxes mark common mistakes.
  • At least one figure or table, with a caption that can be read on its own.
  • A summary of the chapter's main points.
  • Exercises, each with its answer folded underneath. Try each exercise before opening its answer; the struggle is where the learning happens.
  • A short quiz to check your understanding, with instant feedback.

How to use it

Read with Python open. Every piece of code in the book is complete and has been run; the output printed under it is the real output. Type the code yourself (or copy it), run it, and compare. Then change something (a number, a column, a parameter) and predict what will happen before you run it again. That habit, predicting and then checking, builds understanding faster than reading alone.

Within a chapter, the code blocks run in order and build on each other, as cells in a notebook do: a table created in one block is used in the next. Each chapter starts afresh, importing what it needs, so you can begin any chapter without running the earlier ones.

All the datasets are created in code, usually with a seeded random number generator, so you need no files and no internet connection, and your results will match the book's. Simulated data has a second advantage for learning: because we know the rules that generated it, we can check whether a method recovers them. Real data never comes with that answer key, which is why the book also insists on baselines, held-out data and honest uncertainty.

The software

You need Python 3 and four libraries. If they are not installed, install them from a terminal with pip:

python -m pip install numpy pandas matplotlib scikit-learn

SciPy is installed automatically with scikit-learn. You can run the code as scripts, in the interactive Python shell, or in a notebook environment such as Jupyter.

The outputs in this book were produced with Python 3.12, NumPy 2.5, pandas 3.0, matplotlib 3.11, scikit-learn 1.9 and SciPy 1.18. With other versions most results will be identical; some may differ in the last decimal place, in the formatting of printed tables, or occasionally in a name that a library has changed. pandas 3 differs from pandas 2 in two ways that are visible in the book: text columns have their own str type, and selections never modify the table they came from (Section 4.9).

The figures were made with matplotlib by the code shown beside them and saved as SVG files; when you run that code, you get the same figures.

Conventions

  • Code, names of functions and columns, and values typed exactly are set in monospace.
  • New terms are in bold where they are defined; the glossary at the end collects them.
  • Output that a program prints is shown in a separate block under the code.
  • Section numbers are written like "Section 5.3"; figures and tables are numbered by chapter.
  • Money amounts in examples are in an unnamed currency unit (sometimes in thousands of it), because the methods do not depend on the currency.