Sub topic 4
Where Notebooks Fit in a Machine Learning Project
Assume you know basic Python modules, functions, the command line, Git, and how to train a model with a library such as scikit-learn or PyTorch. This chapter focuses on how notebooks work with the rest of a machine learning repository.
A notebook is a document that combines code cells, their output, explanatory text, and visualizations. It is useful for exploration because you can run one step, inspect the result, and change the next step without running the entire program.
A notebook should usually be a client of the project code, not the home of that code. Reusable operations belong in an importable source package. The notebook then chooses inputs, calls those operations, and displays results.
Use notebooks for exploration and communication
Notebooks work well for tasks such as:
- Inspecting a new dataset.
- Plotting distributions and sample predictions.
- Comparing metrics from several completed runs.
- Investigating an unexpected error.
- Explaining a result to another person.
For example, a notebook might load a processed dataset and call functions from my_project:
from pathlib import Path
import pandas as pd
from my_project.data.prepare import make_features
from my_project.train import evaluate_model, train_model
data_path = Path("data/processed/train.csv")
frame = pd.read_csv(data_path)
features, target = make_features(frame)
model = train_model(features, target, learning_rate=0.001)
metrics = evaluate_model(model, features, target)
print(metrics)The notebook contains the experiment-specific choices. The package contains make_features, train_model, and evaluate_model. If you fix a bug in feature preparation, every notebook can use the fixed implementation after the package is reloaded or the notebook kernel is restarted.
Keep notebook code from becoming hidden application code
Notebook code often starts as a quick experiment. It becomes difficult to maintain when it includes duplicated preprocessing, hard-coded paths, model definitions, and training loops.
Use this rule:
| Location | Best use |
|---|---|
| Notebook | Questions, plots, comparisons, and short orchestration code |
| Source package | Reusable transformations, training logic, evaluation, and command-line entry points |
| Configuration file | Paths, hyperparameters, and other values that change between runs |
A notebook should be understandable when its cells run from top to bottom. Avoid relying on variables created in an earlier, unrelated run. Restart the kernel and run all cells before sharing a notebook.
Make notebook results reproducible
Record the dataset version, configuration, code version, and random seed used for an important result. Do not rely on saved cell output as the only record of a metric. A later user must be able to rerun the code and determine which inputs produced the output.
Keep large datasets and model files outside the notebook. Store notebooks in a directory such as notebooks/, and use clear names such as 01_inspect_data.ipynb or compare_models.ipynb. Clear old output before committing when the output contains sensitive data or makes the file unnecessarily large.
Exercise
You wrote a preprocessing function in a notebook and now need it in three training jobs. Where should the function live, and what should the notebook do?
Show answer
Move the function into the importable source package. The notebook should import it, provide the selected inputs, and display or analyze its results.