Tastes From Both Sides

Recommender SystemsHard+30 XP+30

Tastes From Both Sides

The Matrix Factorisation lesson fitted every person's and every item's taste scores with gradient descent. There is another way to fit them, with no learning rate at all.

Freeze one side. Suppose the recipes' taste scores are fixed. Then each customer's predicted ratings are dot products of their unknown scores with numbers you already know. That is a linear regression: the recipes they rated are the rows, the recipes' scores are the features, and their ratings are the targets. So each customer's best scores can be solved for directly. Then freeze the customers and solve for each recipe the same way. Repeating these two half-steps is called alternating least squares.

A meal-kit company tries it. ratings is the user-item matrix: one row per customer, one column per recipe, ratings from 1 to 5, and None for a blank cell. recipe_scores holds each recipe's starting scores on kkk hidden tastes, one list of kkk numbers per recipe.

To stop scores from blowing up for customers with very few ratings, each solve adds a penalty λ\lambdaλ, just as ridge regression does. For one customer, using only the recipes they rated, their new score vector p\mathbf{p}p solves

(∑iviviT+λI) p=∑iri vi\Big(\sum_{i} \mathbf{v}_i \mathbf{v}_i^{\mathsf T} + \lambda I\Big)\,\mathbf{p} = \sum_{i} r_{i}\,\mathbf{v}_i(i∑​vi​viT​+λI)p=i∑​ri​vi​

where vi\mathbf{v}_ivi​ is rated recipe iii's score vector, rir_iri​ is the customer's rating of it, and III is the k×kk \times kk×k identity matrix. The matrix on the left is k×kk \times kk×k: its entry in row aaa, column bbb is ∑ivi,a vi,b\sum_i v_{i,a}\, v_{i,b}∑i​vi,a​vi,b​, plus λ\lambdaλ when a=ba = ba=b. So this is a system of kkk linear equations in kkk unknowns. A recipe's update is the same equation with the roles swapped: sum over the customers who rated it, using their score vectors and their ratings of it.

One sweep is:

  1. update every customer's scores, using the current recipe scores, then
  2. update every recipe's scores, using the customer scores just computed.

Task: write als(ratings, recipe_scores, lam, sweeps) that runs sweeps sweeps and returns (customer_scores, recipe_scores): two lists of lists of plain Python floats, every number rounded to 4 decimal places.

  • A blank cell is not a rating. Leave it out of every sum.
  • λ>0\lambda > 0λ>0 always, so every system has exactly one solution, even for a customer who has rated nothing.

With k=1k = 1k=1, every system is a single equation. Customer 0 rated recipe 0 (5 stars) and recipe 2 (1 star), so (1.02+0.22+0.5) p=5×1.0+1×0.2(1.0^2 + 0.2^2 + 0.5)\,p = 5 \times 1.0 + 1 \times 0.2(1.02+0.22+0.5)p=5×1.0+1×0.2, giving p=5.2/1.54=3.3766p = 5.2 / 1.54 = 3.3766p=5.2/1.54=3.3766.

Examples

ratings = [[5, None, 1], [4, 2, None]]
recipe_scores = [[1.0], [0.5], [0.2]]
lam = 0.5
sweeps = 1
returns ([[3.3766], [2.8571]], [[1.411], [0.6596], [0.2837]])
ratings = [[5, 3, None, 1], [4, None, None, 1], [1, 1, None, 5], [None, 1, 5, 4]]
recipe_scores = [[1.0, 0.2], [0.8, 0.1], [0.1, 1.0], [0.2, 0.9]]
lam = 0.1
sweeps = 1
returns ([[4.1972, 0.2805], [3.5628, 0.3644], [0.3206, 4.8464], [0.6488, 4.4096]], [[1.1483, 0.1294], [0.6995, 0.1435], [0.1625, 1.1043], [0.1738, 0.9555]])

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run