Sub topic 1
What reproducible means and why notebooks drift
Prerequisites
This chapter assumes you can do the following:
- Write basic Python and use Jupyter notebooks (create, edit, and run cells).
- Use a command line to run simple commands.
- Use Git for basic commits.
- Install packages with pip or conda.
You do not need any tools beyond a working Python and Jupyter install. Later chapters cover each fix in detail. This chapter gives you the working model of the problem.
What "reproducible" means
People use several words for related ideas. This tutorial uses them in a specific way.
| Term | Meaning | Example |
|---|---|---|
| Repeatable | You run the same code on the same machine and get the same result. | You rerun your notebook tomorrow. |
| Reproducible | Someone else runs your code and data in a recreated environment and gets the same result. | A colleague clones your repository and runs it. |
| Replicable | A new study, with new data, reaches the same conclusion. | Another team collects fresh measurements. |
This tutorial targets the middle row. A notebook analysis is reproducible when a person who has your project folder can recreate the environment, run the notebook, and get the same numbers, tables, and figures. "Same" needs a definition. For text output and integer counts, the result should match exactly. For floating-point values, tiny differences in the last digits can come from a different CPU or library build. You decide in advance which differences matter. A plot that looks identical is a weaker guarantee than a table of values that match to a stated tolerance.
Reproducibility has a practical payoff that is easy to overlook. The most common person who needs to reproduce your work is you, six months from now, when a reviewer asks a question or a number looks wrong.
Why notebooks drift
A notebook is a document that mixes code, output, and prose. It is also a live session with a running Python process, called the kernel. The kernel keeps variables, imported modules, and open files in memory. The saved .ipynb file stores the code and the outputs, but it does not store that memory. This gap between the saved file and the live session is the root of most drift.
"Drift" here means that the results you see in the notebook slowly stop matching what the code would produce from a clean start. Five sources account for nearly all of it.
- Hidden state. Cells can run in any order, and variables persist. If you delete a cell that defined
threshold, the name still exists in the kernel, so later cells keep working. A fresh run fails or gives a different answer. - Execution order. The numbers beside the cells (
In [7]) show the order in which cells ran, which may not be the order on the page. A reader who runs top to bottom executes a different program than the one that produced the saved outputs. - Environment. The result depends on the Python version, package versions, and system libraries. A package upgrade can change a default, a random algorithm, or a rounding rule.
- Randomness. Sampling, shuffling, train/test splits, and many optimizers use random numbers. Without a fixed seed, each run differs.
- Data. The input file may be edited, replaced, or re-downloaded with new rows. Paths may point to a file that only exists on your laptop.
Notebooks make all five easy to hit because the interactive workflow rewards quick edits and immediate output. Nothing forces you to start clean.
A small demonstration of hidden state
The following three cells show how a notebook can display a result that its own code cannot produce.
# Cell 1
data = [4, 8, 15, 16, 23, 42]# Cell 2
cutoff = 10# Cell 3
kept = [x for x in data if x > cutoff]
print(len(kept), sum(kept))Run in order, cell 3 prints 4 96. Now suppose you decide the cutoff should be 15, and you edit cell 2 to say cutoff = 15 but forget to rerun it. The kernel still holds cutoff = 10. The page shows 15, and the output shows results for 10. A reader sees a mismatch they cannot explain.
The reverse is also common. You delete cell 2 entirely and rerun cell 3. It still prints 4 96, because cutoff lives on in memory. Someone who opens the saved notebook and runs it fresh gets a NameError.
The cost of drift
Drift usually costs time rather than causing a dramatic failure, but it can also change conclusions.
- A colleague cannot run your notebook, so they ask you to explain it, and you cannot remember the order of steps.
- A model's accuracy changes between runs, and you cannot tell whether a code change helped or the random split did.
- A reviewer asks for a figure to be regenerated with one change, and the rest of the figures shift slightly.
- A package update changes a default argument, and a published number differs from what the notebook now prints.
Each case has a different cause, which is why the fix is a set of habits rather than one tool.
The reproducibility checklist this tutorial builds
The remaining chapters map to a set of controls. Each one closes one source of drift.
| Source of drift | Control | Where it appears in the tutorial |
|---|---|---|
| Hidden state, execution order | Restart the kernel and run all cells in order | Next two chapters |
| Scattered files and paths | A project layout that separates code, data, and outputs | Project layout chapter |
| Environment | Virtual environments and pinned dependencies | Environment chapters |
| Randomness | Explicit seeds | Seeding chapter |
| Data changes | Checksums and recorded sources | Data provenance chapter |
| Large, messy notebooks | Reusable modules, parameters, readable Git diffs | Later chapters |
| Unverified runs | Command-line execution, run metadata, automated checks | Later chapters |
| System-level differences | Containers, when needed | Final chapter |
You will not need every control for every project. A quick exploration for your own use needs fewer than a result you will publish or hand to a team. The point is to know which risk each control addresses so you can choose deliberately.
Edge cases and limits
Some things stay outside what these controls can fix, and it helps to know them early.
- Floating-point differences. The same code can give results that differ in the last digits across CPU types, operating systems, or math library builds. Compare with a tolerance (for example,
numpy.allclose) rather than expecting bit-identical output, unless you have a reason to demand it. - Parallel and GPU code. Multithreaded reductions and some GPU operations add numbers in an order that varies from run to run. Seeding does not remove this. Libraries often offer a deterministic mode with a speed cost.
- External services. If a notebook calls a live API or reads from a database that changes, the input changes with it. The fix is to save a snapshot of what you received.
- Time and environment reads. Code that uses the current date, your username, or an environment variable gives different results on different days and machines.
- Unavailable old packages. A pinned version may be removed from a package index or may not build on a new operating system. Recording versions helps, but long-term archival may need a container or a saved copy of the packages.
A notebook can be reproducible without being fast, tidy, or elegant. The goal is that the path from inputs to outputs is explicit and can be followed by someone else.
A first quick test
You can measure how reproducible a notebook is right now. In Jupyter, choose the menu action that restarts the kernel and runs all cells. Then compare the new outputs to the old ones. If a cell errors, you found hidden state. If numbers differ, you found randomness, data changes, or state that depended on run order. Later chapters turn this into a routine. For now, run it once on a notebook you already have and write down every difference you see.
Exercise
A notebook shows cutoff = 15 in cell 2 and prints results that match a cutoff of 10 in cell 3. Name the cause, and name the quickest check that reveals it.
Show answer
The cause is hidden state. The kernel still holds the old value cutoff = 10 because cell 2 was edited but not rerun. Restarting the kernel and running all cells in order reveals it, because the fresh run uses 15 and the output changes or the mismatch disappears.
Exercise
Your colleague runs your notebook on their laptop and gets a model accuracy of 0.842 where you saw 0.847. Which of the five sources of drift could explain this? List at least three and say how you would tell them apart.
Show answer
Randomness (no fixed seed for the train/test split or model), environment (a different version of a library that changed an algorithm or default), and data (a different copy of the input file). Possible checks: set a seed and compare, compare package versions with pip freeze or a printed version list, and compare file checksums. Hidden state and run order are also possible if the saved notebook was not produced by a clean top-to-bottom run.