#195 Machine learning and data science 6.3 min
Recommender From Scratch: Watch 24 Films Find Their Genres
A program learned three numbers for each of 24 made-up films, and films of one genre ended up together in 3D.
Recommend for a different person
Copy the code folder first. Run python run.py recommend. Then in run.py change favourite[3] to favourite[4], top_three(3) to top_three(4), and the words person 3 in the print to person 4, and run it again. You should see that person 4 likes romance and the top three unseen films are Second Orchard, Quiet Goodbyes and Lantern Post.
A hint
There are three small edits, all in the last block of run.py.
Turn off the penalty
In run.py change the train call to train(ratings, 60, len(FILMS), reg=0.0, on_epoch=record) and run python run.py train. Predict first whether the test error at epoch 60 will be lower or higher than 0.78. You should see the training error fall to 0.41 while the test error ends at 0.93.
A hint
reg is the pull toward zero on every number.
Use one number instead of three
In run.py change the train call to train(ratings, 60, len(FILMS), k=1, on_epoch=record). Run python run.py train and then python run.py recommend. You should see the test error end at 1.14, the top three films for person 3 turn into romance films, and the favourite-genre count drop to 99 of 180.
A hint
k is how many numbers each person and each film gets. The video used 3 so it could draw them.
Take a much bigger step
In run.py change the train call to train(ratings, 60, len(FILMS), lr=0.2, on_epoch=record) and run python run.py train. The learning rate was 0.02. You should see that training does not settle: the training error stays near 0.6 instead of reaching 0.46, and the test error moves between 0.77 and 0.90 across the printed epochs.
A hint
lr is the size of each nudge.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
Made-up ratings with genres built in, so the result is easy: 60 people, 24 films, three factors chosen for the picture, 120 test ratings from one split. No real ratings file, no alternating least squares, no implicit feedback, no cold start. The 3D picture is drawn from the real numbers of this run.
Up next in Machine learning and data science
More in Machine learning and data science
- #182 Linear Regression From Scratch, Then scikit-learn
- #183 Precision, Recall, F1: Which Metric Matters?
- #184 Overfitting You Can See: Train vs Validation Error
- #185 Cross-Validation Done Right: The Leakage Mistake
- #186 Data Leakage: The Bug That Fakes Accuracy, Reproduced
- #187 Feature Engineering Beat 500 Trees, on Made-Up Data
Related videos
- #196 A Neural Network From Scratch in NumPy, Gradient Checked Machine learning and data science
- #01 Local AI Agent in Python: No API Key (Ollama) AI agents and MCP
- #02 RAG From Scratch in Python: Chunk, Embed, Retrieve (Local) RAG and vector search
- #03 pgvector vs Chroma vs Qdrant: Same Data, Real Results RAG and vector search