All You Need to Know About a Music Recommendation System with a Step-By-Step Guide to Creating It

You open your favorite streaming app, and instead of drowning in the endless music catalogs, immediately turn on ready-made playlists with songs tailored to your specific tastes. This all is possible thanks to AI-based music recommendation systems (MRS).

In this article, we will survey such recommendation systems for music that are especially used in streaming-like music & audio services. We are going to learn the basic principles of its work from both technical and user points of view, look into its benefits and business use cases, supporting them with a real example.

For the icing on the cake, we will create a collaborative recommendation system by ourselves: for this, we will use a free public dataset and apply the K-Nearest Neighbors (kNN) algorithm to identify songs that you like.

This article will be useful for startups in the Music & Audio industry, especially for those whose business model revolves around streaming technologies. And if you are just an enthusiast curious about how music recommender works and probably even willing to test it in practice – keep on reading to satisfy your curiosity.

What is a music recommendation system?

Recommendation system is a filtering system, the purpose of which is to predict the preference that a user would give to a particular element, in our case – to a song. It is a core of huge engines that work by certain recommender algorithms and suggest a single item or a set of items to users based on such predictions.

Whether we are aware of it or not, a variety of recommendation systems have become an integral part of our daily routine since recently. Starting from accurately targeted advertising product suggestions and finishing with personalized video or music playlists compiled specifically for us – recommendation systems seem to be encompassing our everyday lives from literally every corner of digital space.

A phenomenon of these days, TikTok is built all around the recommendation system engine: that’s why TikTok’s algorithms are considered to be unique and are promising many more opportunities to the creators to grow organically - or in other words, with the help of recommender system algorithms.

In the music industry, recommendation systems are part of a big engine of streaming apps like Spotify, YouTube Music, Deezer, Tidal, and the like.

They serve to ensure a quality streaming experience for you.

How does a music recommendation system work?

There are 2 most popular recommendation systems:

  • content-based (recommendations based on the similarity of content or, in our case — attributes of two songs)
  • collaborative (recommendations based on similarity of users’ preferences and using matrices with ratings for each content piece, in our case — a song)

The content-based approach relies on the similarity of particular items. While using a streaming music service, a user puts likes or dislikes on the songs, creating playlists. The main idea of a content-based recommendation system is to extract keywords from the description of a song that a user likes, compare them with the keywords from the other songs, and, based on this, recommend similar songs to the user.

In turn, a collaborative system is built on the basis of users’ overlapping preferences and ratings of songs. It assumes that if user A and user B express similar preferences, similar songs can be recommended to them, meaning that if user A likes a particular song, it is likely that this song will also appeal to user B and vice versa. Collaborative recommendation systems are generally considered more accurate, as they rely on direct user interactions with the system versus content similarity.

Collaborative Versus Content-Based Filtering

You might wonder, how does this process look from the user’s point of view?

As we have already mentioned, these recommendation systems do not require any additional actions on the user's side. A person simply installs the application and registers, listens to his favorite songs, makes playlists... And the more data we have about his preferences, the more accurate our song recommendations to him become. It means that as the accuracy of algorithms will grow, a user will enjoy music and will be satisfied with a music service even more.

Practical use cases: how to know that you need MRS?

People nowadays are very used to recommendation systems in all areas: news feeds, streaming video and audio services, online stores. For a music service, a recommendation system is a must, given the current consumption habits in our modern world.

Music subscription services use recommendation systems to shape individual songs into comprehensive playlists by particular criteria. That’s for the convenience of the user and to increase customer satisfaction.

Platforms that sell music tracks or albums use recommendation systems to increase sales. It works by the following scheme: when a user buys a certain track, a recommendation system suggests some additional similar tracks for him to buy, in such a way encouraging him to make more purchases.

You need a Music Recommendation System if you want to:

  1. Increase satisfaction and engagement of your customers
  2. Make your platform maximum personalized
  3. Provide quality and immersive customer streaming experience
  4. Build trust and listening habits of your users
  5. Make it convenient to use your service, with no need to waste time finding new songs
  6. Boost sales and subscription rate
  7. Increase CTR and conversion and lower churn rate
  8. Encourage up-sells and cross-sells
  9. Automate curating and playlisting audio
  10. Get insights about users’ behavior and make data-based marketing decisions

