This is a learn by building project to perform data clustering and principle component analysis of Spotify dataset using K-Means Clustering & Principle Component Analysis method.
Founded in 2006, the Spotify’s primary business is providing an audio streaming platform, the “Spotify” platform, that provides DRM-protected music, videos and podcasts from record labels and media companies. As a freemium service, basic features are free with advertisements or automatic music videos, while additional features, such as offline listening and commercial-free listening, are offered via paid subscriptions.
The analysis will use dataset from https://www.kaggle.com/zaheenhamidani/ultimate-spotify-tracks-db.
## Warning: package 'tidyverse' was built under R version 3.6.2
## -- Attaching packages ------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.2.1 v purrr 0.3.3
## v tibble 2.1.3 v dplyr 0.8.3
## v tidyr 1.0.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.6.2
## Warning: package 'tibble' was built under R version 3.6.2
## Warning: package 'tidyr' was built under R version 3.6.2
## Warning: package 'readr' was built under R version 3.6.2
## Warning: package 'purrr' was built under R version 3.6.2
## Warning: package 'dplyr' was built under R version 3.6.2
## Warning: package 'stringr' was built under R version 3.6.2
## Warning: package 'forcats' was built under R version 3.6.2
## -- Conflicts ---------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Warning: package 'FactoMineR' was built under R version 3.6.3
## Warning: package 'factoextra' was built under R version 3.6.2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
## Warning: package 'animation' was built under R version 3.6.2
## ï..genre artist_name track_name
## Comedy : 9681 Giuseppe Verdi : 1394 Home : 100
## Soundtrack: 9646 Giacomo Puccini : 1137 You : 71
## Indie : 9543 Kimbo Children's Music : 971 Intro : 69
## Jazz : 9441 Nobuo Uematsu : 825 Stay : 63
## Pop : 9386 Richard Wagner : 804 Wake Up: 59
## Electronic: 9377 Wolfgang Amadeus Mozart: 800 Closer : 58
## (Other) :175651 (Other) :226794 (Other):232305
## track_id popularity acousticness
## 0UE0RhnRaEYsiYgXpyLoZc: 8 Min. : 0.00 Min. :0.0000
## 0wY9rA9fJkuESyYm9uzVK5: 8 1st Qu.: 29.00 1st Qu.:0.0376
## 3R73Y7X53MIQZWnKloWq5i: 8 Median : 43.00 Median :0.2320
## 3uSSjnDMmoyERaAK9KvpJR: 8 Mean : 41.13 Mean :0.3686
## 6AIte2Iej1QKlaofpjCzW1: 8 3rd Qu.: 55.00 3rd Qu.:0.7220
## 6sVQNUvcVFTXvlk3ec0ngd: 8 Max. :100.00 Max. :0.9960
## (Other) :232677
## danceability duration_ms energy instrumentalness
## Min. :0.0569 Min. : 15387 Min. :2.03e-05 Min. :0.0000000
## 1st Qu.:0.4350 1st Qu.: 182857 1st Qu.:3.85e-01 1st Qu.:0.0000000
## Median :0.5710 Median : 220427 Median :6.05e-01 Median :0.0000443
## Mean :0.5544 Mean : 235122 Mean :5.71e-01 Mean :0.1483012
## 3rd Qu.:0.6920 3rd Qu.: 265768 3rd Qu.:7.87e-01 3rd Qu.:0.0358000
## Max. :0.9890 Max. :5552917 Max. :9.99e-01 Max. :0.9990000
##
## key liveness loudness mode
## C :27583 Min. :0.00967 Min. :-52.457 Major:151744
## G :26390 1st Qu.:0.09740 1st Qu.:-11.771 Minor: 80981
## D :24077 Median :0.12800 Median : -7.762
## C# :23201 Mean :0.21501 Mean : -9.570
## A :22671 3rd Qu.:0.26400 3rd Qu.: -5.501
## F :20279 Max. :1.00000 Max. : 3.744
## (Other):88524
## speechiness tempo time_signature valence
## Min. :0.0222 Min. : 30.38 0/4: 8 Min. :0.0000
## 1st Qu.:0.0367 1st Qu.: 92.96 1/4: 2608 1st Qu.:0.2370
## Median :0.0501 Median :115.78 3/4: 24111 Median :0.4440
## Mean :0.1208 Mean :117.67 4/4:200760 Mean :0.4549
## 3rd Qu.:0.1050 3rd Qu.:139.05 5/4: 5238 3rd Qu.:0.6600
## Max. :0.9670 Max. :242.90 Max. :1.0000
##
## 'data.frame': 232725 obs. of 18 variables:
## $ ï..genre : Factor w/ 27 levels "A Capella","Alternative",..: 16 16 16 16 16 16 16 16 16 16 ...
## $ artist_name : Factor w/ 14564 levels "'Til Tuesday",..: 5283 8366 6575 5283 4140 5283 8366 7434 2465 7469 ...
## $ track_name : Factor w/ 148615 levels "' Cello Song",..: 20191 96046 34319 33138 93914 71569 99298 72873 52992 72167 ...
## $ track_id : Factor w/ 176774 levels "00021Wy6AyMbLP2tqij86e",..: 4971 4768 5608 8233 10160 12589 13641 14649 17254 19465 ...
## $ popularity : int 0 1 3 0 4 0 2 15 0 10 ...
## $ acousticness : num 0.611 0.246 0.952 0.703 0.95 0.749 0.344 0.939 0.00104 0.319 ...
## $ danceability : num 0.389 0.59 0.663 0.24 0.331 0.578 0.703 0.416 0.734 0.598 ...
## $ duration_ms : int 99373 137373 170267 152427 82625 160627 212293 240067 226200 152694 ...
## $ energy : num 0.91 0.737 0.131 0.326 0.225 0.0948 0.27 0.269 0.481 0.705 ...
## $ instrumentalness: num 0 0 0 0 0.123 0 0 0 0.00086 0.00125 ...
## $ key : Factor w/ 12 levels "A","A#","B","C",..: 5 10 4 5 9 5 5 10 4 11 ...
## $ liveness : num 0.346 0.151 0.103 0.0985 0.202 0.107 0.105 0.113 0.0765 0.349 ...
## $ loudness : num -1.83 -5.56 -13.88 -12.18 -21.15 ...
## $ mode : Factor w/ 2 levels "Major","Minor": 1 2 2 1 1 1 1 1 1 1 ...
## $ speechiness : num 0.0525 0.0868 0.0362 0.0395 0.0456 0.143 0.953 0.0286 0.046 0.0281 ...
## $ tempo : num 167 174 99.5 171.8 140.6 ...
## $ time_signature : Factor w/ 5 levels "0/4","1/4","3/4",..: 4 4 5 4 4 4 4 4 4 4 ...
## $ valence : num 0.814 0.816 0.368 0.227 0.39 0.358 0.533 0.274 0.765 0.718 ...
The name of variables are as follows:
1.ï..genre:
Type of Genre
2.artist_name:
Artis Name
3.track_name:
Track Name
4.track_id:
Track Id
5.popularity:
Level of Popularity
6.acousticness:
A confidence measure from 0.0 to 1.0 of whether the track is acoustic.A value of 1.0 represents high confidence the track is acoustic.
7.danceability:
Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable.
8.duration_ms:
The duration of the track in milliseconds.
9.energy:
Energy is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy.
10.instrumentalness:
Predicts whether a track contains no vocals. “Ooh” and “aah” sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly “vocal”. The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0.
11.key:
The estimated overall key of the track. Integers map to pitches using standard Pitch Class notation. E.g. 0 = C, 1 = C♯/D♭, 2 = D, and so on. If no key was detected, the value is -1.
12.liveness:
Detects the presence of an audience in the recording. Higher liveness values represent an increased probability that the track was performed live. A value above 0.8 provides strong likelihood that the track is live.
13.loudness:
The overall loudness of a track in decibels (dB). Loudness values are averaged across the entire track and are useful for comparing relative loudness of tracks. Loudness is the quality of a sound that is the primary psychological correlate of physical strength (amplitude). Values typical range between -60 and 0 db.
14.mode:
Mode indicates the modality (major or minor) of a track, the type of scale from which its melodic content is derived. Major is represented by 1 and minor is 0.
15.speechiness:
Speechiness detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks.
16.tempo:
The overall estimated tempo of a track in beats per minute (BPM). In musical terminology, tempo is the speed or pace of a given piece and derives directly from the average beat duration.
17.time_signature:
An estimated overall time signature of a track. The time signature (meter) is a notational convention to specify how many beats are in each bar (or measure).
18.valence:
A measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry).
## ï..genre artist_name track_name track_id
## 0 0 0 0
## popularity acousticness danceability duration_ms
## 0 0 0 0
## energy instrumentalness key liveness
## 0 0 0 0
## loudness mode speechiness tempo
## 0 0 0 0
## time_signature valence
## 0 0
We found no na from the variables dataset.
## 'data.frame': 232725 obs. of 15 variables:
## $ ï..genre : Factor w/ 27 levels "A Capella","Alternative",..: 16 16 16 16 16 16 16 16 16 16 ...
## $ popularity : int 0 1 3 0 4 0 2 15 0 10 ...
## $ acousticness : num 0.611 0.246 0.952 0.703 0.95 0.749 0.344 0.939 0.00104 0.319 ...
## $ danceability : num 0.389 0.59 0.663 0.24 0.331 0.578 0.703 0.416 0.734 0.598 ...
## $ duration_ms : int 99373 137373 170267 152427 82625 160627 212293 240067 226200 152694 ...
## $ energy : num 0.91 0.737 0.131 0.326 0.225 0.0948 0.27 0.269 0.481 0.705 ...
## $ instrumentalness: num 0 0 0 0 0.123 0 0 0 0.00086 0.00125 ...
## $ key : Factor w/ 12 levels "A","A#","B","C",..: 5 10 4 5 9 5 5 10 4 11 ...
## $ liveness : num 0.346 0.151 0.103 0.0985 0.202 0.107 0.105 0.113 0.0765 0.349 ...
## $ loudness : num -1.83 -5.56 -13.88 -12.18 -21.15 ...
## $ mode : Factor w/ 2 levels "Major","Minor": 1 2 2 1 1 1 1 1 1 1 ...
## $ speechiness : num 0.0525 0.0868 0.0362 0.0395 0.0456 0.143 0.953 0.0286 0.046 0.0281 ...
## $ tempo : num 167 174 99.5 171.8 140.6 ...
## $ time_signature : Factor w/ 5 levels "0/4","1/4","3/4",..: 4 4 5 4 4 4 4 4 4 4 ...
## $ valence : num 0.814 0.816 0.368 0.227 0.39 0.358 0.533 0.274 0.765 0.718 ...
We excluded variables artist_name, track_name, track_id due to many level in the factor data type.
We focused on the Jazz genre for further clustering analysis.
spotify_new <- spotify_jazz %>%
select(-c(ï..genre, key, mode, valence, time_signature))
str(spotify_new)## 'data.frame': 9441 obs. of 10 variables:
## $ popularity : int 74 79 72 74 69 67 66 68 67 68 ...
## $ acousticness : num 0.014 0.114 0.524 0.707 0.125 0.846 0.729 0.936 0.906 0.81 ...
## $ danceability : num 0.643 0.697 0.609 0.171 0.561 0.569 0.271 0.464 0.601 0.421 ...
## $ duration_ms : int 192840 214827 236738 182400 193750 282320 139227 255227 184004 337733 ...
## $ energy : num 0.72 0.809 0.435 0.33 0.474 0.117 0.165 0.305 0.222 0.0161 ...
## $ instrumentalness: num 0.00 5.21e-04 0.00 3.81e-03 5.07e-06 8.52e-01 1.60e-06 8.46e-02 4.11e-05 2.10e-03 ...
## $ liveness : num 0.0834 0.183 0.118 0.302 0.0922 0.0978 0.118 0.208 0.0723 0.0978 ...
## $ loudness : num -7.15 -8.2 -7.86 -9.7 -9.64 ...
## $ speechiness : num 0.0527 0.0302 0.284 0.0329 0.155 0.0446 0.0351 0.0316 0.0303 0.0374 ...
## $ tempo : num 96 125.9 132 174.4 86.9 ...
We excluded variables factor data type for further data scaling pre-processing.
We will have our data properly scaled in order to get a useful PCA.
We will generate the principal component from spotify_scale.
pca_spotify <- PCA(spotify_scale,
scale.unit = FALSE,
graph = F,
ncp = 10) #default: 5)
summary(pca_spotify)##
## Call:
## PCA(X = spotify_scale, scale.unit = FALSE, ncp = 10, graph = F)
##
##
## Eigenvalues
## Dim.1 Dim.2 Dim.3 Dim.4 Dim.5 Dim.6 Dim.7
## Variance 2.633 1.334 1.193 1.095 0.980 0.835 0.748
## % of var. 26.337 13.342 11.934 10.950 9.804 8.353 7.480
## Cumulative % of var. 26.337 39.679 51.613 62.563 72.367 80.720 88.200
## Dim.8 Dim.9 Dim.10
## Variance 0.610 0.396 0.174
## % of var. 6.099 3.965 1.735
## Cumulative % of var. 94.299 98.265 100.000
##
## Individuals (the 10 first)
## Dist Dim.1 ctr cos2 Dim.2 ctr cos2
## 1 | 4.236 | 1.653 0.011 0.152 | -1.086 0.009 0.066 |
## 2 | 4.651 | 1.751 0.012 0.142 | -0.646 0.003 0.019 |
## 3 | 4.449 | 0.762 0.002 0.029 | -1.897 0.029 0.182 |
## 4 | 5.144 | -1.232 0.006 0.057 | 0.773 0.005 0.023 |
## 5 | 3.648 | 0.713 0.002 0.038 | -1.584 0.020 0.189 |
## 6 | 4.699 | -3.161 0.040 0.453 | -1.002 0.008 0.045 |
## 7 | 4.561 | -3.043 0.037 0.445 | -0.973 0.008 0.045 |
## 8 | 3.421 | -1.597 0.010 0.218 | -0.413 0.001 0.015 |
## 9 | 3.480 | -1.311 0.007 0.142 | -1.432 0.016 0.169 |
## 10 | 4.965 | -3.676 0.054 0.548 | -0.269 0.001 0.003 |
## Dim.3 ctr cos2
## 1 0.777 0.005 0.034 |
## 2 1.322 0.016 0.081 |
## 3 2.167 0.042 0.237 |
## 4 3.039 0.082 0.349 |
## 5 1.122 0.011 0.095 |
## 6 -0.772 0.005 0.027 |
## 7 1.267 0.014 0.077 |
## 8 1.580 0.022 0.213 |
## 9 1.007 0.009 0.084 |
## 10 1.281 0.015 0.067 |
##
## Variables
## Dim.1 ctr cos2 Dim.2 ctr cos2 Dim.3 ctr
## popularity | -0.072 0.194 0.005 | -0.196 2.878 0.038 | 0.290 7.059
## acousticness | -0.820 25.521 0.672 | -0.155 1.801 0.024 | 0.165 2.278
## danceability | 0.502 9.563 0.252 | -0.515 19.873 0.265 | -0.278 6.455
## duration_ms | 0.006 0.001 0.000 | 0.737 40.716 0.543 | 0.032 0.085
## energy | 0.903 30.954 0.815 | 0.208 3.249 0.043 | -0.023 0.043
## instrumentalness | -0.099 0.374 0.010 | 0.212 3.373 0.045 | -0.741 45.961
## liveness | 0.119 0.535 0.014 | 0.245 4.506 0.060 | 0.565 26.798
## loudness | 0.866 28.495 0.750 | 0.016 0.020 0.000 | 0.064 0.342
## speechiness | 0.311 3.678 0.097 | -0.454 15.473 0.206 | 0.291 7.120
## tempo | 0.134 0.683 0.018 | 0.329 8.110 0.108 | 0.215 3.859
## cos2
## popularity 0.084 |
## acousticness 0.027 |
## danceability 0.077 |
## duration_ms 0.001 |
## energy 0.001 |
## instrumentalness 0.548 |
## liveness 0.320 |
## loudness 0.004 |
## speechiness 0.085 |
## tempo 0.046 |
We have to use 7 Principal Components (PCs) if we only tolerate no more than 15% of information loss.
We will visualize high dimensional data into 2 dimensional plot for various purposes, such as cluster analysis or detecting any outliers.
We found 5 song-id considered as outliers: 3311, 4800, 5089, 6516, 9222
## [1] 99.180 115.299 182.349 170.012 76.868
## $quanti
## correlation p.value
## energy 0.90291391 0.000000e+00
## loudness 0.86631176 0.000000e+00
## danceability 0.50185684 0.000000e+00
## speechiness 0.31125248 3.680572e-211
## tempo 0.13410724 3.859506e-39
## liveness 0.11868880 5.728887e-31
## popularity -0.07156306 3.368689e-12
## instrumentalness -0.09928999 4.039131e-22
## acousticness -0.81985575 0.000000e+00
##
## attr(,"class")
## [1] "condes" "list "
Based on correlation between variables within Dimension 1 or PC 1, We found 5 variables considered as most contributing variables, i.e: energy, loudness, danceability, speechiness, acousticness.
Before clustering the dataset, We will remove the identified outlier based on the previous individual PCA plot.
## 'data.frame': 9436 obs. of 10 variables:
## $ popularity : int 74 79 72 74 69 67 66 68 67 68 ...
## $ acousticness : num 0.014 0.114 0.524 0.707 0.125 0.846 0.729 0.936 0.906 0.81 ...
## $ danceability : num 0.643 0.697 0.609 0.171 0.561 0.569 0.271 0.464 0.601 0.421 ...
## $ duration_ms : int 192840 214827 236738 182400 193750 282320 139227 255227 184004 337733 ...
## $ energy : num 0.72 0.809 0.435 0.33 0.474 0.117 0.165 0.305 0.222 0.0161 ...
## $ instrumentalness: num 0.00 5.21e-04 0.00 3.81e-03 5.07e-06 8.52e-01 1.60e-06 8.46e-02 4.11e-05 2.10e-03 ...
## $ liveness : num 0.0834 0.183 0.118 0.302 0.0922 0.0978 0.118 0.208 0.0723 0.0978 ...
## $ loudness : num -7.15 -8.2 -7.86 -9.7 -9.64 ...
## $ speechiness : num 0.0527 0.0302 0.284 0.0329 0.155 0.0446 0.0351 0.0316 0.0303 0.0374 ...
## $ tempo : num 96 125.9 132 174.4 86.9 ...
We will have our new data scaled in order to get a new PCA.
We will find the optimum cluster number (“K”) to model our data by using Elbow method.
We will limit the plot into 5 distinct clusters.
RNGkind(sample.kind = "Rounding") #to get the set.seed numbers not changed everytime executed
kmeansTunning <- function(data, maxK) {
withinall <- NULL
total_k <- NULL
for (i in 2:maxK) {
set.seed(101)
temp <- kmeans(data,i)$tot.withinss
withinall <- append(withinall, temp)
total_k <- append(total_k,i)
}
plot(x = total_k, y = withinall, type = "o", xlab = "Number of Cluster", ylab = "Total within")
}
# kmeansTunning(your_data, maxK = 5)
kmeansTunning(spotify_scale1, maxK = 15)From the figures above, We will use the optimum number of 6 clusters.
## Warning in RNGkind(sample.kind = "Rounding"): non-uniform 'Rounding' sampler
## used
##
## 1 2 3 4 5 6
## 0.24 0.25 0.07 0.06 0.25 0.14
From the figures above, the biggest 4 clusters is cluster #1 (24%), cluster #2 (25%), cluster #5 (25%), and cluster #6 (14%), which account for 88% out of 6 six clusters.
We will create a Jazz cluster profiling by using a combination of group_by() and summarise_all(), grouped by the previously created cluster column.
From the figures above, the characteristic summary of Jazz Spotify songs in the same cluster are as follows:
Cluster 1: Highest popularity
Cluster 2: Highest energy, highest loudness
Cluster 3: Highest danceability, highest speechines
Cluster 4: Highest duration, highest liveness, highest tempo
Cluster 5: Highest instrumentalness
Cluster 6: Highest acousticness