Skip to content
TheChatArchiveThe Chat Archive
GPT 5.1 Codex Max· openaiHugging Face Frontier Reasoning Traces

Build a movie recommender with alternating least squares for matrix factorization. Build…

  1. User

    Build a movie recommender with alternating least squares for matrix factorization. Build a user-item rating matrix and iteratively fix the user matrix to solve for items and vice versa until RMSE converges.

  2. Assistant

    Providing ALS matrix factorization example

    Here is a Python example implementing ALS for a movie recommender with a user-item rating matrix, iteratively solving for user and item latent factors using numpy and handling missing values as zeros, including RMSE calculation and convergence control. A common way to build a recommender when you have an explicit user–item ratings matrix (R) is to factor it into two low–rank matrices (P \in \mathbb{R}^{n_\text{users}\times k}) and (Q \in \mathbb{R}^{n_\text{items}\times k}) such that (R \approx P Q^\top). The alternating least squares (ALS) algorithm does exactly what you described: it alternately fixes one factor and solves a regularized least–squares problem for the other.

    Here's a minimal, self–contained example in Python using numpy that:

    • constructs a user–item matrix,
    • initializes latent factors,
    • alternates between updating user and item factors,
    • computes RMSE on the known ratings until convergence.
    import numpy as np
    
    def rmse(R, P, Q, mask):
        """Compute RMSE on observed entries defined by mask."""
        R_hat = P.dot(Q.T)
        diff = (R - R_hat) * mask  # only count known ratings
        return np.sqrt((diff**2).sum() / mask.sum())
    
    def als(R, k=10, lamb=0.1, n_iters=20, tol=1e-3):
        """
        Alternating least squares factorization of R into P, Q.
        R: user-item rating matrix with zeros for missing entries
        k: latent dimensionality
        lamb: regularization parameter
        """
        n_users, n_items = R.shape
        mask = (R > 0).astype(float)  # 1 where rating is known, 0 otherwise
    
        # initialize latent factors
        P = 0.1 * np.random.randn(n_users, k)
        Q = 0.1 * np.random.randn(n_items, k)
    
        last_rmse = None
        for it in range(n_iters):
            # Update user factors P
            for u in range(n_users):
                # indices of items rated by user u
                idx = mask[u, :] == 1
                if not np.any(idx):  # skip users with no ratings
                    continue
                Q_u = Q[idx, :]        # |Iu| x k
                r_u = R[u, idx]        # |Iu|
                # Solve (Q_u^T Q_u + λI) p_u = Q_u^T r_u
                A = Q_u.T @ Q_u + lamb * np.eye(k)
                b = Q_u.T @ r_u
                P[u, :] = np.linalg.solve(A, b)
    
            # Update item factors Q
            for i in range(n_items):
                # indices of users that rated item i
                idx = mask[:, i] == 1
                if not np.any(idx):
                    continue
                P_i = P[idx, :]       # |Ui| x k
                r_i = R[idx, i]       # |Ui|
                # Solve (P_i^T P_i + λI) q_i = P_i^T r_i
                A = P_i.T @ P_i + lamb * np.eye(k)
                b = P_i.T @ r_i
                Q[i, :] = np.linalg.solve(A, b)
    
            # Compute RMSE on known entries
            curr_rmse = rmse(R, P, Q, mask)
            print(f"Iteration {it+1}, RMSE {curr_rmse:.4f}")
            if last_rmse is not None and abs(last_rmse - curr_rmse) < tol:
                break
            last_rmse = curr_rmse
    
        return P, Q
    
    # Example usage with a small toy ratings matrix
    R = np.array([
        [5, 3, 0, 1],
        [4, 0, 0, 1],
        [1, 1, 0, 5],
        [0, 0, 5, 4],
        [0, 1, 5, 4],
    ], dtype=float)
    
    P, Q = als(R, k=3, lamb=0.1, n_iters=50)
    
    # Predict a missing rating, e.g. user 0 on item 2
    pred_rating = P[0, :] @ Q[2, :].T
    print("Predicted rating for user 0 on item 2:", pred_rating)
    

    A few points to note:

    • mask is used to ensure that only observed ratings contribute to the loss/RMSE; zeros in R are treated as missing values.
    • The subproblems for each user and each item are independent and amount to solving a (k \times k) linear system, which is efficient when (k) is small.
    • A small (\lambda) term is added to the normal equations to prevent overfitting and to ensure the matrices are well–conditioned.
    • The loop stops either after a fixed number of iterations or when the improvement in RMSE drops below a tolerance.

    On a real dataset such as MovieLens, you would first build R by pivoting the (user_id, item_id, rating) triples into a dense or sparse matrix, then feed it into als to learn P and Q. Once you have P and Q, you can recommend top–N items to a user by computing the dot product scores P[u] @ Q.T and selecting unseen items with the highest predicted scores.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.