Music

On this page, I’d like to invite you to one of my greatest passions: music. It’s more than just a pastime for me; it’s an integral part of my daily life. Whether I’m at home relaxing or working diligently behind my computer, there’s always music playing in the background. It’s a constant companion that sets the tone for my day and accompanies me through every moment.
Because we both appreciate the power of data, I’ve decided to delve into a small analysis of my listening behavior and its patterns from 2015 to 2022. Using the data I’ve downloaded from Spotify, let’s explore how my musical tastes have evolved and shaped my journey over the years.

Over these seven years, my musical preferences have evolved, mirroring the changes in my life. It’s important to note a significant factor that influenced my listening habits. In September 2019, I immigrated to Canada and began my journey as a student until 2022. This move had a profound impact as it eliminated the need for a VPN to access Spotify, which is filtered in Iran. Additionally, being a student again afforded me more free time to indulge in my passion for music.

Data Collection

Data is collected from Spotify

Data Cleaning

I did not do a very dig data cleaning. The reason is that the data sent by Spotify was pretty clean and did not require that much manipulation. I only dropped some variables here and there and changed the columns’ names. I also cleaned the time variable. The final table has 101,807 rows and 24 columns

Figure 1: The downloaded data of song that I listed

Exploring Artists

ax = sns.countplot(x=df['Artist'],
                   order=df['Artist'].value_counts(ascending=False).iloc[:10].index);
ax.set_xlabel("Artist Name")
ax.set_ylabel("Frequency")
ax.set_xticklabels(df['Artist'].value_counts(ascending=False).iloc[:10].index, rotation=90)
ax
Figure 2: The artists that I listened the most

As the above graph shows my most listened artists are Hossein Alizadeh, Mohsen Namjoo, and Mohammadreza Shajarain. After the three Iranian artists, we have David Gilmour, Sleep Token and Steven Wilson.

In the following video, I would like to show how my favorite artists changed through time.

Album

Figure 3: The albums that I listened the most

Adding Track Feature

In the next step, I used Spotify API to add the track features to my data. This integration allows for a more insightful exploration of my music preferences and usage patterns.

Spotify furnishes specific attributes for each song, encompassing various sound characteristics. These include:

Beats Per Minute (BPM): indicating the tempo
Energy: reflecting the song’s vigor with higher values denoting increased energy
Danceability: where higher values signify ease of dancing to the song
Loudness: representing the volume in decibels, with higher values indicating louder songs
Valence: indicates the positive mood of the song with higher values suggesting a more positive tone
Length: denoting the duration of the song; Acousticness, with higher values, indicating a more acoustic quality
instrumentalness: The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content
Speechiness: it detects the presence of spoken words in a track. 
You can read more about track features here
https://developer.spotify.com/documentation/web-api/reference/get-audio-features

def get_token():
    auth_string=clientID+ ":" +clientsecret
    
    auth_bytes=auth_string.encode('utf-8')
    auth_base64=str(base64.b64encode(auth_bytes),'utf-8')
    url='https://accounts.spotify.com/api/token'
    headers={
    "Authorization":"Basic "+auth_base64,
    "Content-Type":"application/x-www-form-urlencoded"
    }
    data={"grant_type":"client_credentials"}
    result=post(url,headers=headers,data=data)
    json_result=json.loads(result.content)
    token=json_result["access_token"]
    return token
def get_auth_header(token):
    return {"Authorization":"Bearer "+token}

token=get_token()
def track_feature(token, track_id):
    url = "https://api.spotify.com/v1/audio-features"
    headers = {
        "Authorization": "Bearer " + token
    }
    query_params = {
        "ids": track_id,
        "limit": 1
    }
    response = requests.get(url, headers=headers, params=query_params)
    json_result = json.loads(response.content)
    track = pd.json_normalize(json_result['audio_features'])
    return track
Figure 4: The data of features
Figure 5: Correlation between different features

There is a small correlation between different features. However, there is a relation higher correlation between the danceability and energy or loudness of the songs that I listed. Generally, I believe this is expected. Danceable songs are more loud and have more energy. The highest correlation is between accoustiness and energy and the sign of correlation is negative. This means that the higher accousticness of a song is in the dataset, it is likely that it has lower energy.

Figure 5: Different features distributions

It is useful to look at the distribution of each feature for the songs that I listen to. As we can see, the dancibility has almost a normal distribution showing that I usually listen to songs that their dancibility is on average. Regarding liveness, the graph shows that I do not intend to listen to live music that much. Regarding Energy, the graph is very interesting. It shows that I like to listen to songs with various levels of energy.
The loudness graph has a left-skewed distribution. which means that I intend to listen to songs that have a higher level of loudness. The valance or positiveness of songs has a right skewed distribution which means I intend to listen to less positive songs.

How my Taste of Music Changed between 2015-2022

Figure 6: Change in the mean of different features from 2015 to 2022

The findings are interesting, revealing distinct patterns in danceability, tempo, loudness, and energy. Notably, since 2020, my listening preferences have shifted towards songs with elevated danceability, tempo, loudness, and energy. However, there is a discernible decline in the acousticness and instrumentality of songs post-2020. A noteworthy observation is that in 2022, my favorite artist, whom I spent a significant amount of time listening to, was Hossein Alizadeh. As an Iranian artist, the majority of his albums are predominantly instrumental.

How I Listen Throughout a Day

Figure 7: Changes in the mean of different features in different hours of the day

The results show that there are two main times when my music choices change. At 2 A.M. and between 2-3 p.m., I tend to prefer songs that are not too loud, less danceable, more acoustic, have lower energy, and are less positive. Interestingly, at 2 p.m., I lean towards songs with a less positive vibe than any other time of the day. However, after 2 p.m., my preferences flip, and I start to enjoy the most danceable and positive songs, especially around 2 in the morning.

How Much I Listen to Music

Figure 8: Changes in the total hours and average hours per day of music listening from 2015 to 2022

Firstly, let me clarify what each value represents in this graph. On the left y-axis, the total time (in hours) I spent listening to music is displayed. For instance, in 2021, I dedicated 2261 hours to music, equivalent to over 6 hours per day (right y-axis). The time spent on music varies across years. Between 2016 and 2018, I allocated around 1.5 hours daily, a period coinciding with full-time work and limited access to Spotify while living in Iran. In 2019, after immigrating to Canada and gaining easy access to Spotify, there’s a noticeable increase in total listening hours. The graph highlights a significant surge in music consumption in 2020 and 2021. This spike can be attributed to being a student during these years, allowing more time for listening, and the impact of COVID-19 lockdowns, which confined me to home where I spent time enjoying my favorite songs and studying. However, there’s a substantial decline in 2022, primarily due to my return to full-time employment during that year.

Mehdi Mohamadian