Foundations to Frontier
Tests
D. Classical Machine Learning › D13 Recommender Systems

User-Based Collaborative Filtering

Collaborative filtering recommends from the behaviour of many users rather than from item descriptions. In its oldest, user-based form (GroupLens, 1994), the system predicts how a user would rate an item from the ratings of the k users most similar to them, measured by Pearson correlation of their mean-centred ratings over the items both have rated, and recommends the items with the highest predicted ratings. It needs no item features, captures taste that descriptions miss, and can surprise users with items outside their usual profile. Its weaknesses are just as characteristic: ratings matrices are 95–99% empty, so most pairs of users share few or no items and their similarities are noisy; new users and new items have nothing to compare; and computing user–user similarities scales poorly. This page covers mean-centring, Pearson and cosine similarity, neighbourhood selection, the prediction formula, significance weighting, evaluation by RMSE against baseline predictors, sparsity, cold start and scalability. In the exercises you implement user-based k-NN and compare it with bias baselines; then you measure how density, neighbourhood size, similarity measure and shrinkage change its accuracy.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Ask people with the same taste

When choosing a film, you might ask a friend whose taste matches yours: if they loved it, you probably will too. User-based collaborative filtering automates this. It finds the users whose past ratings agree most with yours, then predicts your rating for a new item from how those look-alikes rated it. It never needs to know what the item is about, only who liked what.

User-based CF: predict a rating from similar users
ApolloBridgesCometsDunesEnginesFjordsGlidersHarboursIceJetsAna544.415214Ben4542125Cy4514212Dee12514542Eli21425541Fay5452114Gus125445Hal33332433
Fay: similarity 0.96 (over 4 co-rated) · rated Comets 4 = mean 3.14 + 0.86
Cy: similarity 0.89 (over 5 co-rated) · rated Comets 5 = mean 2.71 + 2.29
Ben: similarity 0.80 (over 4 co-rated) · rated Comets 4 = mean 3.29 + 0.71
Gus: similarity -0.82 (over 4 co-rated) · rated Comets 2 = mean 3.50 − 1.50
Eli: similarity -0.84 (over 5 co-rated) · rated Comets 1 = mean 3.00 − 2.00
prediction for Ana on Comets: 3.14 (their mean) + weighted deviation = 4.44
Ana and Fay share a taste for the space and engineering films and dislike the nature ones; Dee, Eli and Gus are the opposite. Negative similarity counts as evidence in the opposite direction. Neighbours with very few co-rated films can show extreme similarities by chance, which is what shrinking by overlap corrects. With real data the matrix is 95–99% blank, which is the main weakness of user-based methods.
Why engineers care

Collaborative signals appear wherever many people interact with many items: technicians choosing procedures, analysts opening dashboards, pilots selecting routes, engineers reusing test cases. User-based CF is the clearest way to understand how “people like you” recommendations work, and why they struggle when data are sparse.

📝 Quick check (Level 0)
0/3 answered
  1. 1. User-based CF predicts from…
  2. 2. Does it need item features?
  3. 3. A typical ratings matrix is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Similarity and prediction by hand

Mean-centring. Users use the scale differently: one gives 4s and 5s, another 2s and 3s. Subtract each user’s mean rating: r̃ᵤᵢ = rᵤᵢ − r̄ᵤ.

Pearson similarity over the co-rated items Iᵤᵥ:

s(u, v) = Σᵢ r̃ᵤᵢ r̃ᵥᵢ / (√Σᵢ r̃ᵤᵢ² · √Σᵢ r̃ᵥᵢ²).

User u rates a, b, c as 5, 3, 4 (mean 4), and v rates a, b, c, d as 4, 2, 3, 5 (mean 3.5). The deviations on a, b, c are (1, −1, 0) and (0.5, −1.5, −0.5), so s = 2/(√2 · √2.75) = 0.853.

Prediction. With the k most similar users v who rated item i:

r̂ᵤᵢ = r̄ᵤ + Σᵥ s(u, v)(rᵥᵢ − r̄ᵥ) / Σᵥ |s(u, v)|.

With r̄ᵤ = 3, neighbours of similarity 0.8 and 0.4 who rated 5 and 2 against their means 4 and 3: 3 + (0.8 · 1 + 0.4 · (−1))/1.2 = 3.33.

Significance weighting. A similarity computed from 2 co-rated items is unreliable. Multiply it by min(n, β)/β: with β = 25 and n = 2, a similarity of 1 becomes 0.08.

Evaluation. Hold out ratings and compute RMSE = √mean(error²) and MAE = mean|error|. Predictions 3, 4, 5 for true 3, 5, 3 give RMSE √(5/3) = 1.29 and MAE 1. Always compare with baselines: the global mean, the user mean, and μ + bᵤ + bᵢ.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Mean-centring removes…
  2. 2. Pearson similarity is computed over…
  3. 3. Significance weighting shrinks similarities based on…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Sparsity, baselines and neighbourhoods

Sparsity. If each user rates 5% of items independently, two users share only about 0.05² of the catalogue: 1 item in 400. Popular items concentrate ratings and create overlap, which is why real data work at all. Similarities based on 1–3 items are mostly noise.

Baselines first. A large part of rating variance is user and item bias: harsh users, good films. The baseline r̂ = μ + bᵤ + bᵢ, with shrunken biases, often beats naive neighbourhood methods. A fair test of CF is whether it improves on that baseline.

