← All Base Form ML projects
BL-04NOT STARTEDBASE FORM · CLASSICAL MLMLPYTHON

Recipe recommender

Pick a recipe you like and get ten similar ones, using nearest neighbours over ingredients.

EST.5 h
REWARD+100 XP
AFTERoriginal

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
recall@10; queries per second
GOAL · identical neighbours
sklearn NearestNeighbors—
no results yet
YOUR RESULTS

Your recorded results appear here.

Brief

Recommendation systems are everywhere, and the simplest good one is "find things like this". You'll build that for recipes. Pick a recipe you like and the tool returns the ten recipes that use the most similar ingredients.

The data is "Food.com Recipes and Interactions" on Kaggle. RAW_recipes.csv has about 230,000 recipes, each with a list of ingredients, and RAW_interactions.csv has user ratings you can use for a side quest. Ingredient names are messy free text, so some cleaning comes first.

The method is k-nearest neighbours (kNN). Turn every recipe into a vector with one entry per ingredient, then measure how close two vectors are. Here you use cosine similarity, which compares the angle between vectors:

cos⁡(a,b)=a⋅b∥a∥ ∥b∥.\cos(a, b) = \frac{a \cdot b}{\lVert a \rVert \, \lVert b \rVert}.

If you scale every vector to length 1 first, cosine similarity is just the dot product, and scoring a query against every recipe is one matrix-vector product. sklearn uses cosine distance, which is 1−cos⁡(a,b)1 - \cos(a, b), so the ranking is the same. Common ingredients like salt and butter would dominate the match, so you weight each ingredient by its inverse document frequency, idf(w)=log⁡Ndf(w)\text{idf}(w) = \log \frac{N}{\text{df}(w)}, where df(w)\text{df}(w) is the number of recipes that use ingredient ww and NN is the number of recipes.

VISUALFig. 1
TOMATO WEIGHT → ↑ SUGAR WEIGHT query Asimilar direction Bnearer tip, wide angle
Recipe B is nearer to the query by straight-line distance, but recipe A points the same way. Cosine picks A. Why it's here: Shows why the brief uses cosine: it matches the mix of ingredients, not how many there are.
HINT · OBJECTIVE 3
Stuck? Hints stay hidden until you ask.
HINT · OBJECTIVE 4
Stuck? Hints stay hidden until you ask.

DATA · a public recipe dataset

Objectives

0 / 6 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. Recall@10 against sklearn NearestNeighbors with cosine distance is 1.0 on 1,000 random queries (ties broken by index on both sides). Record your batched queries per second with `python tools/mlarena.py record BL-04 "<your queries/s>"`.
  2. The query recipe itself is excluded from its own results.
  3. The README reports queries per second for your single, batched and sklearn searches, and shows three example recommendations you think are good and one you think is bad, with a reason.

Side quests

Compare cosine with Jaccard similarity on the 0/1 vectors. Do the top ten change much?
Use RAW_interactions.csv to hide one highly rated recipe per user and check how often it shows up in the neighbours of that user's other favourites.

Terms in plain words

Nearest neighbours
Answering a query by finding the stored items closest to it under some distance.
Cosine similarity
The cosine of the angle between two vectors. It compares direction and ignores length, so a long recipe and a short one with the same mix still match.
TF-IDF weighting
Giving rare ingredients more weight than common ones, so sharing saffron counts for more than sharing salt.
Sparse matrix
A matrix stored as only its non-zero entries. Each recipe uses about ten of thousands of ingredients, so most entries are zero.
Recall@10
Of the true ten nearest neighbours, the fraction your search returned. 1.0 means you found all ten.