Reproducibility: Seeds, Configs, and Environments
random.seed(), np.random.seed(), YAML config files, environment pinning with pip freeze.
Reproducibility: Seeds, Configs, and Environments is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Reproducibility?
An experiment you cannot reproduce is not science. If running the same code twice gives different results, you cannot trust comparisons or debug regressions.
Three pillars make AI work reproducible: fixed random seeds, externalized configs, and pinned environments.
Sources of Randomness
Randomness sneaks in from many places: data shuffling, train/test splits, weight initialization, dropout, and augmentation. Each may use a different random generator.
To reproduce a run you must seed every generator your code touches.
Seeding Python and NumPy
Fix the standard library and NumPy generators with a single chosen seed (42 is a common convention).
import random
import numpy as np
random.seed(42)
np.random.seed(42)Seeding Frameworks
ML frameworks have their own generators. Seed them too, and pass the seed to functions that accept random_state.
import numpy as np
from sklearn.model_selection import train_test_split
X_train, X_test = train_test_split(X, test_size=0.2, random_state=42)
# torch.manual_seed(42) / tf.random.set_seed(42) for deep learningA set_seed Helper
Wrap all seeding in one function and call it at the top of every script — so you never forget a generator.
import random, os
import numpy as np
def set_seed(seed=42):
random.seed(seed)
np.random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
set_seed(42)Hardcoded Hyperparameters Are a Trap
Scattering 0.01, 200, 0.2 through your code makes runs hard to track and change. Was that learning rate 0.01 or 0.001 in the run that scored best?
The fix: put all hyperparameters in a config file, not in code.
YAML Config Files
YAML is a human-readable format ideal for configs. One file captures every knob for a run.
# config.yaml
seed: 42
model:
name: random_forest
n_estimators: 200
max_depth: 8
training:
test_size: 0.2
data:
path: data/processed/train.csvReading YAML with PyYAML
Load the config at the start of your script with PyYAML. Now your code reads values from cfg instead of hardcoding them.
import yaml
with open("config.yaml") as f:
cfg = yaml.safe_load(f)
print(cfg["model"]["n_estimators"]) # 200
set_seed(cfg["seed"])Passing Config Into Code
Functions take the config (or its values) as arguments. To run a new experiment you change the YAML, not the Python.
from sklearn.ensemble import RandomForestClassifier
m = cfg["model"]
model = RandomForestClassifier(
n_estimators=m["n_estimators"],
max_depth=m["max_depth"],
random_state=cfg["seed"],
)Pinning the Environment
Different library versions produce different results. Pin exact versions so others (and future you) install the same stack.
# requirements.txt with pinned versions
pandas==2.2.0
scikit-learn==1.4.0
numpy==1.26.4
# generate it with:
# pip freeze > requirements.txtVirtual Environments
Isolate each project so its pinned versions do not clash with other projects. Create and activate a virtual environment, then install from the pinned file.
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txtQuick Check: Hyperparameters
You want to try ten different hyperparameter combinations and keep each run reproducible and traceable.
Recap: Reproducibility
You learned the three pillars of reproducible AI:
- Seeds:
random.seed(42),np.random.seed(42), framework seeds, andrandom_state - Configs: all hyperparameters in
config.yaml, loaded with PyYAML - Environments: pinned
requirements.txtin a virtual environment
Change the config, not the code. Next: Jupyter notebook best practices.
Frequently asked questions
Is the “Reproducibility: Seeds, Configs, and Environments” lesson free?
Yes — the full text of “Reproducibility: Seeds, Configs, and Environments” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Reproducibility: Seeds, Configs, and Environments”?
random.seed(), np.random.seed(), YAML config files, environment pinning with pip freeze. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reproducibility: Seeds, Configs, and Environments” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Professional AI Project Directory Structure
- Git for AI Projects
- Reproducibility: Seeds, Configs, and Environments
- Jupyter Notebooks Best Practices