The rise of streaming apps and the implementation of music recommendation systems have affected users’ habits in consuming music. According to Nielsen’s Music Survey, almost 75% of user’s experience is tightly interwoven with playlisting.

Apart from ready-made playlists that listeners make use of, there is also a growing trend of creating your own playlists and sharing them with your friends. However, this is getting more and more challenging due to the immense number of tracks online – a so-called “choice overload”.

To make it easier, streaming services like Spotify and Apple Music have offered their listeners to also use automatically generated song suggestions while making a playlist by themselves. In this case, AI-algorithms can recommend tracks based on the name of a playlist or some of the tracks already included by a user.

Spotify music recommendation algorithm: a streaming giant with highly personalized playlists

A Swedish company Spotify is now viewed as a king in the music industry since streaming services have become one of the main revenue streams to artists and labels. The value of implementing a recommendation system in the service cannot be overlooked, as it has had a massive impact not only on Spotify business but also on the whole music industry.

Firstly, the algorithms recommending music based on listening experience rather than top artists have helped to handle, to some extent, the issue of uneven royalty payment distribution. Before MRS, listeners would give preference to listening to the famous top artists and songs. Consequently, emerging artists didn’t really have a chance to be discovered. With the help of MRS, Spotify has reorganized music around user preferences, mood, and feelings, which has opened new opportunities to both industry players and listeners.

The second important point is that by gathering detailed data about listeners’ habits, Spotify can sell this data to advertisers. It is of great value to the whole market participants, including labels licensing music to the streaming service. The data provides the industry with insights into listeners' habits, giving an understanding of how they consume music and what else they like. This huge privilege has granted Spotify a superior position in the whole music industry.

Spotify now has over 100 million users. For each of them, a fresh "Discover Weekly" playlist arrives on Monday. Spotify is considered a leading streaming service, and its song recommendation algorithm can boast of a high rate of accuracy.

For you to take a glimpse into how one of the best music recommendation systems works and what is the key to such accurate and personalized playlists, let’s learn more about Spotify music recommendation algorithm.

In fact, Spotify did not invent a new algorithm but rather combined 3 of the best ones that are used in this area. The result is a powerful unique mechanism that makes a very accurate list of recommendations.

The service uses 3 models to create playlists with recommendations:

  • collaborative filtering
  • NLP for text analysis
  • audio models for audio file analysis

We have already discussed the method of a collaborative model. Let’s now take a closer look at NLP and audio models.

The NLP model of Spotify is constantly searching on the Internet for articles about songwriters and songs. After collecting the information, the model uses the descriptions and analyzes their content to construct the vector. Comparing vectors allows us to determine the similarity of two or more songs.

But let’s consider the case when a beginning artist uploads his song to the service. In this scenario, the first 2 models will not show this song in the recommended lists since there is little or no data about it on the network, and it will not be included in the collaborative filtering sample.

This is where the third model comes to the rescue: it analyzes the song using a neural convolutional network and creates a spectrogram. After passing through the network, the algorithm knows the characteristics of the song — its volume, timbre, size, harmony, etc. Then, it compares them with the characteristics of other songs and finds similar ones, after which the track goes to the playlist "Discover Weekly".

Discover Weekly Spotify Playlist

Most modern streaming services have similar recommendation playlists.

On the example of Spotify, we could have seen how it is possible to make use of a so-called hybrid model of a recommender system, which unifies the advantages of several models while neutralizing their drawbacks and in such a way reaching high accuracy.

Create a music recommendation system in 4 steps

In this section, we will learn the basics of how to build a music recommendation engine.

In this experiment, we will create a music recommendation system using collaborative filtering, which means that we will only use the user's historical preferences for the song set.

Step 1. Import the necessary data

Dataset: we will use a public Million Song Dataset that contains information about popular tracks. First, let's import the necessary libraries into our Google Collaboratory Notebook.

Load data from public dataset and drop duplicates from song data:

MRS coding

