The world’s leading publication for data science, AI, and ML professionals.

Discovering your Music Taste with Python and Spotify API

A Step-by-Step Guide to Accessing Spotify Data and Creating a Radar Chart

Image by Author
Image by Author

It’s almost the end of 2020! If you have been using Spotify for years, you probably know at the end of each year, Spotify will provide premium users personalized insights, such as your favorite songs and artists, and how much time you spent on the services, etc. As a data scientist, I wanted to take a look at all the songs’ audio features from the Discovery Weekly playlist and see what Music features I enjoy the most based on my listening history on Spotify.

Setting Up Spotify’s Web API

Image from https://developer.spotify.com/documentation/web-api/
Image from https://developer.spotify.com/documentation/web-api/

If you are new to API, you probably are wondering what an API is and does. In short, you can think of API as a shortcut into a web service’s database. It allows programmers to send and receive data without giving them full permission on the entire database. Check out Spotify Web API documentation to know more about what you can do with the API. In the following,

To get started, you will need to log into your Spotify account or create a new Spotify account. Then you can go to your dashboard page.

Screenshot from Spotify
Screenshot from Spotify

Now you can select create an app. Once this is done, you will receive both client ID and client secret ID.

Although we’re not trying to create an app, we will need this client ID to access the same data.

Data Extraction

In this article, I wanted to extract the data from all the songs in my Discovery Weekly playlist. Feel free to create your own playlist if you want to work on a different playlist data instead.

Here’s how you can get the playlist’s id:

Screenshot by Author
Screenshot by Author

The Spotify URI should look something this: spotify:playlist:xxxxxxxxxxxxx.

Installing Spotipy Package

Spotipy is "a lightweight Python library for the Spotify Web API." With Spotipy, we can get full access to all of the music data provided by the Spotify platform. It’s very simple to use. You can install the package using this command.!pip install spotipy.

Authorizing your Client Credential

Convert JSON File to Dataframe

This is what the data frame looks like.

Viewing the created Pandas Dataframe (Screenshot by Author)
Viewing the created Pandas Dataframe (Screenshot by Author)

Data Preprocessing

As shown below, some features are very small while some features are very big. In this case, taking the average values of all the features might not be immediately comparable. Variables that are measured at different scales do not contribute equally to the analysis and might end up introducing biases. We need a way to apply feature scaling and compare the data points.

Generate descriptive statistics (Screenshot by Author)
Generate descriptive statistics (Screenshot by Author)

I decided to use normalization, which is a scaling technique in which values are shifted and rescaled so that they end up ranging between 0 and 1. It is also known as Min-Max scaling.

from sklearn.preprocessing import MinMaxScaler
min_max_scaler = MinMaxScaler()
music_feature.loc[:]=min_max_scaler.fit_transform(music_feature.loc[:])

Creating a Radar Chart

Radar charts are the most effective when they are comparing various features. Based on the radar chart, you can tell I am a fan of acoustic music! 🎼🎹

Radar Chart Result (Screenshot by Author)
Radar Chart Result (Screenshot by Author)

Interested in making your own music taste radar chat? Follow the code below!

Conclusion

There you have it! Now you know how to extract any data using Spotify’s API, Python, and Spotipy. For the next step, we could use different ways to analyze and visualize Spotify’s data, such as building your own Spotify’s Recommendation Engine, visualizing your music taste over time, etc. I hope you find some inspiration here. And please, feel free to share your exciting project ideas in the comment section. Until next time, happy learning! 👩🏻 ‍💻


If you find this helpful, please follow me and check out my other blogs. ❤️

How to Convert Jupyter Notebooks into PDF

How to Prepare for Business Case Interview as an Analyst?

Understanding and Choosing the Right Probability Distributions with Examples

Building a Product Recommendation System for E-Commerce: Part I – Web Scraping

How to Set Up Automated Tasks in Linux Using Cron


Related Articles