Introduction to ALS and Collaborative Filtering

Welcome back! In the previous lesson, you explored the foundation of user-item explicit rating matrices used in recommendation systems. Today, we'll expand on that knowledge by diving into one of the powerful techniques for collaborative filtering known as the Alternating Least Squares (ALS) algorithm.

Recommendation systems have become essential in offering personalized experiences, with collaborative filtering being a primary method. Collaborative filtering works by understanding user preferences through their past interactions and leveraging similar users or items to provide recommendations. The ALS algorithm is a matrix factorization approach that enables us to predict missing ratings effectively, making it a valuable tool in recommendation systems.

Compared with neighborhood-based methods, ALS scales better to large sparse matrices because it works with compact user and item factor representations instead of comparing every user or item directly. It also handles missing values naturally by optimizing only over observed ratings, and its alternating updates are a good fit for efficient linear algebra implementations. In this unit, we still work with explicit ratings; the implicit-feedback version of ALS will come later.

Recap of the Setup

Let's quickly recap and continue from the code you wrote in the previous lesson. You already have code that:

  • Reads a user-item rating matrix from a file (explicit_ratings.txt)
  • Copies the original matrix for later evaluation
  • Randomly marks a proportion of entries as missing (-1) and records their indices

Here is the relevant setup code (with print statements and unnecessary parts omitted):

This setup is crucial, as it establishes the data landscape we will work with throughout the ALS implementation.

If you want fully reproducible runs while debugging, you can temporarily replace the time-based seed with a fixed seed such as . The time-based version is fine for exploration, but fixed seeds make it much easier to compare outputs across runs.

Initializing User and Item Factors

To predict missing ratings using ALS, we need to decompose the interaction matrix into two matrices — user and item factors. These factors capture latent characteristics that influence user preferences and item popularity. Initially, these factors are filled with random values, which will then be optimized through ALS iterations.

Add this function and initialization to your code:

Here, U represents user factors, and V represents item factors, with numFactors indicating the dimensionality of these latent features. We multiply the random initialization by 0.01 so that factor values start small; this helps keep early predictions stable and usually makes optimization behave more smoothly than starting with large random values.

Optimization Problem

The ALS algorithm is the heart of our lesson. Before diving into the steps involved in ALS, it's essential to understand the optimization problem we're tackling and how ratings are predicted.

The ALS algorithm addresses the problem of predicting missing ratings in a user-item interaction matrix by factorizing it into two matrices: user factors (U) and item factors (V). We aim to approximate the matrix RR (user-item ratings) by minimizing the difference between the actual and predicted ratings through the following optimization problem:

minU,Vu,iobserved(RuiUuViT)2+λ(Uu2+
Solving with Alternating Least Squares

The ALS algorithm solves this optimization problem iteratively by alternating between updating user factors and item factors. Here's how ALS specifically tackles this:

  1. Fix Item Factors and Optimize User Factors:

    • For each user uu, it minimizes the error for the observed ratings by updating UuU_u, while keeping V fixed.
    • The update rule for user factors is derived from setting the derivative of the loss function with respect to UuU_u to zero, resulting in:
Implementing the Algorithm

Below is the ALS structure in smaller pieces. Gonum provides the matrix operations and solvers, but the high-level loop is simple: initialize factor matrices, repeatedly update users while holding items fixed, then update items while holding users fixed.

ALS Loop Skeleton

The two update phases follow the same pattern, so it is easier to look at them separately.

First, when updating a user, we collect the items that user actually rated, build a small matrix from those item-factor rows, and solve one regularized least-squares problem:

Updating User Factors

In this block, mat.NewDense(len(itemIdx), numFactors, nil) creates a temporary matrix to hold just the item-factor rows relevant to user u. V.RawRowView(i) returns row i from the factor matrix as a []float64, so SetRow(...) can copy those values directly into V_u. Then A.Mul(V_u.T(), V_u) forms the Gram matrix for this user's observed items, and adding lambda on the diagonal applies regularization.

The item update is symmetric: instead of gathering rated items for one user, we gather rating users for one item and build the temporary matrix from user-factor rows:

Updating Item Factors

The same idea appears here with roles reversed: U_i contains only the user-factor rows associated with item i, and Gonum again uses A.Mul(...) to build the regularized normal-equation matrix for the item update.

Both phases follow the same solve pattern. Once A and b are ready, Gonum handles the linear solve and gives back the updated factor vector:

Solving the System

Here, mat.NewVecDense(...) wraps the right-hand side vector b, and mat.NewSymDense(...) wraps A as a symmetric matrix because V_u^T V_u + lambda I and U_i^T U_i + lambda I are symmetric by construction. Finally, x.SolveVec(...) solves A * x = b and stores the result in x.

So the repeated ALS update pattern is:

  1. Gather observed ratings.
  2. Build a temporary factor matrix from the relevant rows.
  3. Form the regularized system A * x = b.
  4. Solve it and write the result back into U or V.

If SolveVec fails, it usually means the system is numerically unstable or poorly conditioned. In practice, increasing lambda, reducing factor dimensionality, or checking the data for very sparse rows can help.

Predicting Ratings and Evaluating with RMSE

Once user and item factors are optimized, we can predict the missing ratings by matrix multiplication of the two factors. To evaluate the model's accuracy, we calculate the Root Mean Square Error (RMSE) for excluded items:

Predicting Ratings

After optimizing the user and item factors with ALS, we use matrix multiplication to generate predicted ratings for all user-item pairs. The predictRatings function multiplies the user factor matrix U with the transpose of the item factor matrix V, resulting in a matrix of predicted ratings.

Evaluating with RMSE

To assess how well the model predicts the ratings that were intentionally masked (i.e., treated as missing during training), we use the calculateRMSE function. This function computes the root mean square error (RMSE) between the predicted ratings and the actual ratings at the masked positions. RMSE is a common metric for evaluating prediction accuracy: a lower RMSE indicates that the predicted ratings are closer to the true values.

One important detail is that ALS predictions are continuous float64 values, even when the original ratings are integers from 1 to 5. For RMSE, it is common to keep these raw floating-point predictions as-is. In a presentation layer, you might later clip or round them, but that is separate from the training and evaluation logic shown here.

Putting It All Together

Here’s how you use these functions in main:

These hyperparameters are simple teaching defaults, not universal best values. numFactors controls model capacity, lambdaReg controls regularization strength, and numIterations controls how long we alternate the updates. In practice, you would tune them based on validation performance and dataset size.

Summary and Preparation for Practice Exercises

In this lesson, you've successfully implemented the ALS algorithm to tackle collaborative filtering challenges within recommendation systems. You've learned to construct user-item matrices, initialize factors, and update them to predict missing ratings. This understanding equips you with a robust technique for building recommendation models.

Now, it's time to consolidate this theoretical understanding with hands-on exercises. These exercises are designed to reinforce the concepts learned, allowing you to apply ALS in varied scenarios. You've made significant progress, so keep up the great work as you continue to explore the exciting world of recommendation systems!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal