Three.js Recommendation Engine (Collaborative Filtering)

Collaborative filtering with matrix factorisation in Three.js, as SGD learns user and item vectors from a sparse ratings matrix, plotted live in a 3D latent space with predicted ratings filling the matrix and top-5 recommendations for each user.

Key topics: collaborative filtering, matrix factorization, recommendation system, funk svd, latent factors, three.js machine learning

What this code does

- Data: 48 users × 36 items with genre-driven tastes; only a fraction of ratings are observed, and a further 8% are held out for testing.
- Model: rating ≈ μ + bᵤ + bᵢ + uᵤ·vᵢ, a global mean, per-user and per-item biases and a 3-factor dot product.
- Training: SGD over observed ratings with L2 regularisation; held-out RMSE shows generalisation and rises when regularisation is too low.
- Latent Space: with k = 3 the learned vectors plot directly, so same-genre users and items cluster and align without being told genres.
- Recommendations: the highlighted user's lines point to the five unseen items with the highest predicted rating.
- Controls: observed fraction, learning rate, regularisation, epochs per second, prediction fill and user.

Continue Learning

JavaScript Implementation

ES6+
// Collaborative filtering by matrix factorisation: rating(u, i) ≈ μ + b_u + b_i + U[u]·V[i]
for (let epoch = 0; epoch < 50; epoch++) {
  for (const { u, i, r } of shuffle(ratings)) {
    const e = r - (mu + bu[u] + bi[i] + dot(U[u], V[i]))
    bu[u] += lr * (e - reg * bu[u])
    bi[i] += lr * (e - reg * bi[i])
    for (let k = 0; k < K; k++) {
      const pu = U[u][k]
      U[u][k] += lr * (e * V[i][k] - reg * pu)
      V[i][k] += lr * (e * pu - reg * V[i][k])
    }
  }
}
// Recommend: unseen items with the highest predicted rating; plot U and V as points in 3D