Introduction: Predicting User Preferences with Confidence

Welcome back! In the previous lessons, you learned how to prepare training data from user session logs and how to train a machine learning model to predict which tracks a user might like. Now, you are ready to use this trained model to score tracks for each user.

In this lesson, you will learn how to use the model to assign a confidence score — called an "affinity score" — to each track for a given user. This score tells us how likely it is that the user will enjoy a particular track. These scores are the foundation for making smarter, more personalized music recommendations.

Recap: What We Need for Prediction

To score tracks for a user using a machine learning model, we need three things:

  • A trained affinity model, which takes combined user-track vectors and returns a score.
  • A user profile vector summarizing listening preferences.
  • A track embedding for each track we want to score.

We'll use these to construct feature vectors and feed them into the model.

How Predictive Scoring Works

Now, let’s look at how predictive scoring actually works.

For each track you want to score for a user, you need to:

  1. Create a feature vector by combining the user’s profile vector with the track’s embedding. This gives the model all the information it needs about both the user and the track.
  2. Feed the feature vector into the trained model. The model will output a probability, which we call the affinity score.
  3. Interpret the affinity score. This score is a number between 0 and 1. The closer it is to 1, the more confident the model is that the user will like the track.

For example, if the model gives a score of 0.85 for a track, it means there is an 85% chance (according to the model) that the user will enjoy that track.

Example: Predicting Affinity Scores for a User

Let’s walk through the main function that does this: predict_affinity_scores_for_user.

Here is the function from your project:

Let’s break down what happens here:

  • Model Check: The function first checks if the model is loaded. If not, it exits early. This prevents runtime crashes when calling .predict_proba on None.

  • User Vector Creation: Calls generate_user_profile_vector(user_id). If the user has no history, it returns None, and scoring is skipped for that user.

  • Track Embedding Mapping: Retrieves both ordered track IDs and embeddings in a single call to get_track_embeddings(), then zips them into a track_id → embedding dictionary for efficient lookup.

What You’ll See in the Tests

Let’s walk through the test file output to understand what’s being checked.

Test 1 — No Model Available

This confirms that the function gracefully handles the case where no model is loaded, by checking for model is None and returning an empty dictionary — instead of crashing or throwing an error.

Test 2 — Dummy Model with Known Vectors

Here, the dummy model simply sums the feature vector values.

  • The profile [1.0, 2.0] combined with embedding [0.1, 0.2] gives [1.0, 2.0, 0.1, 0.2], which sums to 3.3.
  • This confirms the feature concatenation and prediction logic work as expected.
Recommending Tracks with Predictions

Once you have affinity scores, you can use them to recommend the most promising tracks to a user. That’s exactly what the function recommend_tracks_by_prediction() does.

  • It first loads the trained model using load_model().
  • It gets all available tracks, then filters out those already listened to by the user.
  • It calls predict_affinity_scores_for_user() on the remaining tracks.
  • Finally, it sorts the scores and returns the top N highest-scoring track IDs.

Tip: Instead of always taking the top-N scores, you can set a minimum score threshold (e.g., 0.5 by default, or tuned via validation) to decide whether a track is “likely liked” or “unlikely.” This is especially useful when you want to filter out low-confidence recommendations, even if they appear in the top-N.

This is how the full recommendation loop is closed — from data → model → scores → top picks.

Summary and Practice Preview

In this lesson, you learned how to use your trained model to score tracks for a user with predictive confidence. You saw how to combine user and track information, use the model to get affinity scores, and interpret the results.

These affinity scores are the key to making personalized recommendations. In the next practice exercises, you will get hands-on experience using these functions to score tracks and recommend the best ones to users.

Great job making it this far — let’s put these skills into practice!

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