1 / 11
Build a complete recommender system in Python and PyTorch: interaction data, temporal splits, baselines, ranking metrics (precision, recall, MAP, NDCG), coverage and diversity, content-based models with TF-IDF and embeddings, user and item kNN, matrix factorization in NumPy and PyTorch, implicit…
Build, Evaluate, and Serve Recommendations with Python and PyTorch
Read before you buy: the PDF here is a free sample — the first 20 pages of the 445-page book (payments, sales and reviews on this demo site are sample data). Get the full e-book at thenexbooks on Gumroad.
Learn how real recommender systems are built, evaluated, and shipped — not just how to train one model on a ratings file.
"Recommended for you" and "Customers who bought this also bought" look simple from the outside. Behind them is a pipeline: interaction logs, a split that does not leak the future, baselines that are surprisingly hard to beat, models that turn behavior into scores, a ranking step, business rules, and a service that answers in milliseconds. This book walks through that whole pipeline with code you write and run yourself.
Every model is implemented from scratch in plain Python, NumPy, scikit-learn, and PyTorch, every metric is computed by code in the book, and every number comes from running that code on a seeded dataset you generate yourself. When the book says one model beats another, it shows you the table — and when a simple baseline wins, it says so.
What's inside
- How recommendation problems are framed: explicit ratings vs implicit events, the user–item matrix, sparsity, popularity, and the long tail
- A seeded synthetic data generator for users, books, views, wishlists, purchases, and ratings, so your results match the book's
- Temporal train/validation/test splits, evaluation sets, and the leakage traps that make offline results look better than they are
- Baselines first: random, all-time popular, and trending lists
- Ranking metrics written from scratch: precision, recall, hit rate, MAP, and NDCG at k, plus catalog coverage, diversity, novelty, and popularity bias
- A reusable evaluation harness, and how offline metrics relate to A/B tests, interleaving, and guardrail metrics
- Content-based recommendations with TF-IDF and with sentence embeddings, and "more like this" lists
- User-based and item-based collaborative filtering with k-nearest neighbors, and weighted hybrids
- Matrix factorization by SGD in NumPy and in PyTorch, why rating prediction (RMSE) is not ranking, implicit ALS, and Bayesian Personalized Ranking with negative sampling
- Tuning and debugging factor models on a validation window
- Sequence and session recommendations, a two-tower retrieval model with in-batch softmax and logQ correction, and a gradient-boosted ranking stage
- Cold start for new users and new books, diversity re-ranking (MMR and genre caps), fairness and business rules
- Serving: batch precompute vs real-time, nearest-neighbor search, a FastAPI recommendation service with tests, monitoring, feedback loops, and retraining
One running example, start to finish. You build the recommender for Pagewell Books, a fictional online bookstore with 2,000 customers, 1,200 books in twelve genres, and a year of browsing and purchase events. You start with a popularity list and finish with a two-stage pipeline, cold-start fallbacks, diversity rules, and an API that serves precomputed recommendations — all evaluated on the same temporal split with the same code, so every comparison is fair.
Practical and honest. The examples target Python 3.12 and PyTorch 2.x and run on an ordinary CPU in seconds to a few minutes. GPU usage is explained but was not part of the book's tests. The book contains no invented benchmarks or statistics; all metric values come from the book's own code and data. Every chapter includes runnable code, Tips, Notes, Warnings, exercises, and Key Takeaways, and the appendices give you a cheat sheet, templates and reference tables, a production checklist, and a glossary.
Format: PDF, 445 pages (A4), linked table of contents and bookmarks. English. First Edition, 2026. Published by thenexbooks. Basic Python knowledge is assumed; no prior recommender-systems or deep-learning experience is required.
Table of Contents
Part I — Foundations
- Ch 1 · What Recommender Systems Do: Pagewell Books and the Shape of the Problem
- Ch 2 · Interaction Data: Ratings, Implicit Events, and the User–Item Matrix
- Ch 3 · The Pagewell Dataset: A Seeded Synthetic Generator
- Ch 4 · Train/Test Splits Without Leakage: Temporal Splits and Evaluation Sets
- Ch 5 · Baselines That Are Hard to Beat: Random, Popularity, and Trending
Part II — Evaluation
- Ch 6 · Ranking Metrics: Precision, Recall, Hit Rate, MAP, and NDCG at k
- Ch 7 · Beyond Accuracy: Coverage, Diversity, Novelty, and Popularity Bias
- Ch 8 · An Evaluation Harness: Comparing Models Fairly and Reproducibly
- Ch 9 · Offline vs Online: A/B Tests, Interleaving, and Guardrail Metrics
Part III — Content and Neighbors
- Ch 10 · Content-Based Recommendations with TF-IDF
- Ch 11 · Text Embeddings for Content-Based Recommendations
- Ch 12 · User-Based Collaborative Filtering with k-Nearest Neighbors
- Ch 13 · Item-Based Collaborative Filtering: Readers Who Bought This Also Bought
- Ch 14 · Hybrid Recommenders: Blending Content, Neighbors, and Popularity
Part IV — Matrix Factorization
- Ch 15 · Matrix Factorization from Scratch in NumPy
- Ch 16 · Matrix Factorization in PyTorch: Embeddings, Biases, and Training Loops
- Ch 17 · Implicit Feedback and Alternating Least Squares
- Ch 18 · Bayesian Personalized Ranking and Negative Sampling
- Ch 19 · Tuning and Debugging Factorization Models
Part V — Deep and Sequential Models
- Ch 20 · Sequence and Session Recommendations: From Markov Chains to Next-Item Models
- Ch 21 · Two-Tower Retrieval Models in PyTorch
- Ch 22 · The Ranking Stage: Candidate Features and a Learned Ranker
- Ch 23 · Cold Start: New Users, New Books, and Fallbacks
Part VI — Production
- Ch 24 · Diversity, Fairness, and Business Rules: Re-Ranking the Final List
- Ch 25 · Serving Architecture: Batch Precompute, Real-Time APIs, and Nearest-Neighbor Search
- Ch 26 · A Recommendation API with FastAPI
- Ch 27 · Monitoring, Feedback Loops, and Retraining
- Ch 28 · Case Study: The Pagewell Recommender from Data to Production
Appendices
A · Recommender Systems Cheat Sheet
B · Templates and Reference
C · Production Checklist
D · Glossary
Book details
| Detail | |
|---|---|
| Pages | 445 |
| Format | PDF (A4) |
| Language | English |
| Level | Beginner to Intermediate |
| Edition | 1st Edition (2026) |
| Download on this site | Free sample, first 20 pages (PDF) |
| Publisher | thenexbooks |
Files you get
No reviews yet. Only buyers can review.
More from thenexbooks