Sub topic 3
The user-item matrix and why it is sparse
This chapter assumes you know basic Python, NumPy arrays, and matrices. It shows how interaction data becomes a matrix, and why that matrix is mostly empty.
From interaction logs to a matrix
Collaborative filtering starts from a log of events. Each event says that a user did something with an item: rated it, bought it, or watched it. The log is a list of records such as (user, item, value).
The user-item matrix R arranges these records in a grid.
- Each row is one user.
- Each column is one item.
- Cell
R[u, i]holds the value userugave itemi.
If a user never interacted with an item, the cell has no value. Every method in the later chapters reads from this matrix. Neighborhood methods compare its rows or columns. Matrix factorization approximates it with two smaller matrices.
Building a small matrix
The code below builds a matrix of 5 users and 4 items. It marks missing ratings with 0 and measures how many cells are empty.
import numpy as np
# Rows are users, columns are items. 0 means "not rated".
R = np.array([
[5, 3, 0, 1],
[4, 0, 0, 1],
[1, 1, 0, 5],
[1, 0, 0, 4],
[0, 1, 5, 4],
], dtype=float)
observed = R > 0
sparsity = 1.0 - observed.sum() / R.size
print(f"{observed.sum()} ratings, sparsity = {sparsity:.2f}")The program prints 13 ratings, sparsity = 0.35. The boolean mask observed marks the cells that hold a real rating. Sparsity is the fraction of cells with no observation. Here 13 of 20 cells are filled, so 35% are empty.
Why real matrices are far sparser
Sparsity is the normal case. A user can only interact with a tiny part of a large catalog. The Netflix Prize data had about 480,000 users and 17,770 movies. That is roughly 8.5 billion cells, but only about 100 million ratings. Only about 1.2% of the cells were filled.
The empty cells are the reason a recommender exists. Each one is a question: would this user like this item? The system has to answer that question for items the user has not tried.
Practical consequences
- Memory: A dense array of 8.5 billion floats is impractical. Store only the observed triples with a sparse format, such as SciPy's
coo_matrixorcsr_matrix. - Overlap: Two users may have rated no items in common. Similarity between them is then undefined or unreliable.
- Training: Models must learn from observed cells only. Treating every empty cell as a real zero teaches the model the wrong thing.
from scipy.sparse import coo_matrix
users = [0, 0, 1]
items = [0, 1, 0]
values = [5, 3, 4]
M = coo_matrix((values, (users, items)), shape=(5, 4)).tocsr()
print(M.nnz) # 3 stored entriesCommon errors
- Averaging a row including its zeros. This pulls every user's mean rating toward 0. Average over observed cells only.
- Computing cosine similarity on rows with few shared items. Check how many items two users share before you trust a score.
- Building a dense matrix for a large catalog and running out of memory.
Exercise
A matrix has 1,000 users and 2,000 items, with 40,000 observed ratings. What is its sparsity, and why is a dense array a poor way to store it?
Show answer
There are 2,000,000 cells, so density is 40,000 / 2,000,000 = 2%, and sparsity is 98%. A dense array stores all 2,000,000 values, but 1,960,000 of them are empty. A sparse format stores only the 40,000 observed entries.