Spotify Recommendation System using Content-Based Filtering

This video builds a Spotify song recommendation system using content-based filtering on audio features like danceability and energy, pulled via the Spotify API.

6:14 video3 min readWatch on YouTube

Recommendation systems can work in very different ways depending on what data they rely on. This video moves from a collaborative filtering approach, covered in an earlier video, to content-based filtering, and builds a working Spotify song recommendation system using the audio characteristics of the tracks themselves rather than how other users have behaved.

Content-based filtering versus collaborative filtering

Collaborative filtering relies on the preferences and similarities between users, recommending content that similar users have already watched or interacted with. Content-based filtering works differently: it relies on the content, features, or characteristics of the items a user has already interacted with or liked. The example given is straightforward: if a user has watched a movie and liked its genre or its actors, other movies sharing those same characteristics become good candidates for recommendation, regardless of what other users have done.

Getting data from the Spotify API

To build the system, the project pulls data directly from the Spotify API. Anyone with a Spotify account can create an app on the Spotify developer dashboard and obtain a client ID and client secret. Those credentials are used to generate an access token, which in turn is used with a specific playlist ID to retrieve all the tracks in that playlist, along with the audio features of each individual track. The core data used for the recommendation system includes the track name, the artist, album details, and the audio features associated with each song.

Choosing the features that define similarity

After pulling the playlist data and reviewing what came back, the project settles on a set of audio features to build recommendations around, including danceability, energy, and key, among other characteristics of a song. These values are normalized so they can be compared consistently across tracks.

How the recommendation function works

The content-based recommendation function starts by finding the index of the input song, the one the user wants recommendations for. Using that index, it calculates similarity scores between that song and every other song in the dataset, then retrieves the top N+1 most similar songs. The "plus one" matters because the most similar song to any given track is itself, so the function needs one extra slot to return N genuinely different recommendations. The output includes each recommended song's track name, artist, and album, and the approach can be extended to return additional features as needed.

Where content-based filtering falls short

The video is candid about the tradeoffs of this approach. Recommendations tend to stay similar to items the user has already interacted with, which limits diversity and can hold back the discovery of new interests. There is also a cold start problem: content-based filtering struggles with new users or new items because it depends on historical interaction data or established item attributes, and without enough of that data, its recommendations become unreliable. Because it only analyzes item attributes, it may not fully capture the complexity of user preferences, making it less accurate in some cases than collaborative filtering. Finally, there is a risk of over-specialization, where the system overly focuses on specific features and misses broader patterns or associations that could otherwise improve recommendation quality.

Key takeaways

  • Content-based filtering recommends items based on the item's own features rather than on similarities between users.
  • Spotify's API provides the track, artist, album, and audio feature data needed to power the recommender, accessed via a client ID, client secret, and access token.
  • The recommendation function scores similarity against an input song and returns the top N most similar tracks, excluding the song itself.
  • Weaknesses include limited diversity, a cold start problem for new users or items, and a risk of over-specialization on narrow features.
  • This approach directly complements the collaborative filtering method covered in an earlier video in the series.

Who this is for

This video is useful for anyone building a first recommendation system and wanting to understand content-based filtering as an alternative or complement to collaborative filtering. It is part of a Humanitarians AI series exploring different recommendation approaches using real Spotify data.

Full transcript(auto-generated, with timestamps)

[0:01]Hello everyone previously we have seen how to implement a recommendation system using collaborative filtering let us recollect what collaborative filtering is collaborative filtering relays on the preferences and similarities between the users and then recommend the content which the similar users have watched or interacted with now let us Implement a recommendation system using content based filtering so what is content based filtering let us understand that with the help of an example there is a user user one and they have watched a film movie one and there are other films like Movie 2 and Movie 3 which have the same Co similar content for example uh let's say

[1:05]They have uh the similar actors or it is from the same genre since user one has already watched a movie from a particular JRE they might also like the other movies from that J so we want to recommend these movies to user one that means content based filtering relies on the content or the uh features or characteristics of the uh items the the user have already interacted with or liked now let us implement the Spotify music recommendation system using content based filtering for the data let us use the data from Spotify API and for that we need the client ID and client secret anyone with a Spotify account can create

[2:08]An app on the Spotify dashboard and obtain the client ID and client secret after getting those values we need an access token to make the API calls here I have generated the access toen and using that and a playlist ID I am getting all the track tracks from the playlist uh out from that I'm also getting the audio features of a single track so the main data that I am using for this uh system is the uh track name artist and few of the album details along with the audio features this is the playlist ID that I have used and these are the songs that

[3:00]I've got from the playlist now after that I've like uh described the playlist and uh went through few details about that from that I have decided that these features I want to create a recommendation system using these uh content or these features like the danceability energy key and other things about a song using that I have normalized these values and so in the content based recommendations function I am first getting the index of the input song that I uh for which I want to make the recommendations and using that song I'm finding the similarity scores between the other songs and from that I am retrieving the top n+

[4:04]One similar songs I'm doing n+ one because the first song would be the same song and we actually want the songs from one to n+ one and I am wring those uh songs along with it track name artist and album and we can even extend the other features that we want to return so for this particular input song these are the top five recommendations that we have made using content based filtering however there are few disadvantages using content based filtering like limited diversity recommendations tend to be similar to items the user has already interacted with which can lead to a lack of diversity in recommendations and

[5:02]Potentially limit the discovery of new interests there is also Co start problem as the content based filtering struggles with new users or items because it relays on the historical data of user interactions or item attributes without significant data it may not be able to provide accurate recommendations there also limited understanding of user preferences as we only analyze the item attributes it may not fully capture the complexity of user preferences leading to less accurate recommendations compared to collaborative filtering methods also there is over specialization as we may May overly focus on specific features and potentially miss out on Border patterns or associations that could improve the

[6:09]Recommendation accuracy thank you

More videos

Humanitarians AI Lyrical Literacy Project