Neighbourhood size.

  • Small k uses only the closest users, but is noisy.
  • Large k includes weakly similar users and drifts toward the user mean.
  • Typical good values are 20–50.
  • Requiring a minimum overlap or a minimum similarity helps.

Exercise 1 (300 users × 400 items, 10.3% observed, with popular items rated more often; 2547 held-out ratings). RMSE:

  • global mean 1.069;
  • user mean 1.031;
  • baseline μ + bᵤ + bᵢ 0.950;
  • user-based k-NN (k = 20, Pearson, significance weighting) 0.847.
from surprise import KNNWithMeans, Dataset, accuracy                 # scikit-surprise
algo = KNNWithMeans(k=20, sim_options={"name": "pearson", "user_based": True, "min_support": 3})
🐍 Exercise 1: user-based k-NN
Simulate a sparse rating matrix with popularity-skewed observations, compute Pearson similarities on co-rated items with significance weighting, predict held-out ratings from the k most similar users, and compare RMSE with mean and bias baselines.
📝 Quick check (Level 2)
0/3 answered
  1. 1. With 5% independent density, two users share about … of the items.
  2. 2. A fair test of collaborative filtering is improvement over…
  3. 3. Very large k makes predictions drift toward…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why Pearson and not raw cosine? Cosine similarity on raw ratings, all positive, treats any two users who rate the same items as similar, mostly measuring overlap. Mean-centring (Pearson, or “adjusted cosine”) measures agreement in deviations, that is, taste.

Scalability. All user–user similarities cost O(m² n) with dense operations, or about O(Σᵢ nᵢ²) with sparse ones, where nᵢ is the number of ratings of item i. That grows badly with users, and user neighbourhoods change as users rate more. Item-based CF (the next topic) precomputes item–item similarities, which are more stable.

Cold start and bias. New users have no neighbours. Popular items are over-recommended, since they dominate overlaps. Ratings are missing not at random: users rate what they chose to consume, which biases offline evaluation.

Interview questions.

  • Why might user-based CF fail on your data? Too sparse: few co-rated items per pair, so noisy similarities. Too many users to compute neighbourhoods. Many new users. Use item-based CF, matrix factorisation or hybrids.
  • How do you evaluate a recommender offline? Hold out ratings, preferably by time, and report RMSE or ranking metrics against bias baselines. Beware missing-not-at-random effects; validate online with A/B tests when possible.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Raw cosine on ratings mostly measures…
  2. 2. Ratings are typically missing…
  3. 3. User–user similarity computation scales roughly with…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

When do neighbours help?

History.

  • D. Goldberg et al. (1992) introduced collaborative filtering (Tapestry).
  • P. Resnick et al. (1994) introduced GroupLens and the Pearson-based prediction formula.
  • J. Herlocker, J. Konstan, A. Borchers and J. Riedl (1999) introduced significance weighting and neighbourhood design.
  • Y. Koren (2008, 2010) integrated baselines and neighbourhood models for the Netflix Prize.

Exercise 2. The same simulation at two densities, 6% and 15%. Each cell gives the held-out RMSE without, then with, significance weighting (β = 25).

density similarity k = 5 k = 20 k = 100
6% (baseline 0.926) Pearson 0.966, 0.938 0.944, 0.924 0.958, 0.946
raw cosine 0.968, 1.001 0.944, 0.958 0.943, 0.955
15% (baseline 0.928) Pearson 0.838, 0.825 0.811, 0.810 0.839, 0.836
raw cosine 0.873, 0.974 0.856, 0.932 0.905, 0.926
  • At 6% density, user-based CF could not beat the bias baseline: its best RMSE was 0.924 against 0.926. With so few co-rated items, similarities were too noisy to add information.
  • At 15% density it was clearly useful: 0.810 against 0.928, a 13% reduction.
  • k = 20 was best; k = 5 was noisy, and k = 100 diluted the signal with weak neighbours.
  • Significance weighting helped Pearson, most with few neighbours (0.966 → 0.938 at k = 5, 6%). It hurt raw cosine, whose uncentred similarities mostly reflect overlap.
  • Mean-centred Pearson beat raw cosine everywhere at 15%. Removing each user’s rating level is essential.
🐍 Exercise 2 (research): density, k, similarity and shrinkage
At densities of 6% and 15%, compute the held-out RMSE of user-based k-NN with Pearson or raw-cosine similarity, k = 5, 20, 100, with and without significance weighting, against the bias baseline.

User-based CF in AI, robotics and aerospace

  • Procedure and document recommendation: technicians with similar job histories tend to need similar documents.
  • Analyst tooling: recommending dashboards, queries or datasets used by colleagues with overlapping work.
  • Crew and route preferences: suggesting options favoured by similar operators, with care for sparsity and bias.

References

  1. P. Resnick, N. Iacovou, M. Suchak, P. Bergstrom, J. Riedl (1994). “GroupLens: an open architecture for collaborative filtering of netnews.” Proc. ACM CSCW.
  2. J. L. Herlocker, J. A. Konstan, A. Borchers, J. Riedl (1999). “An algorithmic framework for performing collaborative filtering.” Proc. ACM SIGIR.
  3. Y. Koren (2010). “Factor in the neighbors: scalable and accurate collaborative filtering.” ACM Transactions on Knowledge Discovery from Data 4(1).
  4. B. Marlin, R. Zemel (2009). “Collaborative prediction and ranking with non-random missing data.” Proc. ACM RecSys.