Overview
This project models Spotify users with machine learning to understand how different groups of people listen. It chains three techniques, PCA, K-Means clustering and Apriori association rules, to turn raw listening behavior into user segments and genre recommendations.
Context: Machine Learning final exam project, Informatics, Universitas Jenderal Soedirman (2025)
Team: 4 people
Tools: Python, Pandas, NumPy, scikit-learn, mlxtend, Matplotlib, Seaborn, Google Colab
Data & Preprocessing
Each user record holds four behavioral features plus a list of frequently played genres:
Total daily listening time (hours)
Average session duration (hours)
Daily access frequency
Songs played per day
The dataset had no missing values. The genre column was split into individual genres, and the numeric features were standardized with StandardScaler so no single feature dominates PCA and clustering.
1. Dimensionality Reduction with PCA
Components | Variance explained |
|---|---|
2 (PC1, PC2) | 51.94% |
3 (PC1 to PC3) | 76.45% |
Two components lose almost half of the information, so three components were kept to preserve most of the signal.
2. Clustering with K-Means
The Elbow Method showed inertia dropping sharply from K = 2 to 4 and flattening after that, and the Silhouette Score also peaked at K = 4, so the 301 users were grouped into four clusters.
Cluster | Users | Daily time | Session | Access/day | Songs/day | Type |
|---|---|---|---|---|---|---|
0 | 96 | 0.68 h | 0.51 h | 1.21 | 12.64 | Light Listener |
1 | 66 | 2.29 h | 0.20 h | 1.12 | 14.88 | Heavy Listener |
2 | 39 | 0.87 h | 0.28 h | 3.51 | 12.90 | Light Listener |
3 | 100 | 0.63 h | 0.21 h | 1.20 | 31.47 | Light Listener |
Cluster 1 is the only Heavy Listener group, spending 2.29 hours a day on Spotify, more than 2.5 times longer than any other cluster. Cluster 3 listens briefly but plays the most songs (31.47 a day), and Cluster 2 opens the app most often (3.51 times a day).
3. Association Rules with Apriori
Inside each cluster, every user's genre list was treated as a transaction and mined with Apriori: minimum support 0.03 and minimum confidence 0.5, with adaptive fallbacks when a cluster produced no rules, and itemsets of up to 3 genres.
Cluster | Strongest rule | Support | Confidence | Lift |
|---|---|---|---|---|
0 | Indie → Country | 0.042 | 0.364 | 2.33 |
1 | Folk + Electronic → Metal | 0.030 | 1.000 | 7.33 |
2 | R&B → Electronic | 0.077 | 0.500 | 1.95 |
3 | Country → Classical | 0.050 | 0.417 | 2.60 |
The standout is the Heavy Listener cluster: users who play Folk and Electronic always play Metal too (100% confidence, lift 7.33), the strongest association in the study, although it covers a small slice of users (3% support).
Recommendations
Cluster 0: recommend Country to Indie listeners
Cluster 1: build playlists that blend Folk, Electronic and Metal for Heavy Listeners
Cluster 2: recommend Electronic to R&B listeners
Cluster 3: recommend Classical to Country listeners
The same segments can drive targeted offers, such as premium subscriptions or personalized playlists for each cluster.
