Scoring Tracks with Confidence
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:
- 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.
- Feed the feature vector into the trained model. The model will output a probability, which we call the
affinity score. - 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 atrack_id → embeddingdictionary for efficient lookup. -
Valid Feature Vector Construction: For each track to be scored:
- If it has a valid embedding, concatenate the user profile and track vector.
- Add it to the list for scoring.
- Tracks with missing embeddings are skipped silently.
-
Scoring with Model: Converts the list of vectors to a DataFrame and feeds it into model.predict_proba(). The second column [:, 1] gives the score for the positive class (liking the track).
-
Return Format: Returns a dict of {track_id: score} for all valid tracks.
