This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
pip install -e '.[dev]'# Run all tests
pytest
# Run specific test suite
pytest tests/unit/
pytest tests/integration/
pytest tests/exercises/
# Run exercise-specific tests
python scripts/run_exercise.py --chapter 3 --exercise 1 --variant solution
pytest topics/ch03_gradient_descent/exercises/ex01_unified_optimizer/test_solution.py# Check formatting
ruff check .
# Apply formatting
ruff check --fix .
# Type checking
mypy src/src/ml_from_scratch/- Maintained algorithm implementations- Each algorithm has its own module (optimization, linear_models, naive_bayes, etc.)
- Shared utilities in validation.py, random.py, datasets/, metrics/, preprocessing/
- Production-ready interfaces in pipeline/ and production/
topics/chXX_topic/- Chapter-specific topic directoriestopics/chXX_topic/exercises/exNN_slug/- Individual exercise directoriesstarter.py- Starting point with TODOssolution.py- Complete, tested solutiontest_solution.py- Focused tests for the exerciseREADME.md- Exercise description, learning objectives, setup instructionsexpected/- Small expected outputs (when needed)config.yaml- Exercise-specific configuration (when useful)
scripts/- Helper scripts for exercise managementlist_exercises.py- List all exercisesrun_exercise.py- Run starter/solution variantscheck_exercise_coverage.py- Verify all 83 exercises are presentcheck_book_routes.py- Validate manuscript code referencesregenerate_figures.py- Generate plots from versioned scripts
tests/- Test suitesunit/- Mathematical kernel testsintegration/- Chapter workflow testsinvariants/- Property-based testsparity/- Library comparison testssmoke/- Basic execution tests for each exercise
docs/- Documentationindex.md- Main documentation landing pagetopic-index.md- Browse by topicexercise-index.md- Browse by exerciseinstallation.md- Setup instructionsdata-policy.md- Data usage guidelinescontributor-guide.md- Contribution process
data/- Datasetsraw/- Original, versioned datasetsgenerated/- Preprocessed data (not committed)
figures/generated/- Generated plots (not committed)
All estimators follow a consistent interface:
fit(X, y=None) -> selfpredict(X) -> predictions or labelspredict_proba(X) -> probabilities (when applicable)fit_predict(X) -> labels (for clustering)score(X, y=None) -> metric score
- Use
numpy.random.default_rng(random_state)for all stochastic operations - Document seeds in all exercises and examples
- Never shuffle X and y independently
- Notebooks should be thin: explain, visualize, and call maintained modules
- Not contain core algorithm logic (that belongs in src/)
- Execute from clean checkout when optional dependencies are installed
- Valid JSON format
# List all exercises
python scripts/list_exercises.py
# Run exercise starter
python scripts/run_exercise.py --chapter 3 --exercise 1 --variant starter
# Run exercise solution
python scripts/run_exercise.py --chapter 3 --exercise 1 --variant solution
# Check exercise coverage
python scripts/check_exercise_coverage.py
# Validate book routes
python scripts/check_book_routes.py
# Regenerate figures
python scripts/regenerate_figures.py# Install development dependencies
pip install -e '.[dev]'
# Run linter
ruff check .
# Run formatter
ruff check --fix .
# Run type checker
mypy src/
# Run full test suite
pytest
# Run exercise tests specifically
pytest tests/exercises/The repository maintains one implementation per algorithm in src/ml_from_scratch/:
- Optimization algorithms:
optimization.py - Linear models:
linear_models/(linear_regression.py, logistic_regression.py, etc.) - Naive Bayes:
naive_bayes/ - SVM:
svm/ - Decision trees:
trees/ - Clustering:
clustering/(kmeans.py, gmm.py) - Neural networks:
neural_networks/andpinn/ - Pipeline utilities:
pipeline/andproduction/
Exercises import from these maintained modules rather than duplicating implementations.
Each exercise follows this structure:
-
README.md - Complete description with:
- Chapter, exercise number, name, difficulty
- Learning objectives (observable skills)
- Complete task description from manuscript
- Starter guide (explains TODOs without revealing solution)
- Exact run commands
- Acceptance criteria (numerical tolerances, required outputs)
- Common pitfalls to avoid
- Further exploration (optional, clearly labeled)
-
Starter - Runs far enough to expose TODOs, includes:
- Focused TODO markers (not empty files)
- Small deterministic data or documented loader
- Actionable errors for missing prerequisites
-
Solution - Complete, readable, tested implementation that:
- Reuses maintained modules from src/
- Explains non-obvious mathematical choices
- Preserves intended difficulty (doesn't trivialize with library calls unless comparison is the task)
- Reports actual results (not manuscript expectations)
-
Tests - Each solution has:
- At least one focused correctness test
- One relevant edge case or invariant test
- For stochastic tests: fixed seeds with tolerant assertions
- For plotting tests: data/metric validation + file generation check
- For performance tests: hardware-tolerant checks with environment recording
Before considering work complete:
- All 83 exercises present exactly once (no Chapter 13 duplicate)
- Every exercise has documented starter, solution, and passing tests
- Formatting, linting, and type checks pass
- At least one representative solution from each topic executes successfully
- All notebooks are valid JSON and representative ones execute
- All active manuscript code routes resolve
- Representative figures regenerate correctly from scripts
- Book compiles cleanly after any route/code-snippet edits
- No credentials or private data committed
- Dependency groups documented (core vs optional heavyweight)
This structure allows learners to progress naturally from book equations → tested implementation → runnable example → guided starter → complete solution → clear verification without encountering duplicated logic, missing routes, or insecure access.