Preparing Training Data
Introduction: Why Prepare Training Data?
Welcome to the course. In this lesson, we will focus on preparing training data from session logs — a crucial step in building a system that can predict what music a user might like next.
Training data is the foundation of any machine learning model. For our smart music player, we want to teach the model to recognize patterns in what users listen to so it can make smart recommendations. Session logs are records of what tracks users have listened to. By turning these logs into structured training data, we give our model the information it needs to learn user preferences.
By the end of this lesson, you will understand how to transform raw session logs into a format that a machine learning model can use.
Recap: Project Setup and Data Sources
Before we dive in, let’s briefly remind ourselves of the project setup and where our data comes from. Our project uses several data sources:
- Session logs: These are stored in a file called
sessions.csvand record which users listened to which tracks. - Track data: Information about all available tracks.
- User profiles: Vectors that summarize a user's listening history.
- Track embeddings: Vectors that represent the features of each track.
Here is a quick example of how we load the necessary modules and data in our project:
This code sets up the basic data we need for preparing our training data. If you are using the CodeSignal environment, these modules and files should already be available.
Data Check: If the session log file path doesn’t exist, it’s good practice to print a warning so it’s clear that no training data will be loaded. You can also print the first few rows of the DataFrame with
sessions_df.head()when the file exists — this helps remind you what data you’re working with before moving on.
Positive and Negative Samples Explained
To train a model to predict user preferences, we need to show it examples of both what a user likes and what they don’t like (or at least, what they haven’t listened to yet).
- Positive samples: These are track-user pairs where the user has listened to the track. In our session logs, these are easy to find.
- Negative samples: These are track-user pairs where the user has not listened to the track. We create these by pairing users with tracks they haven’t played.
Note that we don’t include all unlistened tracks as negative samples — this would create an overwhelming number of negatives compared to positives and hurt model training. Instead, we sample a limited number using the
negative_sample_ratioparameter, which controls how many negatives are generated per positive. This helps maintain a healthy balance in the training dataset. Keep in mind that if a user has listened to nearly all tracks, the number of unlistened tracks might be very small or even zero. In such cases, the function gracefully avoids creating excess negative samples — meaning fewer (or no) negatives will be generated, even if thenegative_sample_ratiois set high. This behavior prevents index errors or sampling failures but might result in class imbalance for some users.
A very high ratio can create class imbalance (far more 0s than 1s), which often inflates accuracy while hurting recall for positives. It can also slow training and bias the model toward predicting “not liked.” Practical tips: start with 1–3, cap by available unlistened tracks (already handled), and monitor label counts. If you must go higher, consider class weights or downsampling negatives during training.
Why do we need both? If we only show the model what users like, it won’t learn to tell the difference between liked and unliked tracks. By including both, we help the model learn what makes a track appealing to a user.
What Happens If We Only Include Positives?
Let’s try preparing the training data with negative_sample_ratio=0, meaning no negative examples are added:
Sample Output:
As expected, only positive samples are included. This is not ideal for training a classifier — without negatives, the model cannot learn to distinguish good from bad recommendations.
Heads-up: The function prepare_training_data is implemented later in this lesson. The example below is for intuition only.