After loading all the required data, merge them to get a summary of songs and users. In any project that involves machine learning or data science, data mining is an important part. Let's look a little deeper at our dataset:

Code piece

Let's see how many unique songs there are in our dataset:

songs dataset

And how many unique users we have in our dataset:

dataset with users

Step 2. Get statistics of the top 10 most played songs and artists

How about looking at the top 10 most played songs and receiving the statistics on a graph? To do this, import the libraries you need:

Now let's get this top list from your dataset and plot it:

code with top songs list

And this is the graph we get:

graph with most famous songs

Now let's find the 10 most popular artists in our dataset:

code with top artists
graph with top artists

Step 3. Prepare the data

To go further, we need to understand how many songs one user listens on average in order to discard irrelevant data:

graph with statistics of listening music

Here, we have found out that the median is 16 songs listened to.

Let's select users who have listened to at least 16 songs:

Convert the data frame into a pivot table:

Since we will now receive the song ID instead of its title, let’s match these values:

Step 4. Choose an algorithm and create a model

The k-nearest neighbors algorithm (kNN) is a non-parametric machine learning method first developed by Evelyn Fix and Joseph Hodges in 1951 and later extended by Thomas Cover. It is used for classification and regression. In both cases, the input consists of the k closest training examples in the feature space.

In kNN classification, the output is a class membership. The object is classified by a plurality vote of its neighbors, with the object being assigned to the class most common among its k nearest neighbors (k is a positive integer, typically small). If k = 1, then the object is simply assigned to the class of that single nearest neighbor.

Now, let's use a simple example to analyze the operation of this algorithm.

Let's imagine that we have two classes: ClassA (red squares) and ClassB (blue circles). So how does the algorithm determine to which class an object of the unknown class belongs?

Algorithms Classification

First, it looks at the k-nearest neighbors to him. Then, it makes a prediction, which class the point belongs to, by voting for the most popular class among those points. For example, when k = 3, the algorithm will assign the point to Class B, and when k = 7, then to class A.

If we use this algorithm to find the songs to recommend, the algorithm will calculate the distance between the target song and all the songs in our dataset. Then, based on the distance, it will return us a ranking of the best fitting songs.

Let's create a class Recommender and methods for it without getting too technical (the class is fully described in Google Collaboratory):

creating a class Recommender

Now it’s time to create a Recommender model and get your recommendations.

The input parameters are:

  • metric — we will use the cosine
  • algorithm — a method for calculating the nearest neighbors, we will use the brute force method
  • k — the number of neighbors that the algorithm will choose (you can try to change the values by yourself and look at the results)

And here are the results that what we get in the end:

Starting the recommendation process for Secrets ...

... Done

The recommendations for Secrets are:

['Blind', 'Sunday', 'Half Of My Heart', 'Hey_ Soul Sister', 'Lucky (Album Version)', 'Heartbreak Warfare', 'Fireflies', 'All The Right Moves', 'Tonight', 'Sehr kosmisch']

Ta da! Your music recommendation system is ready. You can also try testing this music recommender system algorithm with your favorite song in Google Collaboratory.

You can use this system on a streaming music service or on sites that sell music tracks. It can also be customized according to your client's requests - maybe it's worth trying out a different algorithm or supplementing this system with other models.

Final Thoughts

Like any other type of recommendation system, a modern music recommendation system is built using Machine Learning and Artificial Intelligence. Creating such a system requires high expertise in these areas, a set of data about users and the songs they listen to. Using this data, we can combine models and make custom solutions for a particular business case.

For example, you can build and program algorithms that will focus on the time when a user listens to a certain song. By this, we can generate personalized playlists based on the timing criteria - such as morning music playlists or weekend playlists.

By the same logic, you can create any other recommendation type based on particular criteria for which we take data from the dataset.

So even though the basic principles of music recommendation systems are similar, their applied value can be different. As we’ve learned from the article, the possibilities of MRS can vary depending on the types of models you choose to integrate and the data you are using. But most of all - it all starts from your goals, business model, customers’ habits, and application functionality.

In delivering satisfying personalized experiences to your users, a music recommendation system is just the right tool for you, and you can always customize it to turn in the direction that will help you reach your particular goals.